2014-01-22 122 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