我有一个WebMethod
调用来varchar(max)
从 SQL Server 检索列。我创建了我需要的存储过程,它在 Management Studio 中运行良好,但是当我运行以下代码时出现错误:
不存在数据时尝试读取无效
代码:
[WebMethod]
public static void PopulatePopUp(string arg)
{
var str = GlobalStatic.ExceptBlanks(arg);
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
SqlDataReader rdr = null;
using (conn)
{
using (SqlCommand cmd = new SqlCommand())
{
conn.Open();
cmd.CommandText = "GetMessage_Sel";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@messageId", SqlDbType.VarChar, -1).Value = str;
cmd.Parameters.Add("@RowCount", SqlDbType.Int).Direction = ParameterDirection.Output;
cmd.Connection = conn;
try
{
rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
string fieldValue = rdr.GetString(0);
}
else
{
Console.WriteLine("No rows found.");
}
rdr.Close();
}
catch (Exception err)
{
// handle the error
//messageInsert = false;
}
finally
{
conn.Close();
}
}
}
}