本文整理汇总了C#中System.Data.SqlClient.SqlCommand.CreateParameter方法的典型用法代码示例。如果您正苦于以下问题:C# SqlCommand.CreateParameter方法的具体用法?C# SqlCommand.CreateParameter怎么用?C# SqlCommand.CreateParameter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.SqlClient.SqlCommand
的用法示例。
在下文中一共展示了SqlCommand.CreateParameter方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddSqlParameter
/// <summary>
/// SQLのパラメータを追加します。サニタイジング(SQLインジェクション対策)をします
/// </summary>
/// <param name="cmd"></param>
/// <param name="direction"></param>
/// <param name="paramName"></param>
/// <param name="type"></param>
/// <param name="value">値がnullの場合はDBNull.Valueを挿入します</param>
public static void AddSqlParameter(SqlCommand cmd, ParameterDirection direction, string paramName, SqlDbType type, Object value)
{
if (cmd == null)
{
return;
}
if (string.IsNullOrEmpty(paramName) || string.IsNullOrWhiteSpace(paramName))
{
return;
}
if (value == null)
{
value = DBNull.Value;
}
SqlParameter param = cmd.CreateParameter();
param.ParameterName = paramName;
param.SqlDbType = type;
param.Direction = direction;
param.Value = value;
cmd.Parameters.Add(param);
}
示例2: CreateParameter
/// <summary>
/// Creates and adds parameter to SqlCommand
/// </summary>
/// <param name="command">SqlCommand</param>
/// <param name="parameterName">Paramater Name</param>
/// <param name="value">Paramater Value</param>
public void CreateParameter(SqlCommand command, string parameterName, object value)
{
SqlParameter param = command.CreateParameter();
param.ParameterName = parameterName;
param.Value = value;
command.Parameters.Add(param);
}
示例3: ReportAssaysResult
public static DataSet ReportAssaysResult()
{
DataSet ds = new DataSet("ReportResult");
using (new TransactionScope(TransactionScopeOption.Suppress))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
//////Get Curves
cmd.CommandText = "[X_SP_GetAssaysByWorkflow]";
//cmd.CommandText = "[dbo].[X_SP_GetAssays]";
cmd.CommandType = CommandType.StoredProcedure;
var parm1 = cmd.CreateParameter();
parm1.ParameterName = "@assay_group_projectid";
parm1.DbType = DbType.Guid;
parm1.Value = new Guid("31B4CCEC-A72C-4F30-A13A-32B48762FDD9");
//cmd.Parameters.Add(parm1);
try
{
//Let's actually run the queries
cmd.Connection.Open();
cmd.CommandTimeout = 600; //10 mins
var reader = cmd.ExecuteReader();
ds.Load(reader, LoadOption.OverwriteChanges, "t");
}
finally
{
cmd.Connection.Close();
}
return ds;
}
}
示例4: DeletePackageStatistics
private static void DeletePackageStatistics(string connectionString, int warehouseHighWatermark)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
int iterations = 0;
int rows;
do
{
string sql = @"
DELETE TOP(50000) [PackageStatistics]
WHERE [Key] <= @OriginalKey
AND [Key] <= (SELECT DownloadStatsLastAggregatedId FROM GallerySettings)
AND [TimeStamp] < DATEADD(day, -7, GETDATE())
";
SqlCommand command = new SqlCommand(sql, connection);
command.CommandType = CommandType.Text;
SqlParameter parameter = command.CreateParameter();
parameter.DbType = DbType.Int32;
parameter.ParameterName = "@OriginalKey";
parameter.Value = warehouseHighWatermark;
command.Parameters.Add(parameter);
rows = command.ExecuteNonQuery();
}
while (rows > 0 && iterations++ < 10);
}
}
示例5: GetLastOriginalKey
public static int GetLastOriginalKey(string connectionString)
{
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand("GetLastOriginalKey", connection);
command.CommandType = CommandType.StoredProcedure;
command.CommandTimeout = 120;
SqlParameter resultParam = command.CreateParameter();
resultParam.Direction = ParameterDirection.Output;
resultParam.DbType = DbType.Int32;
resultParam.ParameterName = "@OriginalKey";
command.Parameters.Add(resultParam);
command.ExecuteNonQuery();
if (resultParam.Value is DBNull)
{
return 0;
}
return (int)resultParam.Value;
}
}
catch (Exception e)
{
string msg = $"Exception in GetLastOriginalKey (warehouse side): {e.Message}";
throw new ApplicationException(msg, e);
}
}
示例6: Insert_Callstack
public static bool Insert_Callstack(SqlConnection conn, ref SqlTransaction tran, int project_uid, byte[] signature, out int callstack_uid)
{
callstack_uid = 0;
try
{
SqlCommand cmd1 = new SqlCommand("usp_insert_callstack", conn, tran);
cmd1.CommandType = CommandType.StoredProcedure;
SqlParameter returned = cmd1.CreateParameter();
returned.Direction = ParameterDirection.ReturnValue;
cmd1.Parameters.Add(returned);
cmd1.Parameters.AddWithValue("@project_uid", project_uid);
cmd1.Parameters.AddWithValue("@signature", signature);
cmd1.ExecuteNonQuery();
callstack_uid = (int)returned.Value;
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
return false;
}
return true;
}
示例7: CreateCommand
protected override CustomerCommand CreateCommand(DataCommandConfig commandConfig)
{
//1.创建 Connection 对象【需要从连接池中获取连接对象,此处后续优化】
//string strConn = ConfigurationManager.AppSettings["ConnectionString"];
var dbConfig = DataManager.DatabaseDictionary[commandConfig.DataSourceID];
string strConn = dbConfig.ConnectionString;
strConn = string.Format(strConn, ConfigurationManager.AppSettings["Tests.Environment"].ToLower());
SqlConnection dbConnection = new SqlConnection(strConn);
//2.创建 Command 对象
SqlCommand command = new SqlCommand(commandConfig.CommandText, dbConnection);
command.CommandType = CommandType.Text;
//3.填充参数列表
foreach (ParameterConfig param in commandConfig.Parameters)
{
SqlParameter parameter = command.CreateParameter();
parameter.ParameterName = param.Name;
parameter.DbType = param.DBType;
parameter.Size = param.Size == 0 ? 4 : param.Size;
parameter.Direction = ParameterDirection.Input; //默认是输入参数
command.Parameters.Add(parameter);
}
CustomerCommand customerCmd = new CustomerCommand(command, DataBaseType.SQLServer);
return customerCmd;
}
示例8: Select
public override void Select(bool display)
{
string testcommand = @"
select * from Entities
join @intt
on [@intt].Id = Entities.EntityId
";
using (SqlConnection connection = new SqlConnection(this.ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(testcommand, connection))
{
command.CommandTimeout = 5000;
command.CommandType = CommandType.Text;
var param = command.CreateParameter();
param.SqlDbType = SqlDbType.Structured;
param.ParameterName = "@intt";
param.TypeName = "udt_inttable";
param.Value = this.SelectRecords;
command.Parameters.Add(param);
using (var reader = command.ExecuteReader())
{
DisplayReader(display, reader);
}
}
}
}
示例9: Select
public override void Select(bool display)
{
string testcommand = @"
select * from Entities
where Entities.EntityId in (select data from dbo.split(@p1,','))
";
using (SqlConnection connection = new SqlConnection(this.ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(testcommand, connection))
{
command.CommandTimeout = 5000;
command.CommandType = CommandType.Text;
var param = command.CreateParameter();
param.ParameterName = "@p1";
param.Value = string.Join(",", this.SelectRecords.Select(i => i.GetValue(0)).ToArray());
command.Parameters.Add(param);
using (var reader = command.ExecuteReader())
{
DisplayReader(display, reader);
}
}
}
}
示例10: TestBug4689
public void TestBug4689()
{
if (ConnectedDataProvider.GetDbType() != DataBaseServer.SQLServer) {
//All tests in this class are only for MSSQLServer.
Log(string.Format("All tests in this class are only for MSSQLServer and cannot be tested on {0}", ConnectedDataProvider.GetDbType()));
return;
}
try
{
// Every Sub Test must begin with BeginCase
BeginCase("Test Bug 4689 - Exception when adding System.Data.DbType.Date parameter");
SqlCommand command = new SqlCommand();
SqlParameter param = command.CreateParameter();
param.ParameterName = "@EffectiveDate";
param.DbType = DbType.Date;
param.Value = DateTime.Now.Date;
command.Parameters.Add(param);
Pass("Addition of parameter didn't throw exception.");
}
catch(Exception ex)
{
exp = ex;
}
finally
{
// Every Sub Test must end with EndCase
EndCase(exp);
exp = null;
}
}
示例11: AddSqlParameter
private void AddSqlParameter(SqlCommand sqlCommand, string name, SqlDbType sqlDbType, Object value)
{
SqlParameter sqlParameter = sqlCommand.CreateParameter();
sqlParameter.ParameterName = name;
sqlParameter.SqlDbType = sqlDbType;
sqlParameter.Direction = ParameterDirection.Input;
sqlParameter.Value = value;
sqlCommand.Parameters.Add(sqlParameter);
}
示例12: ParameterAdd
/// <summary>
/// Crear parametros para el comando
/// </summary>
public static SqlParameter ParameterAdd(SqlCommand cmd, string nombre, SqlDbType tipo, object valor)
{
SqlParameter para = new SqlParameter();
para = cmd.CreateParameter();
para.ParameterName = nombre;
para.Value = valor;
para.SqlDbType = tipo;
cmd.Parameters.Add(para);
return para;
}
示例13: SetSaveParameters
internal static void SetSaveParameters(SqlCommand command, BusinessBase businessBase)
{
DbParameter idParam = command.CreateParameter();
idParam.DbType = DbType.Int32;
idParam.Direction = ParameterDirection.InputOutput;
idParam.ParameterName = idParamName;
if (businessBase.Id == 0)
{
idParam.Value = DBNull.Value;
}
else
{
idParam.Value = businessBase.Id;
}
command.Parameters.Add(idParam);
DbParameter returnValue = command.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(returnValue);
}
示例14: InsertParameters
protected void InsertParameters(SqlCommand cmd)
{
foreach (var item in _parameters)
{
cmd.Parameters.Add(cmd.CreateParameter()).ParameterName = item.Key;
cmd.Parameters[item.Key].Value = item.Value.Value;
if (item.Value.TranslateDataType)
{
cmd.Parameters[item.Key].SqlDbType = item.Value.DbTranslationType;
}
}
}
示例15: AddParameter
private void AddParameter(SqlCommand dbCmd, string paramName, object paramValue, ParameterDirection direction)
{
if (dbCmd == null)
return;
SqlParameter param = dbCmd.CreateParameter();
param.Direction = direction;
param.ParameterName = paramName;
if (DateTime.MinValue.Equals(paramValue))
param.Value = DBNull.Value;
else
param.Value = paramValue ?? DBNull.Value;
dbCmd.Parameters.Add(param);
}