2011-06-27 165 views
1

我需要一些幫助,在VS2008中使用C#爲Access 2007數據庫創建插入語句。當前的代碼給了我「INSERT INTO語句中的錯誤42000:語法錯誤。」我不知道什麼是錯的。我基於今年早些時候使用的一個工作代碼編寫了這段代碼,該代碼連接到一個ms訪問表並將數據庫信息傳輸到一個oracle數據庫中。使用C#VS2008插入MS Access 2007 ODBC

當前代碼

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Diagnostics; 
using System.Linq; 
using System.Text; 
using System.Data.OracleClient; 
using System.Data.SqlClient; 
using System.IO; 
using System.Data.Odbc; 
class MainClass 
{ 
static void Main(string[] args) 
{ 
    string connectionString = "Dsn=Northwind 2007"; 
    string SQL = "SELECT * FROM Orders"; 
    string sqlins = ""; 
    OdbcConnection conn = new OdbcConnection(connectionString); 

    OdbcCommand cmd = new OdbcCommand(SQL); 
    cmd.Connection = conn; 
    OdbcCommand cmdnon=new OdbcCommand(sqlins,conn); 
    try 
     { 

     conn.Open(); 

     OdbcDataReader reader = cmd.ExecuteReader(); 
     while (reader.Read()) 
     { 
      Console.Write("OrderID:" + reader.GetInt32(0).ToString()); 
      Console.Write(" ,"); 
      Console.WriteLine("Customer:" + reader.GetString(1).ToString()); 
      cmdnon.CommandText = "INSERT Commonstation (S1Flow,S2Flow,S3Flow,S4Flow) VALUES (9999,999,999,999)"; 
      int rowsAffected = cmdnon.ExecuteNonQuery(); 
      Console.WriteLine(rowsAffected); 
     } 
    } 
    catch (Exception ex) 
    { 
     Console.WriteLine(ex.InnerException.ToString()); 
    } 
    finally 
    { 
     reader.Close(); 
     conn.Close(); 
    } 
} 
} 

工作代碼

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Diagnostics; 
using System.Linq; 
using System.Text; 
using System.Data.OracleClient; 
using System.Data.SqlClient; 
using System.IO; 
using System.Data.Odbc; 


class GasReporter 
{ 
    static void Primary(string[] args) 
    { 
string connectionString = "Dsn=Gas_meter"; 
     string col0 = ""; 
     string col1 = ""; 
     string col2 = ""; 
     string col3 = ""; 
     string col4 = ""; 
     string col5 = ""; 
     string col6 = ""; 
     string col7 = ""; 
     string col8 = ""; 
     string sqlins = ""; 
     string connString = "DSN=Gas_meter_proj;Uid=cm;Pwd=cmdev123"; 
     OdbcConnection conn = new OdbcConnection(connString);  
try 
{ 
     OdbcCommand cmdnon = new OdbcCommand(sqlins, conn);        
     conn.Open(); 
     cmdnon.Parameters.Add(col0, OdbcType.DateTime); 
     cmdnon.Parameters.Add(col1, OdbcType.Numeric); 
     cmdnon.Parameters.Add(col2, OdbcType.Numeric); 
     cmdnon.Parameters.Add(col3, OdbcType.Numeric); 
     cmdnon.Parameters.Add(col4, OdbcType.Numeric); 
     cmdnon.Parameters.Add(col5, OdbcType.Numeric); 
     cmdnon.Parameters.Add(col6, OdbcType.Numeric); 
     cmdnon.Parameters.Add(col7, OdbcType.Numeric); 
     cmdnon.Parameters.Add(col8, OdbcType.Numeric); 

        OdbcConnection DbConnection = new OdbcConnection(connectionString); 
        OdbcCommand DbCommand = DbConnection.CreateCommand(); 
        DbConnection.Open(); 
        DbCommand.CommandText = "SELECT DateTime, S1Flow, S2Flow, S3Flow, S4Flow, S1FlowTotal, S2FlowTotal, S3FlowTotal, S4FlowTotal FROM CommonStation WHERE Format(DateTime, 'mm/dd/yyyy') >=(select Format(max(DateTime),'mm/dd/yyyy') from CommonStation)"; 
        OdbcDataReader DbReader = DbCommand.ExecuteReader(); 
        int fCount = DbReader.FieldCount; 

        while (DbReader.Read()) 
        { 
         col0 = DbReader["DateTime"].ToString(); 
         col1 = DbReader["S1Flow"].ToString(); 
         col2 = DbReader["S2Flow"].ToString(); 
         col3 = DbReader["S3Flow"].ToString(); 
         col4 = DbReader["S4Flow"].ToString(); 
         col5 = DbReader["S1FlowTotal"].ToString(); 
         col6 = DbReader["S2FlowTotal"].ToString(); 
         col7 = DbReader["S3FlowTotal"].ToString(); 
         col8 = DbReader["S4FlowTotal"].ToString(); 
         cmdnon.CommandText="insert into Commonstation(CommStatDate_Time, S1_Flow, S2_Flow, S3_Flow, S4_Flow, S1_Flow_Total, S2_Flow_Total, S3_Flow_Total, S4_Flow_Total) values (to_date('" + col0 + "', 'MM/DD/YYYY HH:MI:SS AM'),to_number('" + col1 + "'), to_number('" + col2 + "'), to_number('" + col3 + "'), to_number('" + col4 + "'),to_number('" + col5 + "'),to_number('" + col6 + "'),to_number('" + col7 + "'),to_number('" + col8 + "'))"; 
         int rowsAffected = cmdnon.ExecuteNonQuery(); 
         Console.WriteLine(rowsAffected); 
        } 
       } 
       catch (Exception ex) 
       { 
        ex.InnerException.ToString()+")");            
} 
    finally 
       { 
        conn.Close(); 
        DbReader.Close(); 
        DbCommand.Dispose(); 
        DbConnection.Close(); 
       } 

    } 

} 

回答

1

嘗試:

cmdnon.CommandText = "INSERT INTO Commonstation... 

至少,那是什麼錯誤是告訴你。

+0

我不確定它與您所說的內容以及代碼中已存在的內容有什麼不同。他們似乎是完全一樣的。 –

+0

@丹特。你當前的代碼有'INSERT Commonstation'。我在'INSERT'後面說'放入'INTO'。 – AngryHacker

+0

好的,對不起,我以爲我確實有這樣寫。這解決了問題,謝謝。我的下一個問題使事情有點棘手。 –

0
string MyAccessConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:/mydata.accdb;Persist Security Info=False;"; 

private void button1_Click(object sender, EventArgs e) 
{ 
    OleDbConnection conn = new OleDbConnection(MyAccessConn); 
    OleDbCommand cmd; 
    conn.Open(); 
    try 
    { 
     cmd = conn.CreateCommand(); 

     cmd.CommandText = "INSERT INTO data([Name],[Surname],[Username],[Password]) VALUES(@Name,@Surname,@Username,@Password)"; 
     cmd.Parameters.AddWithValue("@Name", name_txt.Text); 
     cmd.Parameters.AddWithValue("@Surname", sur_txt.Text); 
     cmd.Parameters.AddWithValue("@Username", user_txt.Text); 
     cmd.Parameters.AddWithValue("@Password", pwd_txt.Text); 

     cmd.ExecuteNonQuery(); 
    } 
    catch (OleDbException ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
    finally 
    { 
     if (conn.State == ConnectionState.Open) 
     { 
      conn.Close(); 
     } 
    } 
}