在 C# 中使用 oledb 更新数据
update data using oledb in c#
我使用 c# 创建了一个项目,使用 access accdb 创建了一个数据库,并在它们之间进行了连接。我制作了 2 个按钮,第一个用于添加新客户,效果很好,第二个用于更新客户的数据(名字和姓氏),由于某种原因,更新按钮不起作用,当我运行这个项目,但是我点击之后没有任何反应...
private void button2_Click(object sender, EventArgs e)
{
connect.Open();
string cid = textBox1.Text;
string cfname = textBox2.Text;
string clname = textBox3.Text;
OleDbCommand command = new OleDbCommand();
command.Connection = connect;
command.CommandText = "UPDATE Tcostumers SET cfname= " + cfname + "clname= " + clname + " WHERE cid = " + cid;
if (connect.State == ConnectionState.Open)
{
try
{
command.ExecuteNonQuery();
MessageBox.Show("DATA UPDATED");
connect.Close();
}
catch (Exception expe)
{
MessageBox.Show(expe.Source);
connect.Close();
}
}
else
{
MessageBox.Show("ERROR");
}
}
这可能是一件愚蠢的事情,但是...
您更新的是字符串而不是整数,因此请尝试在您的字符串中添加“”,例如:
command.CommandText = "UPDATE Tcostumers SET cfname= '" + cfname + "' clname='" + clname + "' WHERE cid = " + cid;
我相信你的命令文本就是问题所在;
command.CommandText = "UPDATE Tcostumers SET cfname= " + cfname + "clname= " + clname + " WHERE cid = " + cid;
你需要在 set 语句之间使用逗号,正如 Gino 指出的那样。
编辑:
这比你为变量使用参数要好,你现在的方法是开放给SQL注入的,例如
private void button2_Click(object sender, EventArgs e)
{
OleDbCommand command = new OleDbCommand(@"UPDATE Tcostumers
SET cfname = @CFName,
clname = @CLName
WHERE cid = @CID", connect);
command.Parameters.AddWithValue("@CFName", textBox2.Text);
command.Parameters.AddWithValue("@CLName", textBox3.Text);
command.Parameters.AddWithValue("@CID", textBox1.Text);
try
{
connect.Open();
}
catch (Exception expe)
{
MessageBox.Show(expe.Source);
}
try
{
command.ExecuteNonQuery();
MessageBox.Show("DATA UPDATED");
}
catch (Exception expe)
{
MessageBox.Show(expe.Source);
}
finally
{
connect.Close();
}
}
这就是我倾向于格式化代码的方式,所以请按照您的意愿进行操作。希望对你有帮助。
//my sample code for edit/update
Table Name = StudentFIle
Fields = id,fname,lname
bool found = false;
OleDbConnection BOMHConnection = new OleDbConnection(connect);
string sql = "SELECT * FROM StudentFIle";
BOMHConnection.Open();
OleDbCommand mrNoCommand = new OleDbCommand(sql, BOMHConnection);
OleDbDataReader mrNoReader = mrNoCommand.ExecuteReader();
while (mrNoReader.Read())
{
if (mrNoReader["id"].ToString().ToUpper().Trim() == idtextbox.Text.Trim())
{
mrNoReader.Close();
string query = "UPDATE StudentFIle set fname='" +firstnametextbox.Text+ "',lname='"+lastnametextbox.Text+"' where id="+idtextbox.Text+" ";
mrNoCommand.CommandText = query;
mrNoCommand.ExecuteNonQuery();
MessageBox.Show("Successfully Updated");
found = true;
break;
}
continue;
}
if (found == false)
{
MessageBox.Show("Id Doesn't Exist !.. ");
mrNoReader.Close();
BOMHConnection.Close();
idtextbox.Focus();
}
我使用 c# 创建了一个项目,使用 access accdb 创建了一个数据库,并在它们之间进行了连接。我制作了 2 个按钮,第一个用于添加新客户,效果很好,第二个用于更新客户的数据(名字和姓氏),由于某种原因,更新按钮不起作用,当我运行这个项目,但是我点击之后没有任何反应...
private void button2_Click(object sender, EventArgs e)
{
connect.Open();
string cid = textBox1.Text;
string cfname = textBox2.Text;
string clname = textBox3.Text;
OleDbCommand command = new OleDbCommand();
command.Connection = connect;
command.CommandText = "UPDATE Tcostumers SET cfname= " + cfname + "clname= " + clname + " WHERE cid = " + cid;
if (connect.State == ConnectionState.Open)
{
try
{
command.ExecuteNonQuery();
MessageBox.Show("DATA UPDATED");
connect.Close();
}
catch (Exception expe)
{
MessageBox.Show(expe.Source);
connect.Close();
}
}
else
{
MessageBox.Show("ERROR");
}
}
这可能是一件愚蠢的事情,但是...
您更新的是字符串而不是整数,因此请尝试在您的字符串中添加“”,例如:
command.CommandText = "UPDATE Tcostumers SET cfname= '" + cfname + "' clname='" + clname + "' WHERE cid = " + cid;
我相信你的命令文本就是问题所在;
command.CommandText = "UPDATE Tcostumers SET cfname= " + cfname + "clname= " + clname + " WHERE cid = " + cid;
你需要在 set 语句之间使用逗号,正如 Gino 指出的那样。
编辑:
这比你为变量使用参数要好,你现在的方法是开放给SQL注入的,例如
private void button2_Click(object sender, EventArgs e)
{
OleDbCommand command = new OleDbCommand(@"UPDATE Tcostumers
SET cfname = @CFName,
clname = @CLName
WHERE cid = @CID", connect);
command.Parameters.AddWithValue("@CFName", textBox2.Text);
command.Parameters.AddWithValue("@CLName", textBox3.Text);
command.Parameters.AddWithValue("@CID", textBox1.Text);
try
{
connect.Open();
}
catch (Exception expe)
{
MessageBox.Show(expe.Source);
}
try
{
command.ExecuteNonQuery();
MessageBox.Show("DATA UPDATED");
}
catch (Exception expe)
{
MessageBox.Show(expe.Source);
}
finally
{
connect.Close();
}
}
这就是我倾向于格式化代码的方式,所以请按照您的意愿进行操作。希望对你有帮助。
//my sample code for edit/update
Table Name = StudentFIle
Fields = id,fname,lname
bool found = false;
OleDbConnection BOMHConnection = new OleDbConnection(connect);
string sql = "SELECT * FROM StudentFIle";
BOMHConnection.Open();
OleDbCommand mrNoCommand = new OleDbCommand(sql, BOMHConnection);
OleDbDataReader mrNoReader = mrNoCommand.ExecuteReader();
while (mrNoReader.Read())
{
if (mrNoReader["id"].ToString().ToUpper().Trim() == idtextbox.Text.Trim())
{
mrNoReader.Close();
string query = "UPDATE StudentFIle set fname='" +firstnametextbox.Text+ "',lname='"+lastnametextbox.Text+"' where id="+idtextbox.Text+" ";
mrNoCommand.CommandText = query;
mrNoCommand.ExecuteNonQuery();
MessageBox.Show("Successfully Updated");
found = true;
break;
}
continue;
}
if (found == false)
{
MessageBox.Show("Id Doesn't Exist !.. ");
mrNoReader.Close();
BOMHConnection.Close();
idtextbox.Focus();
}