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);
}
}
謝謝.. !!! 它的工作原理,我試圖找出如何保護密碼。 我不知道如何但真的非常感謝! – Richard
只是一個簡單的問題先生,我有一個問題,每當我添加/編輯/刪除我的數據上的項目,然後關閉程序並再次打開後,我做的所有更改不會永久保存/反映在訪問數據庫,你知道如何解決它嗎? – Richard
也許你需要調用'OleDbCommand.ExecuteNonQuery'而不是'ExecuteReader'。另外,如果你有一個事務處於活動狀態(雖然我沒有看到),調用'OleDbTransaction.Commit'。如果這不起作用,請嘗試使用'OleDb'對象查找訪問數據庫的一些示例查詢,並查看是否有任何差異。我對Access數據庫不太熟悉。 – dukedukes