【发布时间】:2014-02-03 11:14:36
【问题描述】:
我在 C# 中有以下代码,用于与 DB 的接口。我在界面上的 TextBoxes 中引入值。没有错误,但是执行时,数据不会存储在使用 Microsoft Access 2010 创建的数据库中。我将在此处给出完整的代码。感谢您的回答!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\lumi\\Desktop\\Test_DataB.accdb");
OleDbDataAdapter ad = new OleDbDataAdapter();
DataSet ds = new DataSet();
public Form1()
{
InitializeComponent();
}
private void indexBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.indexBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.test_DataBDataSet);
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'test_DataBDataSet.Index' table. You can move, or remove it, as needed.
this.indexTableAdapter.Fill(this.test_DataBDataSet.Index);
}
private void button1_Click(object sender, EventArgs e)
{
try
{ con.Open();
ad.InsertCommand = new OleDbCommand("Insert @Birth_month, @Firstname, @Surname, @ID", con);
ad.InsertCommand.Parameters.Add("@Birth_month", OleDbType.Numeric).Value = int.Parse(textBox1.Text);
ad.InsertCommand.Parameters.Add("@Firstname", OleDbType.VarChar).Value = textBox2.Text.ToString();
ad.InsertCommand.Parameters.Add("@Surname", OleDbType.VarChar).Value = textBox3.Text.ToString();
ad.InsertCommand.Parameters.Add("@ID", OleDbType.Numeric).Value = int.Parse(textBox4.Text);
ad.InsertCommand.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
【问题讨论】:
-
什么..?您的 OleDbCommand 看起来不是有效的 sql 语句。 o.O
-
我翻译了,对不起!
标签: c# database-connection ms-access-2010 sql-insert oledbcommand