2013-04-06 61 views
2

我有一個簡單的創建一個新的用戶窗體,它從兩個文本框,用戶名和密碼的值。 button2 click事件應該取這些值並將它們插入數據庫的Users表中。但是,當我運行我的代碼時,消息框似乎表示數據已添加,我無法使用VS2010查看數據庫中的數據。C#表單不插入值到SQL Server數據庫

請參閱VS中數據庫連接的屏幕截圖。我也創建了VS中數據庫的數據源。

任何想法?

非常感謝。

private void button2_Click(object sender, EventArgs e) 
    { 
     string username = txtUsername.Text; 
     string password = txtPassword.Text; 
     string sqlquery; 
     string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Users\Nick\Documents\Visual Studio 2010\Projects\DebenhamsProjectOffice V.01\DebenhamsProjectOffice V.01\DebenhamsProjectOfficeDatabase.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True"; 
     SqlConnection cn = new SqlConnection(connection); 
     try 
     { 
      cn.Open(); 
     } 
     catch (Exception) 
     { 
      MessageBox.Show("Unable to connect to Database"); 
     } 

     sqlquery = "INSERT INTO Users (Username, Password) VALUES ('" + txtUsername.Text + "','" + txtPassword.Text + "')"; 
     try 
     { 
      SqlCommand command = new SqlCommand(sqlquery, cn); 
      command.Parameters.AddWithValue("Username", username); 
      command.Parameters.AddWithValue("Password", password); 
      command.Parameters.Clear(); 
      MessageBox.Show("User Added"); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
     txtUsername.Text = ""; 
     txtPassword.Text = ""; 
     cn.Close(); 
    } 

enter image description here

回答

1

只是嘗試一個代碼修復。有些元素是至關重要的,有些則是優雅的。 試試,它可能會奏效,或者可能指向錯誤所在:

private void button2_Click(object sender, EventArgs e) 
    { 
     string username = txtUsername.Text; 
     string password = txtPassword.Text; 
     string sqlquery; 

     //Put away the apostrophes and used twice double quotations for 
     //the full path of the database file: 
     string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=""C:\Users\Nick\Documents\Visual Studio 2010\Projects\DebenhamsProjectOffice V.01\DebenhamsProjectOffice V.01\DebenhamsProjectOfficeDatabase.mdf"";Integrated Security=True;Connect Timeout=30;User Instance=True"; 
     SqlConnection cn = new SqlConnection(connection); 

     /* Better to let the program fail than think it's open and moving on 
     removed try, catch*/ 
     cn.Open(); 


     //Why using your TextBoxes values if you already created strings? 
     //changed 

     //you should also be careful users can't type something like "') in the  
     //textboxes or they may cause a SQL injection 

     sqlquery = "INSERT INTO Users (Username, Password) VALUES ('" + username + "','" + password + "')"; 

     try 
     { 
      SqlCommand command = new SqlCommand(sqlquery, cn); 
      /* unnecessary since you already built a query command.Parameters.AddWithValue("Username", username); 
      command.Parameters.AddWithValue("Password", password); 
      command.Parameters.Clear(); */ 

      //Missing!! 
      command.ExecuteNonQuery(); 
      MessageBox.Show("User Added"); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 

     //Elegance 
     txtUsername.Clear(); 
     txtPassword.Clear(); 
     cn.Close(); 
    } 
+0

非常感謝你花時間解決這個問題。它現在工作! – NickB6 2013-04-08 10:49:17

2

你必須調用Command.ExecuteNonQuery(),以便採取insert語句的效果。

try 
{ 
     SqlCommand command = new SqlCommand(sqlquery, cn); 
     command.Parameters.AddWithValue("Username", username); 
     command.Parameters.AddWithValue("Password", password); 
     command.ExecuteNonQuery(); 
     command.Parameters.Clear(); 
     MessageBox.Show("User Added"); 
} 
catch (Exception ex) 
{ 
     MessageBox.Show(ex.Message); 
} 
4

整個用戶實例和AttachDbFileName =方法是有缺陷的 - 在最好的!在Visual Studio中運行應用程序時,它將複製.mdf文件(從您的App_Data目錄到輸出目錄 - 通常爲.\bin\debug) - 最有可能爲,您的INSERT工作得很好 - 但是您只是看着錯誤.mdf文件到底!

如果你想堅持這種方法,那麼試着在myConnection.Close()調用上放一個斷點 - 然後用SQL Server Mgmt Studio Express檢查.mdf文件 - 我幾乎可以確定你的數據在那裏。

在我看來真正的解決方案

  1. 安裝SQL Server Express(和你已經做到這一點無論如何)

  2. 安裝SQL Server Management Studio中快速

  3. 中創建您的數據庫SSMS Express,給它一個邏輯名稱(例如DebenhamsProjectOfficeDatabase

  4. 使用其邏輯數據庫名稱連接到它(在服務器上創建時給出) - 並且不要亂用物理數據庫文件和用戶實例。在這種情況下,您的連接字符串將是這樣的:

    Data Source=.\\SQLEXPRESS;Database=DebenhamsProjectOfficeDatabase;Integrated Security=True 
    

    和其他一切是正是和以前一樣......

另外:你應該總是使用參數化查詢而不是將您的SQL語句(,特別是,不包括用戶輸入時)連接在一起!(a)避免任何SQL注入攻擊的危險,並且(b)提高性能!

+0

非常感謝您的意見,我會嘗試這一點,對於天真道歉我是新手C#程序員。非常感激。 – NickB6 2013-04-06 18:14:18

+1

'catch(Exception)'是另一個有缺陷的方法。 – 2013-04-06 20:17:30

相關問題