数据未保存在 MS Access 数据库中
我有一个可视化 C# 项目,当我按下按钮时,我尝试在 MS Access 数据库中插入数据。代码如下:
private void button1_Click(object sender, EventArgs e)
{
try
{
OleDbDataAdapter adapter=new OleDbDataAdapter();
adapter.InsertCommand = new OleDbCommand();
adapter.InsertCommand.CommandText =
"insert into Candidati values ('" + maskedTextBox1.Text.Trim() + "','" + textBox1.Text.Trim() + "', '" + textBox2.Text.Trim() + "', '" + textBox3.Text.Trim() + "','" + Convert.ToDouble(maskedTextBox2.Text) + "','" + Convert.ToDouble(maskedTextBox3.Text) + "')";
con.Open();
adapter.InsertCommand.Connection = con;
adapter.InsertCommand.ExecuteNonQuery();
con.Close();
MessageBox.Show("Inregistrare adaugata cu succes!");
maskedTextBox1.Text = null;
maskedTextBox2.Text = null;
maskedTextBox3.Text = null;
textBox1.Text = null;
textBox2.Text = null;
textBox3.Text = null;
maskedTextBox1.Focus();
}
catch (AdmitereException exc)
{
MessageBox.Show("A aparut o exceptie: "+exc.Message, "Eroare!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
连接字符串为:
private static string connectionString;
OleDbConnection con;
public AddCandidati()
{
connectionString = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=Admitere.mdb";
con = new OleDbConnection(connectionString);
InitializeComponent();
}
其中 AddCandidati
是表单。 数据没有保存在数据库中,为什么?我的项目文件夹中有 .mdb 文件。我做错了什么?当我按下按钮时,没有出现任何异常。
I have a visual C# project and I'm trying to insert data in a MS Access Database when I press a button. Here is the code:
private void button1_Click(object sender, EventArgs e)
{
try
{
OleDbDataAdapter adapter=new OleDbDataAdapter();
adapter.InsertCommand = new OleDbCommand();
adapter.InsertCommand.CommandText =
"insert into Candidati values ('" + maskedTextBox1.Text.Trim() + "','" + textBox1.Text.Trim() + "', '" + textBox2.Text.Trim() + "', '" + textBox3.Text.Trim() + "','" + Convert.ToDouble(maskedTextBox2.Text) + "','" + Convert.ToDouble(maskedTextBox3.Text) + "')";
con.Open();
adapter.InsertCommand.Connection = con;
adapter.InsertCommand.ExecuteNonQuery();
con.Close();
MessageBox.Show("Inregistrare adaugata cu succes!");
maskedTextBox1.Text = null;
maskedTextBox2.Text = null;
maskedTextBox3.Text = null;
textBox1.Text = null;
textBox2.Text = null;
textBox3.Text = null;
maskedTextBox1.Focus();
}
catch (AdmitereException exc)
{
MessageBox.Show("A aparut o exceptie: "+exc.Message, "Eroare!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
The connection string is:
private static string connectionString;
OleDbConnection con;
public AddCandidati()
{
connectionString = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=Admitere.mdb";
con = new OleDbConnection(connectionString);
InitializeComponent();
}
Where AddCandidati
is the form.
The data is not saved in the database, why? I have the .mdb file in the project folder. What I'm doing wrong? I did not got any exception when I pressed the button.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的插入命令是错误的。您必须首先指定列的名称,然后为每个列提供值。
Your insert command is wrong. You have to specify the names of the columns first, then you give the values for each of those columns.