将参数传递给存储过程无需@符号
问题描述:
嘛,我倒,如果我传递一些参数,无需@
符号将参数传递给存储过程无需@符号
public static int Run(string command,string anotherParameter, DataTable dt)
{
SqlDataAdapter adapter = new SqlDataAdapter("SPName", Connection.Connect);
adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
adapter.SelectCommand.Parameters.Add("@command",SqlDbType.NVarChar).Value=command;
//another one without @
adapter.SelectCommand.Parameters.Add("anotherParameter",SqlDbType.NVarChar).Value=anotherParameter;
try
{
return adapter.Fill(dt);
}
catch (Exception ex)
{
return -1;
}
finally
{
adapter.Dispose();
}
}
这有时会运行和失败的另一个会发生什么?
感谢
答
如果不加"@"
参数名称前,"@"
添加automatically.But好的做法是手动添加@
,不依赖自动功能。 有关更多信息,请参见http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter.parametername.aspx
你试过这段代码吗?你有什么错误 – 2013-04-22 09:16:20
@Mido相当肯定我们有一些代码指定'@',有些则不指定。两者都有效。 – 2013-04-22 09:17:59
@Garath好吧,我试过了,但我注意到它有时候有时候并且有时候不起作用。 – Mido 2013-04-22 09:19:44