以下代码会产生错误。当我直接在服务器上调用 dbo.getit 时,它可以正常工作。错误发生在 cmd.ExecuteReader() 。我究竟做错了什么?
string user;
string pw;
SqlDataReader dr = null;
SqlConnection conn = new SqlConnection("Data Source=xxx;Initial Catalog=myDB; Integrated Security=True");
user = Username.Text.Trim();
pw = Password.Text.Trim();
conn.Open();
try {
SqlCommand cmd = new SqlCommand("dbo.getit", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@param1", user);
cmd.Parameters.AddWithValue("@param2", pw);
dr = cmd.ExecuteReader();
while ( dr.Read() )
{
Session["username"] = user;
// Session["admin"] =
// Session["completed"] =
Server.Transfer("all_is_well.aspx");
}
conn.Close();
conn.Dispose();
} catch (Exception ex) {
if (dr != null)
{
dr.Close();
conn.Close();
}
Server.Transfer("ERROR.aspx");
}
解决方案:将上面对应的两行替换为:
SqlCommand cmd = new SqlCommand("select * from dbo.getit(@param1, @param2);", conn);
cmd.CommandType = CommandType.text;