2016-09-25 17 views
1

這裏是我的保存按鈕命令。 需要幫助才能得到這份工作,將爲明天的學校項目得到此辯護。 謝謝!其爲Datagridview,訪問,C#。 我使用2010VS和MS Access 2007在Database/Datagrid上插入和更新語法錯誤

private void save_Click(object sender, EventArgs e) 
    { 

     if (adminyes.Checked == true || adminno.Checked == true && textBox1.Text != null && textBox2.Text != null && textBox3.Text != null) 
     { 
      admin = "Yes"; 

      if (mode == "a") 
      { 
       x = 0; 
       connect.Close(); 
       connect.ConnectionString = inventorydb; 
       connect.Open(); 
       sqlcommand.CommandText = "SELECT * FROM Users WHERE Username ='" +textBox2.Text+ "' Or User_ID ='" +textBox1.Text+ "' "; 
       sqlcommand.Connection = connect; 
       OleDbDataReader reader = sqlcommand.ExecuteReader(); 
       while (reader.Read()) 
       { 
        x++; 
       } 

       if (x != 0) 
       { 
        MessageBox.Show("", "",MessageBoxButtons.OK); 
       } 
       else 
       { 
        DialogResult res = MessageBox.Show("Are you sure?", "Save User", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); 

        if (DialogResult.Yes == res) 
        { 
         connect.Close(); 
         connect.ConnectionString = inventorydb; 
         connect.Open(); 
         sqlcommand.CommandText = "INSERT INTO Users (User_ID, Username, Password, Admin) VALUES ('" + textBox1.Text + "','" + textBox2.Text + "', '" + textBox3.Text + "', '" + admin + "') "; 
         sqlcommand.Connection = connect; 
         reader = sqlcommand.ExecuteReader(); 
         MessageBox.Show("Record(s) Saved", "Sample"); 
        } 

        reset(); 
       } 
      } 
      else if (mode == "e") 
      { 
       DialogResult res = MessageBox.Show("Are you sure?", "Update User", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); 

       if (DialogResult.Yes == res) 
       { 
        connect.Close(); 
        connect.ConnectionString = inventorydb; 
        connect.Open(); 
        sqlcommand.CommandText = "UPDATE Users SET User_ID = '" + textBox1.Text + "', Username = '" + textBox2.Text + "', Password = '" + textBox3.Text + "',Admin = '" + admin + "' WHERE SerialID = '" + idholder + "' "; 
        sqlcommand.Connection = connect; 
        OleDbDataReader reader = sqlcommand.ExecuteReader(); 
        reader.Read(); 
        MessageBox.Show("Record(s) Updated", "Sample"); 

       } 

       reset(); 
      } 
     } 
     else 
     { 
      MessageBox.Show("", "", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
    } 

回答

1

Password是Access保留字。在您的SQL查詢中將其更改爲[Password]。你應該像這樣包裝所有的列和表。

雖然這僅僅是一個學校的項目,我會提到幾件事情:

您的代碼很容易受到SQL注入。作爲示例,以下是如何解決此問題的方法:

sqlcommand.CommandText = "INSERT INTO [Users] ([User_ID], [Username], [Password], [Admin]) VALUES (@user_id, @username, @password, @admin)"; 
sqlcommand.Connection = connect; 
sqlcommand.Parameters.AddWithValue("@user_id", textBox1.Text); 
sqlcommand.Parameters.AddWithValue("@username", textBox2.Text); 
sqlcommand.Parameters.AddWithValue("@password", textBox3.Text); 
sqlcommand.Parameters.AddWithValue("@admin", admin); 
reader = sqlcommand.ExecuteReader(); 

此外,密碼不應以純文本形式存儲。查看密碼散列和醃製以及如何正確處理以獲取更多信息。

+0

謝謝.. !!! 它的工作原理,我試圖找出如何保護密碼。 我不知道如何但真的非常感謝! – Richard

+0

只是一個簡單的問題先生,我有一個問題,每當我添加/編輯/刪除我的數據上的項目,然後關閉程序並再次打開後,我做的所有更改不會永久保存/反映在訪問數據庫,你知道如何解決它嗎? – Richard

+0

也許你需要調用'OleDbCommand.ExecuteNonQuery'而不是'ExecuteReader'。另外,如果你有一個事務處於活動狀態(雖然我沒有看到),調用'OleDbTransaction.Commit'。如果這不起作用,請嘗試使用'OleDb'對象查找訪問數據庫的一些示例查詢,並查看是否有任何差異。我對Access數據庫不太熟悉。 – dukedukes