简体   繁体   English

c#和ms-access数据库插入

[英]c# and ms-access database inserting

I want to save data by save button from : 我想通过保存按钮保存数据:

idbox (textbox) - empbox (combobox) - jobbox (combobox) - unitbox (combobox) - idbox(文本框)-empbox(组合框)-jobbox(组合框)-unitbox(组合框)-

destbox (combobox) - detectivebox (combobox) - statebox (combobox) - destbox(combobox)-Detectivebox(combobox)-statebox(combobox)-

investdate (datepicker) - investresult (textbox) investdate(日期选择器)-investresult(文本框)

into the table investinside same order : 到表investinside相同的顺序:

id - emp - unit - job - dest - detective - state - investdate - investresult id-emp-单位-职位-目标-侦探-状态-投资日期-investresult

i tried this one and had no errors but nothing was saved in the table when i check .... 我尝试了这个并且没有错误,但是当我检查...时,表中什么也没有保存。

can i know what is the reason for that ? 我能知道是什么原因吗?

here's the code i tried 这是我尝试的代码

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();
    }
}

} }

There are many ways you can make ADO.Net easier for you. 您可以通过多种方法使ADO.Net更加容易。

If your tables have primarykeys you should take a look at CommandBuilder feature of ADO.Net. 如果表具有主键,则应查看ADO.Net的CommandBuilder功能。

If so adding data to you table is accomplished by a simple DataTable.Rows.Add and a DataAdapter.Update 如果是这样,则通过简单的DataTable.Rows.Add和DataAdapter.Update将数据添加到表中

If you want to do it manually you can try to go manually all the way. 如果您想手动执行此操作,则可以尝试全部手动操作。 Instead of: 代替:



    "INSERT INTO investinside([id],[emp],[job],[unit],[dest],[detective],[state],[investdate],[investresult])VALUES(@id,@emp,@job,@unit,@dest,@detective,@state,@investdate,@investresult)"

Try 尝试



    string.Format("INSERT INTO investinside([id],[emp],[job],[unit],[dest],[detective],[state],[investdate],[investresult])VALUES('{0}','{1}',....)", idbox.Text, empbox.Text,...)

It looks like to me you are having a local DB connection. 在我看来,您正在建立本地数据库连接。 This is how I usually connect... "server=USUALLY_YOYR_PC_NAME_CAPPITAL_LETTERS; database=database_name; integrated security=yes" 这就是我通常的连接方式……“服务器= USUALLY_YOYR_PC_NAME_CAPPITAL_LETTERS;数据库=数据库名称;集成安全性=是”

the last part integrated security is important don't omit it. 最后一部分,集成安全性很重要,请不要忽略它。

Advice: Check out ORM - Entity Frame Work tutorial from MSDN I much rather work with Objects than SQL statements. 建议:查阅MSDN上的 ORM- 实体框架工作教程。我更喜欢使用对象而不是SQL语句。 Also I make spelling errors all the time. 我也经常犯拼写错误。 SQL will not forgive those - in Entity Framework you cannot mess up... SQL不会原谅那些-在Entity Framework中,您不能搞砸...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM