winform界面上有两个textbox分别用于输入工程名称,工程编号,四个comboBox用于输入工程级别, 两个dateTimePicker输入生产日期,在my_db中已建了一个数据表”新建项目“,希望通过界面上的创建button,将textbox这些空间中 的内容存入这个数据表中对应的内容下面
 private void button1_Click(object sender, EventArgs e)
        {  OleDbConnection Conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\\my_db.mdb");             Conn.Open();
            string sql;
            OleDbCommand cmd = new OleDbCommand("", Conn);
            if (newtextBox1.Text != "")
            {
                sql = "select * from 新建项目 where 项目编号='" + newtextBox1.Text.Trim() + "'";
                cmd.CommandText = sql;
                if (null == cmd.ExecuteScalar())
                {
                    sql = "insert into 新建项目 values ('" + newtextBox1.Text.Trim() + "','" + 项目名称textBox2.Text.Trim() + "','" + comboBox1.Text.ToString() + "','" + comboBox2.Text.ToString() + "','" + comboBox3.Text.ToString() + "','" + comboBox4.Text.ToString() + "','" + dateTimePicker1.Value.ToString()+ "','" + dateTimePicker2.Value.ToString()+ "')";
                    cmd.CommandText = sql;
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("项目创建成功");
                }
                else
                    MessageBox.Show("已有重名角色存在", "提示");
            }
            else
                MessageBox.Show("角色名称不能为空");
            Conn.Close();}
最后没有达到预期效果,
未处理oledbexception

解决方案 »

  1.   

    Winform连接access数据库操作 (1)access设计数据库是不能用关键字做为字段名, (2)添加数据时CommandBuilder对DataAdapter进行封装才能执行成功!! 代码如下,两种方法对数据库写进录入: 
    using System; 
    using System.Data.OleDb; 
    using System.Data; 
    namespace zzzDemo 
    {
     /// /// ConnDAO 的摘要说明。 /// 
    public class ConnDAO { 
    //数据连接 private OleDbConnection oConn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='D:/OrderSystem.mdb'"); 
    private OleDbDataAdapter oda = null; 
    private OleDbCommandBuilder cb = null; 
    //private DataTable dt = new DataTable("Dictionary1"); 
    private DataSet ds = new DataSet(); 
    public ConnDAO() 

    oConn.Open(); 
    oda = new OleDbDataAdapter("select * from Dictionary1",oConn); 
    oda.Fill(ds,"dd"); 
    oConn.Close(); 

    //添加数据方法 
    public bool addDictionary(Dictionary dy) 
    { //第一种解决方法 /*
    oda.InsertCommand = new OleDbCommand(); 
    oda.InsertCommand.CommandText = "insert into Dictionary1(Category,CValue) values('"+dy.Category+"','"+dy.Value+"')";
     oda.InsertCommand.Connection = oConn; oConn.Open(); 
    int i = oda.InsertCommand.ExecuteNonQuery(); 
    oConn.Close(); if(i>0) return true; 
    else return false;*/ 
    //第二种解决方法 
    cb = new OleDbCommandBuilder(oda); 
    //一定要用CommandBuilder进行封装 DataRow dr = ds.Tables["dd"].NewRow(); dr["Category"] = dy.Category; dr["CValue"] = dy.Value; ds.Tables["dd"].Rows.Add(dr); ds.Tables["dd"].GetChanges(); oConn.Open(); int i = oda.Update(ds.Tables["dd"]); oConn.Close(); if(i>0) return true; else return false; } //返回DataTable表 public DataTable GetDictionary() { return ds.Tables["dd"]; } public static void Main(string[]args) { //测试 ConnDAO dao = new ConnDAO(); Dictionary dd = new Dictionary(); dd.Category = "ccc"; dd.Value = "555"; bool falg = dao.addDictionary(dd); Console.WriteLine(falg); } } public class Dictionary { private string _Category; private string _Value; public string Category { get{return _Category;} set{_Category = value;} } public string Value { get{return _Value;} set{_Value = value;} } } }