我遇到了SqlDataReader的问题。当我尝试运行页面时,我收到错误“类型'System.Data.SqlClient.SqlDataReader'没有定义构造函数”。我的目的是在用户没有访问权限时返回字符串值0,或者在用户评估时返回1。以下是我的代码段。
public string CheckAssess(string emailAddress, string columnName)
{
string chkAssess;
SqlDataReader readAssess;
readAssess = new SqlDataReader();
string MgrAssessQry = "SELECT '"+columnName+"' FROM tblAllUsers";
MgrAssessQry += " WHERE email ='" + emailAddress + "'";
SqlCommand cmdReadAssess = new SqlCommand(MgrAssessQry, cn);
cn.Open();
readAssess = cmdReadAssess.ExecuteReader();
while(readAssess.Read())
{
// Add the rows
chkAssess = readAssess["IsAssessMgr"].ToString();
}
return chkAssess;
}
答案 0 :(得分:3)
从SqlDataReader声明中删除New。
试试这个:
更改此
SqlDataReader readAssess;
readAssess = new SqlDataReader();
到
SqlDataReader readAssess;
答案 1 :(得分:2)
SqlDataReader类没有构造函数,将代码重写为以下内容:
public string CheckAssess(string emailAddress, string columnName)
{
string chkAssess;
SqlDataReader readAssess;
string MgrAssessQry = "SELECT '"+columnName+"' FROM tblAllUsers WHERE email ='" + emailAddress + "'";
SqlCommand cmdReadAssess = new SqlCommand(MgrAssessQry, cn);
cn.Open();
readAssess = cmdReadAssess.ExecuteReader();
while(readAssess.Read())
{
chkAssess = readAssess["IsAssessMgr"].ToString();
}
return chkAssess;
}
答案 2 :(得分:1)
尝试类似:
public string CheckAssess(string emailAddress, string columnName)
{
string chkAssess;
string MgrAssessQry = "SELECT '"+columnName+"' FROM tblAllUsers";
MgrAssessQry += " WHERE email ='" + emailAddress + "'";
SqlCommand cmdReadAssess = new SqlCommand(MgrAssessQry, cn);
cn.Open();
SqlDataReader readAssess = cmdReadAssess.ExecuteReader();
while(readAssess.Read())
{
// Add the rows
chkAssess = readAssess["IsAssessMgr"].ToString();
}
return chkAssess;
}
我已将您的SqlDataReader实例化更改为执行查询的行 与MSDN
中的此示例相同http://msdn.microsoft.com/es-es/library/system.data.sqlclient.sqldatareader(v=vs.110).aspx
答案 3 :(得分:1)
张你的
readAssess = new SqlDataReader ();
readAssess = cmdReadAssess.ExecuteReader();
到
SqlDataReader readAssess= cmdReadAssess.ExecuteReader();
答案 4 :(得分:-1)
I had the same problem. I resolved by a simple way like this.
SqlDataReader read = null;
and to check 'read' has row or not
if(read.HasRows)
{
while(read.Read()){}
}
So please see this your code.
public string CheckAssess(string emailAddress, string columnName)
{
string chkAssess;
SqlDataReader readAssess;
readAssess = null; // this from my comments
string MgrAssessQry = "SELECT '"+columnName+"' FROM tblAllUsers";
MgrAssessQry += " WHERE email ='" + emailAddress + "'";
SqlCommand cmdReadAssess = new SqlCommand(MgrAssessQry, cn);
cn.Open();
readAssess = cmdReadAssess.ExecuteReader();
if(readAssess.HasRows && readAssess != null) // this from my comments
{
while(readAssess.Read())
{
// Add the rows
chkAssess = readAssess["IsAssessMgr"].ToString();
}
}
return chkAssess;
}
// I hope this code would help you.