[英]C# insert datetime to DBF file
我在下面的代碼中出現語法錯誤。 根據我的測試,它來自字段"tax_month"
,它是一個DateTime
。 我不知道如何以DateTime格式插入值。
該字段的模式:
ColumnName: tax_month,
ColumnOrdinal: 0,
ColumnSize: 6,
NumericPrecision: 10,
NumericScale: 0,
DataType: System.DateTime,
ProviderType: 23
這是我的C#代碼:
string path = @"C:\Purchases\DATA\";
string fileName = "purchase.dbf";
DateTime tax_month = DateTime.FromOADate(41305);
private void button1_Click(object sender, EventArgs e)
{
OdbcConnection dbfConn = new OdbcConnection();
dbfConn.ConnectionString = @"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;Exclusive=No;Collate=Machine;NULL=NO;DELETED=YES;BACKGROUNDFETCH=NO;SourceDB=" + path;
dbfConn.Open();
OdbcCommand oCmd = dbfConn.CreateCommand(); // needed to query data
oCmd.CommandText = "INSERT INTO " + fileName + " ('tax_month') VALUES ("+tax_month+")";
int inserted = oCmd.ExecuteNonQuery();
dbfConn.Close();
MessageBox.Show("Number of Rows inserted:"+inserted);
}
嘗試使用參數化查詢,並讓工作將您的數據時間解析為NET Framework代碼
using(OdbcConnection dbfConn = new OdbcConnection())
{
dbfConn.ConnectionString = @"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;Exclusive=No;Collate=Machine;NULL=NO;DELETED=YES;BACKGROUNDFETCH=NO;SourceDB=" + path;
dbfConn.Open();
OdbcCommand oCmd = dbfConn.CreateCommand(); // needed to query data
oCmd.CommandText = "INSERT INTO " + fileName + " +
"(tax_month,seq_no,tin,registered_name,last_name,first_name,middle_name,address1," +
"address2,gpurchase,gtpurchase,gepurchase,gzpurchase,gtservpurchase,gtcappurchase," +
"gtothpurchase,tinputtax,tax_rate) VALUES (" +
"?, 3, '222333445','TEST COMPANY','','','','DI MAKITA STREET','MANDALUYONG 1602', " +
"52107.14, 49107.14, 1000, 2000, 3000, 4000, 42107.14, 5892.86, 12)"
oCmd.Parameters.Add("@p1", OdbcType.DateTime).Value = tax_month;
int inserted = oCmd.ExecuteNonQuery();
}
另外,列名的寫法不要用單引號引起來。 使用參數查詢還具有避免Sql注入的額外好處,盡管您的代碼並不完全安全,因為表名附加了字符串連接
在insert
查詢中使用'"+tax_month+"'
代替"+tax_month+"
。 嘗試像這樣更改您的插入查詢
oCmd.CommandText = "INSERT INTO " + fileName + " ('tax_month','seq_no','tin','registered_name','last_name','first_name','middle_name','address1','address2','gpurchase','gtpurchase','gepurchase','gzpurchase','gtservpurchase','gtcappurchase','gtothpurchase','tinputtax','tax_rate') VALUES ('"+tax_month.ToString("yyyy-MM-dd")+"',3,'222333445','TEST COMPANY','','','','DI MAKITA STREET','MANDALUYONG 1602', 52107.14, 49107.14, 1000, 2000, 3000, 4000, 42107.14, 5892.86, 12)";
或者您也可以使用parameterized query
oCmd.CommandText = "INSERT INTO " + fileName + " +
"(tax_month,seq_no,tin,registered_name,last_name,first_name,middle_name,address1," +
"address2,gpurchase,gtpurchase,gepurchase,gzpurchase,gtservpurchase,gtcappurchase," +
"gtothpurchase,tinputtax,tax_rate) VALUES (" +
"?, 3, '222333445','TEST COMPANY','','','','DI MAKITA STREET','MANDALUYONG 1602', " +
"52107.14, 49107.14, 1000, 2000, 3000, 4000, 42107.14, 5892.86, 12)"
oCmd.Parameters.Add("@p1", OdbcType.DateTime).Value = tax_month;
希望對你有效。
sql = "insert into ts8heat (HDATE,HEAT_NO[,STGR,WIDTH,C,SI,MN,P,S,AL); values (@HDATE[,@HEAT_NO,@STGR,@WIDTH,@C,@SI,@MN,@P,@S,@AL])";//select scope_identity()";
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.Parameters.AddWithValue("@HDATE", D_PICER1.Text.ToString());
cmd.Parameters.AddWithValue("@HEAT_NO", txt_heats.Text);
cmd.Parameters.AddWithValue("@STGR", txt_stgr.Text);
cmd.Parameters.AddWithValue("@WIDTH", txt_width.Text);
cmd.Parameters.AddWithValue("@c", txt_C.Text);
cmd.Parameters.AddWithValue("@si", txt_Si.Text);
cmd.Parameters.AddWithValue("@MN", txt_Mn.Text);
cmd.Parameters.AddWithValue("@P", txt_P.Text);
cmd.Parameters.AddWithValue("@s", txt_S.Text);
cmd.Parameters.AddWithValue("@AL", txt_al.Text);
DataTable dt = new DataTable();
da = new OleDbDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Refresh();
MessageBox.Show(" Add SaccessFuly !: ");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.