日期:2014-05-18  浏览次数:20981 次

请教:SqlCommand运行时出错
int i;
int n;
string mySelectQuery1 = "select count(*) as zhongshu from (select top @m_i * from lott) where N1=@m_n";

SqlConnection myConn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand myCommand = new SqlCommand(mySelectQuery1,myConn);
myCommand.Parameters.Add(new SqlParameter("@m_n", SqlDbType.Int,0)).Value=n;
myCommand.Parameters.Add(new SqlParameter("@m_i", SqlDbType.Int,0)).Value=i;

myConn.Open();
SqlDataReader myReader = myCommand.ExecuteReader();


执行时,报错:(请问:怎么回事?)
System.Data.SqlClient.SqlException: 第 1 行: '@m_i' 附近有语法错误。
  at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
  at System.Data.SqlClient.SqlCommand.ExecuteReader()
  at lott.dbworker.lott_n(Int32 n, Int32 i) in e:\_____learn\3。software_develop\practice\lottery\lott\dbworker.cs:line 137(有颜色那行)


------解决方案--------------------
SQL code
create procedure proc_test
(@m_i int,@m_n int)
AS
exec('select count(*) as zhongshu from (select top '+@m_i+' * from lott) where N1='''+@m_n+'''')