简体   繁体   中英

c# and ms-access database inserting

I want to save data by save button from :

idbox (textbox) - empbox (combobox) - jobbox (combobox) - unitbox (combobox) -

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

investdate (datepicker) - investresult (textbox)

into the table investinside same order :

id - emp - unit - job - dest - detective - state - investdate - 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.

If your tables have primarykeys you should take a look at CommandBuilder feature of ADO.Net.

If so adding data to you table is accomplished by a simple DataTable.Rows.Add and a 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"

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. Also I make spelling errors all the time. SQL will not forgive those - in Entity Framework you cannot mess up...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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