C# 如何从SQLServer读取id字段并用C在MessageBox上显示它#
这是我的登录页面,它可以工作,但我无法读取数据库中的C# 如何从SQLServer读取id字段并用C在MessageBox上显示它#,c#,sql-server,C#,Sql Server,这是我的登录页面,它可以工作,但我无法读取数据库中的id行 如何读取登录者的id,并使用消息框向其显示值 using System.Data.Sql; using System.Data.SqlClient; namespace WindowsFormsApplication2 { public partial class Form2 : Form { SqlConnection con = new SqlConnection(@"Data Source=
id
行如何读取登录者的
id
,并使用消息框向其显示值
using System.Data.Sql;
using System.Data.SqlClient;
namespace WindowsFormsApplication2
{
public partial class Form2 : Form
{
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-TLOVFL6;Initial Catalog=testsqlinsert;Integrated Security=True");
SqlDataAdapter da;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3();
this.Hide();
f3.ShowDialog();
this.Show();
}
private void button3_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT id,username,password FROM empdata Where username=@username AND password=@password", con);
cmd.Parameters.Add("@username", textBox1.Text);
cmd.Parameters.Add("@password", textBox2.Text);
SqlDataReader dr;
dr = cmd.ExecuteReader();
int c = 0;
while (dr.Read())
{
c += 1;
}
if (c == 1)
{
Form4 f4 = new Form4();
this.Hide();
f4.ShowDialog();
this.Show();
}
else if (c > 0)
{
MessageBox.Show("duplicTE");
}
else
{
MessageBox.Show("USERNAME OR PASSWORD IS NOT CURRECT");
}
textBox1.Clear();
textBox2.Clear();
con.Close();
}
}
}
感谢您的帮助id
行与用户名和密码是同一行还是另一行?因为从您的查询中,它应该是id
。您可以按照以下说明操作:您也可以使用dr[0]或dr[“id”]