![](/img/trans.png)
[英]How to access specific rows and insert values into specific columns of a table?
[英]How to insert into specific columns of a database
問題是當我嘗試插入時出現異常錯誤,表明外部數據庫中的數據不匹配
我有一個帶有預訂 ID 的客戶預訂表...pk 作為自動編號,客戶....fk 和日期作為日期時間預訂 ID 是自動遞增的,所以我不需要插入任何東西
我想要實現的是插入到 customer_id 和日期中,僅此而已,但我不斷得到數據不匹配和查詢值的數量與目標字段不同
那么放置這個的正確方法是什么
using (OleDbConnection myCon = new OleDbConnection(ConfigurationManager.ConnectionStrings["DbConn"].ToString())) {
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into customer_booking ([customer_id],[date_in]) values (?,?)";
cmd.Parameters.AddWithValue("@customer_id", txtusername.Text);
cmd.Parameters.AddWithValue("@date_in", dtdate_in);
cmd.Connection = myCon;
myCon.Open();
cmd.ExecuteNonQuery();
System.Windows.Forms.MessageBox.Show("your booking was successful ", "Caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
使用cmd.Parameters.add('...')
方法如下:
using (OleDbConnection myCon = new OleDbConnection(ConfigurationManager.ConnectionStrings["DbConn"].ToString())) {
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into customer_booking ([customer_id],[date_in]) values (@customer_id,@date_in)";
///* Set your column dataType*/
cmd.Parameters.Add("@customer_id", SqlDbType.VarChar , 50).Value = txtusername.Text;
cmd.Parameters.Add("@date_in", SqlDbType.DateTime).Value = dtdate_in;
cmd.Connection = myCon;
myCon.Open();
cmd.ExecuteNonQuery();
System.Windows.Forms.MessageBox.Show("your booking was successful ", "Caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.