使用c#表单应用程序更新Access db
我已经编写了一个代码来完成上述任务。没有错误,但访问数据库未更新使用c#表单应用程序更新Access db,c#,C#,我已经编写了一个代码来完成上述任务。没有错误,但访问数据库未更新 private void button1_Click(object sender, EventArgs e) { OleDbConnection mycon = new OleDbConnection(); mycon.ConnectionString =@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Dinesh\C#\GIS_Pow
private void button1_Click(object sender, EventArgs e)
{
OleDbConnection mycon = new OleDbConnection();
mycon.ConnectionString =@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Dinesh\C#\GIS_Power\WindowsFormsApplication1\bin\Power_DB1";
OleDbCommand command = new OleDbCommand();
command.CommandText = "INSERT INTO Table1 (Emp_ID,Asset_ID)VALUES('" + textBox1.Text + "','" + textBox2.Text + "')";
}
问题1:您需要使用
ExecuteNonQuery()
方法执行命令
问题2:您没有通过从连接对象调用open()
方法打开与数据库的连接
问题3:您没有将连接
对象分配给命令
对象
建议:您的惰性INTO
语句容易受到SQL注入攻击
,因此我建议您使用参数化查询
来避免这些攻击
完整代码:
OleDbConnection mycon = new OleDbConnection();
mycon.ConnectionString =@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Dinesh\C#\GIS_Power\WindowsFormsApplication1\bin\Power_DB1";
OleDbCommand command = new OleDbCommand();
command.CommandText = "INSERT INTO Table1 (Emp_ID,Asset_ID) VALUES(@empID,@assetID)";
command.Parameters.AddWithValue("@empID",textBox1.Text);
command.Parameters.AddWithValue("@assetID",textBox2.Text);
mycon.Open();
command.Connection=mycon;
command.ExecuteNonQuery();
OleDbConnection mycon = new OleDbConnection();
mycon.ConnectionString =@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Dinesh\C#\GIS_Power\WindowsFormsApplication1\bin\Power_DB1";
mycon.Open(); //opening connection
OleDbCommand command = new OleDbCommand();
command.CommandText = "INSERT INTO Table1 (Emp_ID,Asset_ID)VALUES('" + textBox1.Text + "','" + textBox2.Text + "')";
command.ExecuteNonQuery(); //executing query
mycon.Close(); //close the connection after executing the query
您没有执行该命令。请执行insert语句,然后只有它将数据插入数据库 这会解决你的问题
OleDbCommand command = new OleDbCommand("//Isert statment here", mycon);
command.ExecuteNonQuery();
您创建了连接字符串,但未打开连接 您创建了查询,但没有执行它 解决方案:
OleDbConnection mycon = new OleDbConnection();
mycon.ConnectionString =@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Dinesh\C#\GIS_Power\WindowsFormsApplication1\bin\Power_DB1";
OleDbCommand command = new OleDbCommand();
command.CommandText = "INSERT INTO Table1 (Emp_ID,Asset_ID) VALUES(@empID,@assetID)";
command.Parameters.AddWithValue("@empID",textBox1.Text);
command.Parameters.AddWithValue("@assetID",textBox2.Text);
mycon.Open();
command.Connection=mycon;
command.ExecuteNonQuery();
OleDbConnection mycon = new OleDbConnection();
mycon.ConnectionString =@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Dinesh\C#\GIS_Power\WindowsFormsApplication1\bin\Power_DB1";
mycon.Open(); //opening connection
OleDbCommand command = new OleDbCommand();
command.CommandText = "INSERT INTO Table1 (Emp_ID,Asset_ID)VALUES('" + textBox1.Text + "','" + textBox2.Text + "')";
command.ExecuteNonQuery(); //executing query
mycon.Close(); //close the connection after executing the query
您需要打开连接
并执行
查询
代码:
OleDbConnection mycon = new OleDbConnection();
mycon.ConnectionString =@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Dinesh\C#\GIS_Power\WindowsFormsApplication1\bin\Power_DB1";
OleDbCommand command = new OleDbCommand();
command.CommandText = "INSERT INTO Table1 (Emp_ID,Asset_ID) VALUES(@empID,@assetID)";
command.Parameters.AddWithValue("@empID",textBox1.Text);
command.Parameters.AddWithValue("@assetID",textBox2.Text);
mycon.Open();
command.Connection=mycon;
command.ExecuteNonQuery();
OleDbConnection mycon = new OleDbConnection();
mycon.ConnectionString =@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Dinesh\C#\GIS_Power\WindowsFormsApplication1\bin\Power_DB1";
mycon.Open(); //opening connection
OleDbCommand command = new OleDbCommand();
command.CommandText = "INSERT INTO Table1 (Emp_ID,Asset_ID)VALUES('" + textBox1.Text + "','" + textBox2.Text + "')";
command.ExecuteNonQuery(); //executing query
mycon.Close(); //close the connection after executing the query
使用
建议:使用<代码>使用<代码>进行连接和(可能?)命令。在这种情况下,费率将非常感谢:)