2014-01-22 177 views
-4

我遇到了c#中的一個問題。我正在嘗試從文本框中寫入數據到SQL Server Express 2012.我遇到了ExecuteNonQuery()問題;我的查詢一定有問題。這是代碼。SQL Server 2012. c#查詢出錯

void saveData() 
{ 
    try 
    { 
    var firstName = fNameTextbox.Text; 
    var lastName = LNameTextBox.Text; 
    var userName = UserName.Text; 

    String pass = PasswordTextBox.Password; 
    String confirm = ConfirmTextBox.Password; 
    int loggedIn = 1; 

    //parameterise values 
    string connectionString = @"Data Source=.\SQLEXPRESS;Integrated Security=True;User Instance=True"; 
     using (SqlConnection connection = new SqlConnection(connectionString)) 
     using (SqlCommand command = connection.CreateCommand()) 
     { 
      command.CommandText = "INSERT INTO [EVUSERS] (UName, Pass, FName, LName, Attempts, LastLogin, LoggedIn) VALUES (@UName, @Pass, @FName, @LName, @Attempts, @LastLogin, @LoggedIn)"; 

      command.Parameters.AddWithValue("@UName", UserName); 
      command.Parameters.AddWithValue("@Pass", pass); 
      command.Parameters.AddWithValue("@FName", firstName); 
      command.Parameters.AddWithValue("@LName", lastName); 
      command.Parameters.AddWithValue("@Attempts", attempts); 
      command.Parameters.AddWithValue("@LastLogin", lastLogIn); 
      command.Parameters.AddWithValue("@LoggedIn", loggedIn); 

      connection.Open(); 
      command.ExecuteNonQuery(); 
      MessageBox.Show("command number of rows = " + command); 
     } 

     //connection.Close(); 
    } 
    catch (SqlException ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 

} 

錯誤: enter image description here

我想寫表。

enter image description here

想不通的問題。似乎在查詢中。不確定。謝謝。

+0

HTTP://meta.stackexchange。 com/questions/10647/how-do-i-write-a-good-title –

回答

6

在這一行:

command.Parameters.AddWithValue("@UName", UserName); 

你供應TextBox作爲參數,而不是它的內容(userName用小寫字母 'U')

+0

然後人們想知道爲什麼匈牙利符號很有用... ;-) – CesarGon

+1

啊......用戶名和用戶名...像有人一直在閱讀「如何編寫不可維護的代碼」:P – LittleBobbyTables