从数据库读取对/错
大家好,
我有一个包含用户的表.其中一个字段是一个称为Admin的字段,用于存储正确或错误的值.
I have a table with users in. One of the fields is a bit called Admin to store true or false values.
如何使用DataReader读取值?我可以读取字符串,但似乎无法为布尔值做到这一点.
How do I use a DataReader to read the values? I can read strings but can't seem to do it for booleans.
我的代码:
protected void btnLogin_Click(object sender, EventArgs e)
{
string ID = "";
string firstName = "";
string username = "";
string storedPassword = "";
using (SqlConnection dbCon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConMotorstore"].ConnectionString))
{
dbCon.Open();
using (SqlCommand cmd = new SqlCommand("SELECT ID, firstName, username, password, admin FROM [user] WHERE username = @usernameTxt"))
{
cmd.Connection = dbCon;
cmd.Parameters.AddWithValue("usernameTxt", usernameTxt.Text);
SqlDataReader rdr = cmd.ExecuteReader();
var adminUser = rdr.GetOrdinal("admin");
while (rdr.Read())
{
ID = rdr["ID"].ToString();
firstName = rdr["firstName"].ToString();
username = rdr["username"].ToString();
storedPassword = rdr["password"].ToString();
adminUser = rdr.GetBoolean(adminUser);
}
}
}
string inputPassword = passwordTxt.Text;
bool verifyPassword = BCrypt.Net.BCrypt.Verify(inputPassword, storedPassword);
if (verifyPassword == true)
{
Session["ID"] = ID;
Session["FirstName"] = firstName;
Session["username"] = username;
Session["admin"] = adminUser;
Session["active"] = "active";
Response.Redirect("account.aspx");
}
else
{
loginOutput.Text = "Username or password incorrect. Try again.";
}
}
}
}
我得到的错误:无法将bool转换为int.
The error I get: Unable to convert bool to int.
我是C#的新手,对不起任何简单的错误!
I'm new to C#, sorry for any simple errors!
谢谢,
杰克
Thanks,
Jack
DB表列上布尔值的类型是Bit或Int,其值为0 = true和1 = false .这就是保存到DB并从程序的编程方面读取程序在其中进行转换或bool变量解释的位置的方式 为布尔值设置为true或false.
The type for boolean on the DB table column is a Bit or an Int with 0 = true and 1 = false. That is how you save to the DB and reading it where your program does the conversion or the interpretation of the bool variable from a programming aspect of being set to true or false for a boolean.
您可以使用Bing或Google来将布尔值保存到数据库中.
You can use Bing or Google on how to save a boolean value to the DB.