0

我想通过保存按钮从以下位置保存数据:

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

destbox(组合框)-detectivebox(组合框)-statebox(组合框)-

投资日期(日期选择器)-投资结果(文本框)

进入表投资相同的顺序:

id - emp - unit - job - dest - 侦探 - state -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();
    }
}

}

4

2 回答 2

0

有很多方法可以让您更轻松地使用 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,...)

于 2013-03-22T11:15:20.723 回答
-1

在我看来,您有一个本地数据库连接。这就是我通常连接的方式...“服务器=USUALLY_YOYR_PC_NAME_CAPPITAL_LETTERS;数据库=数据库名称;集成安全=是”

最后一部分集成安全性很重要,不要忽略它。

建议:查看MSDN 的 ORM - Entity Frame Work 教程我更喜欢使用对象而不是 SQL 语句。我也经常犯拼写错误。SQL 不会原谅那些 - 在实体框架中你不能搞砸......

于 2013-03-21T19:17:22.027 回答