开发者

How to get USERID from accessdatabase

开发者 https://www.devze.com 2022-12-17 23:27 出处:网络
I have a window in WPF. When I user enter name, I want to insert it to database and get USERID. private void button1_Click(object sender, RoutedEventArgs e)

I have a window in WPF. When I user enter name, I want to insert it to database and get USERID.

private void button1_Click(object sender, RoutedEventArgs e)
        {
            OleDbDataReader rd;
            string name=comboBox1.Text;
            OleDbConnection conn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|CellBiology.mdb;Persist Security Info=

True");
            string sql = "select * from UserInformation where UserName='" + name+ "'";
            conn.Open();
            OleDbCom开发者_开发技巧mand cmd = new OleDbCommand(sql, conn);
            rd = cmd.ExecuteReader();
            if (rd.Read())
            {

                string id = rd["UserID"].ToString();
                MessageBox.Show(id);


            }
            else
            {
                string sql2 = "insert into UserInformation(UserName) values ('" + ad+ "')";
                OleDbCommand ne = new OleDbCommand(sql2, conn);
                ne.ExecuteNonQuery();

**the problem is here.**


     }


I believe you are never initializing the variable ad you are using in the following SQL:

string sql2 = "insert into UserInformation(UserName) values ('" + ad+ "')";

Which is a problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号