我有一個WebMethod
被調用以檢索從SQL Server varchar(max)
柱。創建我neccessary的存儲過程,這在Management Studio中工作正常,但是當我運行下面的代碼我得到一個錯誤:檢索VARCHAR(MAX)使用C#SQL Server和存儲過程
Invalid attempt to read when no data is present
代碼:
[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();
}
}
}
}
你能後的程序是什麼? – DoctorMick
那麼你需要一個'rdr.Read()'調用 – V4Vendetta