我有一个可视化的 C# 项目,当我按下一个按钮时,我试图在 MS Access 数据库中插入数据
I have a visual C# project and I'm trying to insert data in a MS Access Database when I press a button
private void btnSave_Click(object sender, EventArgs e)
{
string str = "insert into Checklist_Master values('"+comboBox2.Text+"','"+txtaurno.Text+"','"+comboBox1.Text+"','"+txtdeduction.Text+"','"+txtceckpoint.Text+"','"+mdlconnection.user_name+"')";
int dd = mdlconnection.excuteQuery(str);
//MessageBox.Show(str);
if (dd > 0)
{
MessageBox.Show("Data Saved Successfully..!!!");
reset();
}
}
连接字符串是:
public static void getconn()
{
con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Learning\Quality_Rating_Tool.accdb");
con.open();
}
这是Excutequery方法:
public static int excuteQuery(string q)
{
int d = 0;
try
{
OleDbCommand cmd = new OleDbCommand(q, con);
d = cmd.ExecuteNonQuery();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return d;
}
请帮忙解释为什么数据没有插入table
首先,您永远不会通过调用 getconn()
打开连接。连接从未打开。
其次,您必须为 INSERT INTO Checklist_Master (Col1, Col2...) VALUES ()
指定列
最后,尝试按以下方式设置它:
using(OleDbConnection oledb = new OleDbConnection(<Your Connection String>))
{
oledb.Open();
using (OleDbCommand oleComm = new OleDbCommand(<sqlString>,oledb)) //the sql passed in as a parameter as your method suggests
{
oleComm .CommandType = CommandType.Text;
oleComm.ExecuteNonQuery();
}
}
您可以根据需要添加try catch
。清除和关闭所有实例化对象的好习惯。您也可以尝试将 INSERT
语句中的变量替换为参数
INSERT INTO Checklist_Master (<Col>,<Col2>,...) VALUES (@Param1,@Param2);
oleComm.Parameters.AddWithValue("@Param1", comboBox2.Text); //Conversion if required
private void btnSave_Click(object sender, EventArgs e)
{
string str = "insert into Checklist_Master values('"+comboBox2.Text+"','"+txtaurno.Text+"','"+comboBox1.Text+"','"+txtdeduction.Text+"','"+txtceckpoint.Text+"','"+mdlconnection.user_name+"')";
int dd = mdlconnection.excuteQuery(str);
//MessageBox.Show(str);
if (dd > 0)
{
MessageBox.Show("Data Saved Successfully..!!!");
reset();
}
}
连接字符串是:
public static void getconn()
{
con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Learning\Quality_Rating_Tool.accdb");
con.open();
}
这是Excutequery方法:
public static int excuteQuery(string q)
{
int d = 0;
try
{
OleDbCommand cmd = new OleDbCommand(q, con);
d = cmd.ExecuteNonQuery();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return d;
}
请帮忙解释为什么数据没有插入table
首先,您永远不会通过调用 getconn()
打开连接。连接从未打开。
其次,您必须为 INSERT INTO Checklist_Master (Col1, Col2...) VALUES ()
最后,尝试按以下方式设置它:
using(OleDbConnection oledb = new OleDbConnection(<Your Connection String>))
{
oledb.Open();
using (OleDbCommand oleComm = new OleDbCommand(<sqlString>,oledb)) //the sql passed in as a parameter as your method suggests
{
oleComm .CommandType = CommandType.Text;
oleComm.ExecuteNonQuery();
}
}
您可以根据需要添加try catch
。清除和关闭所有实例化对象的好习惯。您也可以尝试将 INSERT
语句中的变量替换为参数
INSERT INTO Checklist_Master (<Col>,<Col2>,...) VALUES (@Param1,@Param2);
oleComm.Parameters.AddWithValue("@Param1", comboBox2.Text); //Conversion if required