本文整理汇总了C#中System.Data.SqlClient.SqlCommand.AddParameters方法的典型用法代码示例。如果您正苦于以下问题:C# SqlCommand.AddParameters方法的具体用法?C# SqlCommand.AddParameters怎么用?C# SqlCommand.AddParameters使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.SqlClient.SqlCommand
的用法示例。
在下文中一共展示了SqlCommand.AddParameters方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DbCommandExtensionsTest
public DbCommandExtensionsTest()
{
int? nullableInt = null;
var parameters = new { a = "aVal", b = 1, c = false, d = nullableInt };
cmd = new SqlCommand() { CommandType = CommandType.StoredProcedure };
cmd.AddParameters(parameters);
}
示例2: AddNullableParametersTest
public void AddNullableParametersTest()
{
var command = new SqlCommand();
command.AddParameters(new { NullIntParam = new int?(), DateParam = (DateTime?)DateTime.MinValue });
Assert.AreEqual(2, command.Parameters.Count);
Assert.AreEqual(DBNull.Value, command.Parameters["NullIntParam"].Value);
Assert.AreEqual(DateTime.MinValue, command.Parameters["DateParam"].Value);
}
示例3: GetMemberFirstNames
public IEnumerable<string> GetMemberFirstNames(int[] memberIds)
{
var sql = "select firstname from member where memberid in (@memberid)";
using (var cmd = new SqlCommand(sql))
{
cmd.AddParameters("@memberid", memberIds, SqlDbType.Int);
return _helper.ExecuteArray<string>(cmd);
}
}
示例4: AddParametersTest
public void AddParametersTest()
{
var command = new SqlCommand();
command.AddParameters(new { IntParam = 123, DateParam = DateTime.MinValue });
Assert.AreEqual(2, command.Parameters.Count);
Assert.AreEqual(123, command.Parameters["IntParam"].Value);
Assert.AreEqual(DateTime.MinValue, command.Parameters["DateParam"].Value);
command = new SqlCommand();
command.AddParameters<object>(null);
Assert.AreEqual(0, command.Parameters.Count);
}
示例5: AddParameters_AnonymousType_AddsAllPropertiesAsParameters
public void AddParameters_AnonymousType_AddsAllPropertiesAsParameters()
{
var parameters = new {Id = 1, Name = "Reginald", DateOfBirth = new DateTime(1985, 10, 08)};
var cmd = new SqlCommand();
cmd.AddParameters(parameters, s => s.ToSnakeCase().ToLower());
var commandParameters = cmd.Parameters.Cast<SqlParameter>().ToDictionary(p => p.ParameterName, p => p.Value);
var commandParameterKeys = commandParameters.Select(p => p.Key).ToArray();
var expectedKeys = new[] {"id", "name", "date_of_birth"};
expectedKeys.Action(k => Assert.That(commandParameterKeys.Contains(k)));
Assert.AreEqual(1, cmd.Parameters["id"].Value);
Assert.AreEqual("Reginald", cmd.Parameters["name"].Value);
Assert.AreEqual(new DateTime(1985, 10, 08), cmd.Parameters["date_of_birth"].Value);
}
示例6: AddParameters_DictionaryObject_AddsAllParameters
public void AddParameters_DictionaryObject_AddsAllParameters()
{
var parameters = new Dictionary<string, object> {
{"id", 1},
{"name", "Reginald"},
{"date_of_birth", new DateTime(1985, 10, 08)}
};
var cmd = new SqlCommand();
cmd.AddParameters(parameters);
var commandParameters = cmd.Parameters.Cast<SqlParameter>().ToDictionary(p => p.ParameterName, p => p.Value);
var commandParameterKeys = commandParameters.Select(p => p.Key).ToArray();
var missing = parameters.Where(p => p.Key.NotIn(commandParameterKeys));
Assert.AreEqual(0, missing.Count());
foreach (var p in parameters)
Assert.AreEqual(p.Value, cmd.Parameters[p.Key].Value);
}
示例7: VerifyExtensionMethods
public void VerifyExtensionMethods()
{
var excludeTheseStateAbbreviations = new string[] { "CO", "CA", "TX" };
var sql = "select * from state where abbreviation not in (@abbreviation)";
using (var cmd = new SqlCommand(sql))
{
cmd.AddParameters("@abbreviation", excludeTheseStateAbbreviations);
var data = _helper.ExecuteDataTable(cmd);
Assert.AreEqual(68, data.Rows.Count);
}
}