当前位置: 首页>>代码示例>>C#>>正文


C# SqlCommand.AddParameters方法代码示例

本文整理汇总了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);
 }
开发者ID:24hr,项目名称:Clippy,代码行数:7,代码来源:DbCommandExtensionsTest.cs

示例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);
        }
开发者ID:ShomreiTorah,项目名称:Libraries,代码行数:9,代码来源:DBTest.cs

示例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);
     }
 }
开发者ID:chrcar01,项目名称:SimpleTasks,代码行数:9,代码来源:MemberRepository.cs

示例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);
        }
开发者ID:ShomreiTorah,项目名称:Libraries,代码行数:13,代码来源:DBTest.cs

示例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);
        }
开发者ID:veracross,项目名称:ncontrib,代码行数:18,代码来源:SqlCommandExtensionsTests.cs

示例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);
        }
开发者ID:veracross,项目名称:ncontrib,代码行数:21,代码来源:SqlCommandExtensionsTests.cs

示例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);
			}
		}
开发者ID:org-itbiz,项目名称:SqlTools,代码行数:11,代码来源:SqlDbHelperTests.cs


注:本文中的System.Data.SqlClient.SqlCommand.AddParameters方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。