C#和MS-Access数据库插入
问题描述:
我想节省拯救按键数据:C#和MS-Access数据库插入
idbox(文本) - empbox(组合框) - jobbox(组合框) - unitbox (组合框) -
destbox(组合框) - detectivebox(组合框) - statebox(组合框) -
investdate(日期选择器) - investresult(文本框)
进表investinside相同的顺序:
ID - 员工 - 单位 - 工作 - DEST - 侦探 - 状态 - investdate - investresult
我想这一个并没有错误,但什么也没有保存在表当我检查....
我可以知道是什么原因呢?
这里的代码我试图
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace beta_2
{
public partial class Dentry_main : Form
{
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=beta2.mdb;");
OleDbDataAdapter da;
DataSet ds = new DataSet();
OleDbCommand com = new OleDbCommand();
string sql;
public Dentry_main()
{
InitializeComponent();
}
private void Dentry_main_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'beta2DataSet.stateinside' table. You can move, or remove it, as needed.
this.stateinsideTableAdapter.Fill(this.beta2DataSet.stateinside);
// TODO: This line of code loads data into the 'beta2DataSet.detectivestbl' table. You can move, or remove it, as needed.
this.detectivestblTableAdapter.Fill(this.beta2DataSet.detectivestbl);
// TODO: This line of code loads data into the 'beta2DataSet.departments' table. You can move, or remove it, as needed.
this.departmentsTableAdapter.Fill(this.beta2DataSet.departments);
// TODO: This line of code loads data into the 'beta2DataSet.units' table. You can move, or remove it, as needed.
this.unitsTableAdapter.Fill(this.beta2DataSet.units);
// TODO: This line of code loads data into the 'beta2DataSet.employees' table. You can move, or remove it, as needed.
this.employeesTableAdapter.Fill(this.beta2DataSet.employees);
}
private void button1_Click(object sender, EventArgs e)
{
conn.Open();
com.Connection = conn;
sql = "INSERT INTO investinside([id],[emp],[job],[unit],[dest],[detective],[state],[investdate],[investresult])VALUES(@id,@emp,@job,@unit,@dest,@detective,@state,@investdate,@investresult)";
com.CommandText = sql;
com.Parameters.Clear();
com.Parameters.AddWithValue("@id", idbox.Text);
com.Parameters.AddWithValue("@emp", empbox.Text);
com.Parameters.AddWithValue("@job", jobbox.Text);
com.Parameters.AddWithValue("@unit", unitbox.Text);
com.Parameters.AddWithValue("@dest", destbox.Text);
com.Parameters.AddWithValue("@detective", detectivebox.Text);
com.Parameters.AddWithValue("@state", statebox.Text);
com.Parameters.AddWithValue("@investdate", investdatebox.Text);
com.Parameters.AddWithValue("@investresult", investresultbox.Text);
com.ExecuteNonQuery();
MessageBox.Show("success");
conn.Close();
}
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
答
它看起来像我你有一个本地数据库连接。这是我通常如何连接... “server = USUALLY_YOYR_PC_NAME_CAPPITAL_LETTERS; database = database_name; integrated security = yes”
最后一部分集成安全性很重要,不要忽略它。
建议: 退房ORM - Entity Frame Work tutorial from MSDN我更喜欢使用对象而不是SQL语句。我也一直在拼写错误。 SQL不会原谅那些 - 在实体框架中你不能搞乱......
答
有很多方法可以让ADO.Net更容易为你使用。
如果您的表具有主键,则应该查看ADO.Net的CommandBuilder功能。
如果是将数据添加到您的表是由一个简单的DataTable.Rows.Add和DataAdapter.Update
如果你想要做手工来完成,你可以尝试手动一路走下去。 相反的:
"INSERT INTO investinside([id],[emp],[job],[unit],[dest],[detective],[state],[investdate],[investresult])VALUES(@id,@emp,@job,@unit,@dest,@detective,@state,@investdate,@investresult)"
尝试
string.Format("INSERT INTO investinside([id],[emp],[job],[unit],[dest],[detective],[state],[investdate],[investresult])VALUES('{0}','{1}',....)", idbox.Text, empbox.Text,...)
你确定你没有将数据库复制到项目文件夹,当你创造了这个项目? Visual Studio默认会这样做。检查副本。 – Fionnuala 2013-03-21 19:09:59
尝试通过'int result = com.ExecuteNonQuery();' – 2013-03-21 19:10:51
来检查结果如何设置数据库文件的属性。它是一种资源吗?它是否设置为“复制,如果更新”? – plast1K 2013-03-21 19:12:55