Visual Stadio 2017 C# テーブルレコードの新規追加

データベースへの直接書き込みする方法

※DataSetを表示に使用するとこの処理だけでは内容が変わらないので別処理が必要かな?

 

//OlDb接続によるテキストボックスからの直接書き込み
string textValue1 = comboBox1.Text;
string textValue2 = textBox1.Text;
string textValue3 = textBox3.Text;
string textValue4 = textBox4.Text;
string textValue5 = textBox5.Text;
string textValue6 = textBox6.Text;
string textValue7 = textBox7.Text;
var cn = new OleDbConnection();

//接続文字列を設定して接続する
cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;"
+ "Data Source=C:\\Users\\User01\\Source\\Repos\\社内用テスト\\社内用テスト\\TestDB.accdb;";
OleDbCommand oleCmd = new OleDbCommand();
int iRet;

try
{
cn.Open();

oleCmd.Connection = cn;
oleCmd.CommandText = "INSERT INTO 出勤率 ([氏名],[出勤日],[出勤時間],[退勤時間],[休憩時間],[出退勤],[備考])" +
" VALUES ('" + textValue1 + "','" + textValue2 + "','" + textValue3 + "','" + textValue4 + "','" + textValue5 + "','" + textValue6 + "','" + textValue7 + "')";


iRet = oleCmd.ExecuteNonQuery();

MessageBox.Show("接続しました。", "通知");
cn.Close();

}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "通知");
}