1

我正在编写我的第一个程序,一个数据库程序,并且我坚持将新的信息行插入到我的 excel 数据库文件中。问题是每行数据在 A 列中都有一个日期单元格。我希望能够在 dateTimePicker 中选择一个日期,通过文本框输入我的其余数据(姓名、工作编号 ...等),然后单击按钮将所有数据插入新行。我可以使用 Interop 来做到这一点,但我不喜欢它,因为它会打开 excel 并且需要很长时间。我的代码目前适用于所有文本框输入,但不适用于日期。我的 excel 文件中的日期列是“日期”格式。

所以我的问题是,我可以使用 OLE DB 将 dateTimePicker 值插入日期格式的 Excel 单元格吗?

这是我的代码,非常感谢您的帮助。

private void button1_Click(object sender, EventArgs e)
    {
        if (label60.Text == "new")
        {
            try
            {
                System.Data.OleDb.OleDbConnection MyConnection;
                System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
                string sql = null;
                MyConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Declan\\Documents\\Declan\\output.xlsx;Extended Properties=\"Excel 8.0;HDR=Yes;\" ");
                MyConnection.Open();
                myCommand.Connection = MyConnection;

                DateTime date1 = dateTimePicker1.Value;


                string post1 = textBox3.Text;
                string type1 = textBox4.Text;
                string source1 = textBox5.Text;
                string income1 = textBox6.Text;
                string prof1 = textBox7.Text;
                string jobnum1 = textBox8.Text;

                sql = "Insert into [Database$] (date,postcode,type,source,income,profession,customerid) values('" + date1 + "','" + post1 + "','" + type1 + "','" + source1 + "','" + income1 + "','" + prof1 + "','" + jobnum1 + "')";
                myCommand.CommandText = sql;
                myCommand.ExecuteNonQuery();
                MyConnection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            label60.Text = "edit";
        }
    }
4

1 回答 1

0

对于那些感兴趣的人,我已将代码更改为使用 oledb 参数。不知道这是如何解决的,但看起来需要 excel 日期列来获取格式为 dd/MM/yyyy 的字符串

      private void button1_Click(object sender, EventArgs e)
    {
        if (label60.Text == "new")
        {
            try
            {
                System.Data.OleDb.OleDbConnection MyConnection;
                System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
                MyConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Declan\\Documents\\Declan\\output.xlsx;Extended Properties=\"Excel 8.0;HDR=Yes;\" ");
                MyConnection.Open();
                myCommand.Connection = MyConnection;

                DateTime date1 = dateTimePicker1.Value;
                string date2 = date1.ToString("dd/MM/yyyy");


                myCommand.CommandText = "Insert into [Database$] ([date],postcode,type,source,income,profession,phonenumber) values(@date, @post, @type,@source,@income,@prof,@jobnum)";
                myCommand.Parameters.AddWithValue("@date",date2);
                myCommand.Parameters.AddWithValue("@post", textBox3.Text);
                myCommand.Parameters.AddWithValue("@type", textBox4.Text);
                myCommand.Parameters.AddWithValue("@source", textBox5.Text);
                myCommand.Parameters.AddWithValue("@income", textBox6.Text);
                myCommand.Parameters.AddWithValue("@prof", textBox7.Text);
                myCommand.Parameters.AddWithValue("@jobnum", textBox8.Text);
                myCommand.ExecuteNonQuery();
                MyConnection.Close();


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

         label60.Text = "edit";
        }
    }
于 2013-11-04T03:39:09.573 回答