楽にインサートできます。(OLEDBに変更しました)
String ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\shn\source\repos\MAILDBSYSTEM\MailDB.accdb";
OleDbConnection conn;
OleDbCommand comm;
string sqlstr = "INSERT INTO T_USR (USR,TEL,ADDS) VALUES(@USR,@TEL,@ADDS)";
conn = new OleDbConnection(ConnectionString);
conn.Open();
try {
comm = new OleDbCommand(sqlstr, conn);
List sqlParameters = new List();
sqlParameters.AddSqlParameter(new OleDbParameter("@USR", OleDbType.VarChar)).Value = textBox1.Text;
sqlParameters.AddSqlParameter(new OleDbParameter("@TEL", OleDbType.VarChar)).Value = textBox2.Text;
sqlParameters.AddSqlParameter(new OleDbParameter("@ADDS", OleDbType.VarChar)).Value = textBox3.Text;
comm.Parameters.AddParams(sqlParameters);
comm.ExecuteNonQuery();
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
return;
}
finally {
conn.Close();
conn.Dispose();
}
///////////////
public static class SqlParameterExtension {
public static OleDbParameter AddSqlParameter(this List list, OleDbParameter parameter) {
list.Add(parameter);
return parameter;
}
public static void AddParams(this OleDbParameterCollection collection, List list) {
foreach (OleDbParameter parameter in list) {
collection.Add(parameter);
}
}
}
戻る