请查找附件,我编写的代码,但是当它运行时以及当我以表格形式输入数据时n点击提交它会在"Command.executenonquery statement"
给我错误。
显示Invalid Operation exception was mishandled
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\databses\electric_data.accdb";
OleDbCommand command = new OleDbCommand();
command.CommandText = "INSERT INTO Electric_Data (Asset_Id,Asset_Name,Emp_Id,Type_of_Asset,Actual_Start_date) VALUES (?,?,?,?,?)";
command.Parameters.Add("@Asset_Id", OleDbType.VarChar, 20).Value = textBox1.Text;
command.Parameters.Add("@Asset_Name", OleDbType.Char, 20).Value = textBox2.Text;
command.Parameters.Add("@Type_of_Asset", OleDbType.VarChar, 20).Value = textBox3.Text;
command.Parameters.Add("@Emp_Id", OleDbType.Char, 20).Value = textBox4.Text;
command.Parameters.Add("@Actual_Start_date", OleDbType.Date).Value = DateTime.Now;
command.ExecuteNonQuery();
conn.Open();
command.Connection = conn;
MessageBox.Show("Entry Registered Successfully.");
}
if (radioButton2.Checked == true)
{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\databses\electric_data.accdb";
OleDbCommand command = new OleDbCommand();
command.CommandText = "INSERT INTO Electric_Data (Asset_Id,Asset_Name,Emp_Id,Type_of_Asset,Actual_end_date) VALUES (?,?,?,?,?)";
command.Parameters.Add("@Asset_Id", OleDbType.VarChar, 20).Value = textBox1.Text;
command.Parameters.Add("@Asset_Name", OleDbType.Char, 20).Value = textBox2.Text;
command.Parameters.Add("@Emp_Id", OleDbType.VarChar, 20).Value = textBox3.Text;
command.Parameters.Add("@Type_of_Asset", OleDbType.VarChar, 20).Value = textBox4.Text;
command.Parameters.Add("@Actual_end_date", OleDbType.Date).Value = DateTime.Now;
command.ExecuteNonQuery();
conn.Open();
command.Connection = conn;
MessageBox.Show("Entry Registered Successfully.");
}
else if (radioButton1.Checked == false && radioButton2.Checked == false)
MessageBox.Show("Check Work_Start or Work_Complete option for successful Entry.");
}
}
}
答案 0 :(得分:1)
你已经把conn.Open()放错了地方。试试 - >
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\databses\electric_data.accdb";
OleDbCommand command = new OleDbCommand();
command.CommandText = "INSERT INTO Electric_Data (Asset_Id,Asset_Name,Emp_Id,Type_of_Asset,Actual_Start_date) VALUES (@Asset_Id,@Asset_Name,@Emp_Id,@Type_of_Asset,@Actual_end_date)";
command.Parameters.Add("@Asset_Id", OleDbType.VarChar, 20).Value = textBox1.Text;
command.Parameters.Add("@Asset_Name", OleDbType.Char, 20).Value = textBox2.Text;
command.Parameters.Add("@Emp_Id", OleDbType.VarChar, 20).Value = textBox3.Text;
command.Parameters.Add("@Type_of_Asset", OleDbType.VarChar, 20).Value = textBox4.Text;
command.Parameters.Add("@Actual_end_date", OleDbType.Date).Value = DateTime.Now;
// Open connection and assign to command
conn.Open();
command.Connection = conn;
// Execute non-query command
command.ExecuteNonQuery();
暂且不说:
您不需要在代码中复制conn字符串,列名等。做一个噩梦来维护和调试。
您需要添加try ... catch异常处理 - 如果DB由于某种原因没有打开怎么办。
编辑:command.CommandText也是其他学识渊博的人的错误。
答案 1 :(得分:0)
您的命令文本完全错误。使用您的参数名称更改问号:
command.CommandText = "INSERT INTO Electric_Data (Asset_Id,Asset_Name,Emp_Id,Type_of_Asset,Actual_Start_date) VALUES (@Asset_Id,@Asset_Name,@Emp_Id,@Type_of_Asset,@Actual_end_date)";
您在ExecuteNonQuery之后打开连接,首先打开连接,然后执行查询
conn.Open();
command.Connection = conn;
command.ExecuteNonQuery();
答案 2 :(得分:0)
尝试此插入查询。移除?,?,?,?
command.CommandText = "INSERT INTO Electric_Data (Asset_Id,Asset_Name,Emp_Id,Type_of_Asset,Actual_Start_date) VALUES (@Asset_Id,@Asset_Name,@Emp_Id,@Type_of_Asset,@Actual_end_date)";
检查表格列的数据类型