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 パラメータを使用するようにコードを変更しました。これがどのように修正されたのかわかりませんが、dd/MM/yyyy 形式の文字列を取得するために必要な 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();
                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 に答える