2010-01-19 86 views
0

我在WPF中有一個窗口。當我用戶輸入名稱時,我想將其插入數據庫並獲取USERID。如何從accessdatabase獲取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(); 
      OleDbCommand 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.** 


    } 
+0

它可能有助於1)說明問題是什麼(EG是你得到的錯誤信息)和2)用你用來編譯和運行SQL的任何編程語言來標記問題 – 2010-01-19 18:50:30

回答

0

我相信你永遠不會初始化ad您使用下面的SQL變量:

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

這是一個問題。

相關問題