c# - 检查数据是否存在后如何从数据库中获取数据
问题描述
我正在设置一个登录表单,其中包含 2 个用户,即“admin”和“user”。如果用户存在于数据库中,我需要在提供用户名和密码后获取用户类型。我有 2 个用于用户名和密码的文本框(用于输入)和另一个用于用户类型的文本框(查看什么用户类型)。从数据库中检查用户名和密码时,如何显示用户类型?请帮忙。谢谢你。
private void button3_Click(object sender, EventArgs e)
{
string query = "select * from [USER] where username= '" + Unametb.Text + "' and userpw='" + Pwordtb.Text + "'";
using (SqlConnection xcon = new SqlConnection(@"Server=KINGARTHUR\SQLEXPRESS;Database=CENTER;Integrated Security=SSPI;"))
{
using (SqlCommand xcom = new SqlCommand(query, xcon))
{
SqlDataReader xreader;
try
{
if (Unametb.Text.Equals("") && Pwordtb.Text.Equals(""))
{
MessageBox.Show("Username & Password is Empty!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else if (Unametb.Text.Equals(""))
{
MessageBox.Show("Username is Empty!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else if (Pwordtb.Text.Equals(""))
{
MessageBox.Show("Password is Empty!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
xcon.Open();
xreader = xcom.ExecuteReader();
int count = 0;
while (xreader.Read())
{
count = count + 1;
}
if (count == 1)
{
textBox1.Text = xreader.GetValue(3).ToString();
Form b = new Form();
b.Show();
this.Hide();
}
else
{
MessageBox.Show("Username or Password do not match!");
}
loginuser.user = Unametb.Text;
}
}
catch (Exception)
{
throw;
}
finally
{
xcon.Close();
}
}
}
}
解决方案
推荐阅读
- apache-spark - 为什么堆栈函数仅在 selectExpr 中有效,而在 select 中无效
- php - 具有 Genesis 子主题的 Wordpress 模板结构
- java - 尝试通过 Java 在 PowerDesigner 中创建模型时出错
- restart - ARPACK 的 ARNOLDI 迭代中没有重新启动
- python - 用于在 3D 点云上操作权重的 UI
- matrix - 如何在 Power BI 中自动扩展矩阵,同时在 LCD 屏幕上显示
- php - WooCommerce 运输方法 - 条件隐藏(基于账单和运输国家/地区)
- c# - 反正有没有得到控制台光标所在的字符?
- python - 具有不同颜色编码和不同标记的 Sns 散点图
- sql-server - 如何使用 EF Core Fluent API 使用 IGNORE_DUP_KEY 定义索引