不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
给SQL 文本命令中的参数传值
编辑:dnawo 日期:2008-08-27
SqlCommand对象的CommandType属性值有三种类型:Text、StoredProcedure和TableDirect,我们都知道,在存储过程中使用参数是非常常见的,因而我们都知道当CommandType值为StoredProcedure时怎么给存储过程参数传值,但是要知道SQL 文本命令中也是可以带参数的,那怎么给他们传值呢?其实两者是一样的,看下边例子:
在SQL 事件探查器中看到的结果为:
复制内容到剪贴板
程序代码

string Country = "UK";
string City = "London";
using (SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Northwind;User ID=sa;Password=sa"))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandTimeout = 60;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select count(*) From [Customers] Where Country=@Country And City=@City";
cmd.Parameters.Add(new SqlParameter("@Country",Country));
cmd.Parameters.Add(new SqlParameter("@City",City));
conn.Open();
Response.Write(cmd.ExecuteScalar());
}
string City = "London";
using (SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Northwind;User ID=sa;Password=sa"))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandTimeout = 60;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select count(*) From [Customers] Where Country=@Country And City=@City";
cmd.Parameters.Add(new SqlParameter("@Country",Country));
cmd.Parameters.Add(new SqlParameter("@City",City));
conn.Open();
Response.Write(cmd.ExecuteScalar());
}
在SQL 事件探查器中看到的结果为:
复制内容到剪贴板
程序代码

exec sp_executesql N'Select count(*) From [Customers] Where Country=@Country And City=@City', N'@Country nvarchar(2),@City nvarchar(6)', @Country = N'UK', @City = N'London'






评论: 0 | 引用: 0 | 查看次数: 6790
发表评论
请登录后再发表评论!