2013-11-23 48 views
0

我已經使這個小的方法從C#表單插入數據到我的Oracle數據庫。該代碼處理很好,但是當我去SQL Developer來檢查記錄已被插入或沒有,我什麼也沒找到......用c#形式的oracle插入語句不會工作?

 public void conn2db() 
     { 
      try 
      { 
       string connstring = "data source=test_db;user id=system;password=password;"; 
       string statmentcmd = "insert into register_user (userid,username,pass,fullname,phonenum,gender,country) values (" + 1 + "," + textBox1.Text + "," + textBox2.Text + "," + textBox4.Text + "," + textBox5.Text + "," + radioButtonValue+ ","+comboBox1.Text+");"; 

       OracleConnection conn = new OracleConnection(connstring); 
       conn.Open(); 
       MessageBox.Show("connected to database"); 

       OracleCommand cmd = new OracleCommand(); 

       cmd.CommandText=statmentcmd; 
       cmd.Connection=conn; 
       OracleDataAdapter oda = new OracleDataAdapter(cmd); 

       MessageBox.Show(statmentcmd); 

       conn.Close(); 
       MessageBox.Show("Connection closed"); 

      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.ToString()); 
      } 
     } 
+1

你應該逃避字符串或更好的是,在你的命令中使用參數。通過TNS服務致毒 –

+0

問題solved..its未在服務 控制面板>>系統和secuirty >>智優工具>>計算機管理>>服務運行 但我仍然有與插入命令行的問題 string statementcmd =「insert into register_user(pid,username,pwd,fullname)values(」+'1'+「,」+ textBox1.Text +「,」+ textBox2.Text +「,」+ textBox4.Text +「) 「; 錯誤消息編號是ora-00984列不允許在這裏 任何人都可以檢查它嗎?它是雙重qoutes還是單一qoutes? 或它是什麼請 謝謝 – sam

回答

1

嘗試執行這樣的命令:

OracleCommand cmd = new OracleCommand(); 

cmd.CommandText = statmentcmd; 
cmd.Connection = conn; 
cmd.ExecuteNonQuery(); 

或者更簡單地說:

OracleCommand cmd = new OracleCommand(statmentcmd, conn); 
cmd.ExecuteNonQuery(); 
+0

它會拋出一個異常 – sam

+0

@xXghostXx你看到什麼異常? –

+0

https://www.dropbox.com/s/pxpniw260r3novu/Capture.PNG – sam

0

嘗試改變複製到輸出目錄:請notcopy

+0

更多解釋? – sam

+0

找到鏈接http://msdn.microsoft.com/en-us/library/vstudio/ms246989.aspx – Ttami

+2

這個答案是一個開始,但更多的信息會使它成爲一個很好的答案,不僅僅是爲了要求的人,而是爲了其他人誰可能會遇到這個問題。 – Andrew