私は最初のプログラムであるデータベース プログラムを作成していますが、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";
}
}