我在下面的代码中遇到语法错误。根据我的测试,它来自"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);
}
答案 0 :(得分:1)
尝试使用参数化查询,让工作将您的数据时解析为.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 Injection,尽管你的代码不是完全安全的,因为表名附加了字符串连接
答案 1 :(得分:0)
在'"+tax_month+"'
查询中使用"+tax_month+"
代替insert
。尝试更改插入查询,如
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;
希望它适合你。
答案 2 :(得分:0)
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 !: ");