在DBF文件中插入日期时间
本文关键字:日期 时间 插入 DBF 文件 | 更新日期: 2023-09-27 18:14:38
我得到一个语法错误在下面的代码。从我的测试来看,它来自场"tax_month"
,它是一个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 !: ");