将参数传递给没有@符号的存储过程

时间:2013-04-22 09:14:02

标签: c# sql-server stored-procedures

好吧,我很好奇如果我传递一些没有@符号

的参数会发生什么
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();
        }     

 }

这有时可能会运行而另一个会失败吗?

由于

1 个答案:

答案 0 :(得分:1)

如果您未在参数名称前添加"@",则会自动添加"@"。但最好的做法是手动添加@并且不依赖于自动功能。 有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter.parametername.aspx

相关问题