0

我在下面的代码中收到语法错误。"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);
}
4

3 回答 3

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 的额外好处,尽管您的代码并不完全安全,因为表名附加了字符串连接

于 2013-05-20T08:43:07.627 回答
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;

希望对你有效。

于 2013-05-20T08:33:18.070 回答
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 !: ");
于 2018-07-18T09:26:10.403 回答