문제

I'm getting a Syntax Error in the below code. From my testing, it's coming from the field "tax_month" which is a DateTime. I can't figure out how to insert a value in DateTime format.

Schema of that field:

ColumnName:       tax_month,
ColumnOrdinal:    0,
ColumnSize:       6,
NumericPrecision: 10,
NumericScale:     0,
DataType:         System.DateTime,
ProviderType:     23

This is my C# code:

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);
}
도움이 되었습니까?

해결책

Try to use a parameterized query and let the work to parse your datatime to the NET Framework code

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();
}

Also, the column names should be written without single quotes around. The use of a parameter query has also the added benefit to avoid Sql Injection, though your code is not completely safe because the table name appended with the string concatenation

다른 팁

use '"+tax_month+"' instead of "+tax_month+" into your insert query. Try to change your insert query like

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)";

Or you may use parameterized query as well like

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;

Hope it works for you.

 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 ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top