我正在尝试获取返回值,但它一直给我一个错误。我试图通过发送用户名来验证用户名后获取“roleid”——我不知道我做错了什么?
public string ValidateRole(string sUsername)
{
string matchstring = "SELECT roleid FROM tblUserRoles WHERE UserName='" + sUsername +"'";
SqlCommand cmd = new SqlCommand(matchstring);
cmd.Connection = new SqlConnection("Data Source=(local);Initial Catalog=samplename;Integrated Security=True");
cmd.Connection.Open();
cmd.CommandType = CommandType.Text;
SqlDataAdapter sda = new SqlDataAdapter();
DataTable dt = new DataTable();
sda.SelectCommand = cmd;
sda.Fill(dt);
string match;
if (dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
match = row["roleid"].ToString();
return match;
}
}
else
{
match = "fail";
return match;
}
}