我試圖使用SqlDataReader
來檢查條目是否存在。如果存在,它將返回ID,否則它將返回false。當我嘗試編譯時,出現錯誤「Can not convert method group'Read'to non-delegate type'bool'。我一直在關注在VB中找到的示例,但似乎翻譯可能不正確。無法將方法組'Read'轉換爲非委託類型'bool'
private string checkProfileExists()
{
string strReturn = "False";
string strSql = ("SELECT ID FROM tblInformation WHERE [email protected] " +
"AND [email protected]");
string strConn = ConfigurationManager.ConnectionStrings["WEM_PassWord_Reset"].
ConnectionString;
SqlConnection objConn = new SqlConnection(strConn);
SqlCommand objCmd = new SqlCommand(strSql, objConn);
objCmd.Parameters.AddWithValue("@Username", txtUsername.Text);
objCmd.Parameters.AddWithValue("@TrackingID", txtTrackingID.Text);
try
{
objConn.Open();
System.Data.SqlClient.SqlDataReader rdr = objCmd.ExecuteReader();
if (rdr.Read)
{
strReturn = rdr("ID").ToString;
}
else
{
strReturn = "False";
}
}
catch (Exception ex)
{
lblErrorMessage.Text = ex.ToString();
}
finally
{
objConn.Close();
objCmd = null;
}
return strReturn;
}
Read是SqlDataReader類中的一種方法,在這種情況下,在C#中@Jhonny不參數 – 2011-02-03 18:52:41
,方法調用總是需要*圓括號*,即使它們不帶參數* – AakashM 2011-02-03 18:54:50