c#
  • sql
  • ms-access
  • 2011-02-03 29 views 0 likes 
    0

    我不得不更新表我嘗試了兩種查詢,但它顯示了語法錯誤誰能幫助我請我知道這是基本的,但我是一個初學者更正語法

    String query = "Update masterusertable set username='" + txtName.Text + "',and set password='" + txtpassword.Text + "',and set phoneno='" + txtphoneno.Text + "',and set adress='" + rtxtAdress.Text + "' where userid ='" + txtuserid.Text + "'"; 
    
    String query = "Update masterusertable set username='" + txtName.Text + "', password='" + txtpassword.Text + "', phoneno='" + txtphoneno.Text + "', adress='" + rtxtAdress.Text + "' where userid ='" + txtuserid.Text + "'"; 
    

    任何其它的想法,我有許多大表更新使用任何其他想法是更好的。

    +0

    您正在使用什麼數據庫管理系統? – 2011-02-03 12:46:32

    +0

    你能發佈錯誤信息嗎? – Tony 2011-02-03 12:46:52

    回答

    0

    對於每個正在更新的字段,不需要SET命令。你只需要第一個SET命令。

    試試這個:

    String query = "Update masterusertable set username='" + txtName.Text + "',and password='" + txtpassword.Text + "',and phoneno='" + txtphoneno.Text + "',and adress='" + rtxtAdress.Text + "' where userid ='" + txtuserid.Text + "'"; 
    

    而且,這將有助於看到實際的查詢被髮送到SQL Server(如果這就是你正在使用的DB)和你所得到的錯誤。

    1

    您不必使用所有SET字段。也不需要多次使用SET您可以將它們分開,。

    update table set col1=val1,col2=val2.... where coln=valn 
    
    4

    這是SQL Injection attacks敞開的 - 你應該更改爲parameterized queries

    至於UPDATE語法只有一個SET條款:

    UPDATE masterusertable 
    SET 
    username= @username, 
    password= @password, 
    phoneno= @phoneno, 
    adress= @address 
    WHERE userid = @userid 
    
    0

    您是否使用字符串或爲您的主鍵是多少? 語句的WHERE部分格式的字符串:

    where userid ='" + txtuserid.Text + "'" 
    

    這是你想要的嗎?

    但既然你說這個問題是一個語法錯誤,這似乎沒有,我猜這不是問題。

    0

    對於concaterating的字符串時,我也建議你使用的String.format可讀性的原因..

    String query = string.Format(
          "Update masterusertable set username='{0}', password='{1}', phoneno='{2}', adress='{3}' where userid ='{4}'", 
          txtName.Text, 
          txtpassword.Text, 
          txtphoneno.Text, 
          rtxtAdress.Text, 
          txtuserid.Text 
         ); 
    
    相關問題