2013-05-20 74 views
0

我在下面的代碼中收到語法錯誤。從我的測試,它來自"tax_month"這是一個DateTime字段。我無法弄清楚如何以DateTime格式插入一個值。C#將日期時間插入到DBF文件

模式的那場:

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

嘗試使用參數化查詢,讓工作,你DATATIME解析到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注入的額外好處,儘管您的代碼並不完全安全,因爲表名添加了字符串連接

+0

非常感謝你,它的工作! – mhar

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; 

希望它爲你工作。

+0

更改爲您的建議之後。 錯誤[22018] [Microsoft] [ODBC Visual FoxPro驅動程序]數據類型不匹配。 – mhar

+0

什麼是數據庫中的'tax_month'列的數據類型 – Rahul

+0

DataType是DateTime – mhar