日期:2014-05-17 浏览次数:21204 次
private int GetTotalCount() //返回数据总数
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Web_ConnectionString"].ConnectionString);
SqlCommand cmd = con.CreateCommand();
cmd.Parameters.Add(new SqlParameter("@table", SqlDbType.NVarChar, 50) { Value = type }); //type为一string类对象
cmd.CommandText = "select count(*) from @table";
con.Open();
int sum = int.Parse(cmd.ExecuteScalar().ToString()); //此处调试出错
con.Close();
return sum;
}
必须声明表变量 "@type"。
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Data.SqlClient.SqlException: 必须声明表变量 "@type"。
源错误:
行 80:
行 81: con.Open();
行 82: int sum = int.Parse(cmd.ExecuteScalar().ToString());
行 83: con.Close();
行 84: return sum;
源文件: d:\Programming\东乡行\WebSite1\List.aspx.cs 行: 82
堆栈跟踪:
[SqlException (0x80131904): 必须声明表变量 "@type"。]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +2073422
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +5063564
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +234
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2275
System.Data.SqlClient.SqlDataReader.ConsumeMetaData() +33
System.Data.SqlClient.SqlDataReader.get_MetaData() +86
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +311
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +987
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
System.Data.SqlClient.SqlCommand.ExecuteScalar() +139
List.GetTotalCount() in d:\Progr