如何从SQLServer读取id字段并使用C#在MessageBox上显示它

时间:2017-03-30 00:59:23

标签: c# sql-server

这是我的登录页面,但它无法读取数据库中的行id 如何阅读登录人员的id并使用MessageBox向他显示值?

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();

        }
    }
}

感谢您的帮助

0 个答案:

没有答案