Search code examples
c#sql-server-celocal-database

How to execute a SQL statement with SQL Server CE using a local database in C#


I am trying to insert a new record into a table (HistQuote) of a local database that I have set up. I am using SQL Server CE to create and execute my SQL statement. The table is made up of 8 columns:

  • Symbol, data type: nchar(10)
  • Date, data type: datetime(8)
  • Open, data type: numeric(9)
  • High, data type: numeric(9)
  • Low, data type: numeric(9)
  • Close, data type: numeric(9)
  • Volumne, data type: numeric(9)
  • AdjClose, data type: numeric(9)

But when running the code below I get the following error on the line

sqlCeCommand.ExecuteNonQuery():

There was an error parsing the query. [ Token line number = 1,Token line offset = 64,Token in error = Close ]

I am not sure if this is due to the fact that my parameters have a different data type then my table or something else.

thanks for your help.

object[] objSQLArray = new object[8];

using (SqlCeConnection connection = new SqlCeConnection(Settings.Default.DataStorageConnectionString))
{ 
    for (int intCurrentQuote = 0; intCurrentQuote < this.clbStockSelect.CheckedItems.Count; ++intCurrentQuote)
    {
        for (int intCurrentDate = 0; intCurrentDate < Quotes[intCurrentQuote].HistStockDate.Count; ++intCurrentDate)
        {
            string strInsert = "INSERT INTO [HistQuote] ";
            string strColumns = "(Symbol, High, Low, Volumne, AdjClose, Close, Open, Date) ";
            string strValues = "VALUES (@Symbol, @High, @Low, @Volumne, @AdjClose, @Close, @Open, @Date)";

            objSQLArray[0] = this.Quotes[intCurrentQuote].HistSymbol;
            objSQLArray[7] = this.Quotes[intCurrentQuote].HistStockDate[intCurrentDate];
            objSQLArray[1] = this.Quotes[intCurrentQuote].HistOpen[intCurrentDate];
            objSQLArray[2] = this.Quotes[intCurrentQuote].HistHigh[intCurrentDate];
            objSQLArray[3] = this.Quotes[intCurrentQuote].HistLow[intCurrentDate];
            objSQLArray[4] = this.Quotes[intCurrentQuote].HistClose[intCurrentDate];
            objSQLArray[5] = this.Quotes[intCurrentQuote].HistVolume[intCurrentDate];
            objSQLArray[6] = this.Quotes[intCurrentQuote].HistAdjClose[intCurrentDate];

            using (SqlCeCommand sqlCeCommand = new SqlCeCommand(strInsert + strColumns + strValues, connection))
            {
                sqlCeCommand.Connection.Open();
                sqlCeCommand.Parameters.Clear();
                sqlCeCommand.Parameters.Add(new SqlCeParameter("@Symbol", SqlDbType.NChar));
                sqlCeCommand.Parameters.Add(new SqlCeParameter("@Date", SqlDbType.DateTime));
                sqlCeCommand.Parameters.Add(new SqlCeParameter("@Open", SqlDbType.Real));
                sqlCeCommand.Parameters.Add(new SqlCeParameter("@High", SqlDbType.Real));
                sqlCeCommand.Parameters.Add(new SqlCeParameter("@Low", SqlDbType.Real));
                sqlCeCommand.Parameters.Add(new SqlCeParameter("@Close", SqlDbType.Real));
                sqlCeCommand.Parameters.Add(new SqlCeParameter("@Volume", SqlDbType.Real));
                sqlCeCommand.Parameters.Add(new SqlCeParameter("@Adj_Close", SqlDbType.Real));

                sqlCeCommand.Parameters["@Symbol"].Size = 10;

                sqlCeCommand.Prepare();

                sqlCeCommand.Parameters["@Symbol"].Value = objSQLArray[0].ToString();
                sqlCeCommand.Parameters["@Date"].Value = objSQLArray[7];
                sqlCeCommand.Parameters["@Open"].Value = objSQLArray[1];
                sqlCeCommand.Parameters["@High"].Value = objSQLArray[2];
                sqlCeCommand.Parameters["@Low"].Value = objSQLArray[3];
                sqlCeCommand.Parameters["@Close"].Value = objSQLArray[4];
                sqlCeCommand.Parameters["@Volume"].Value = objSQLArray[5];
                sqlCeCommand.Parameters["@Adj_Close"].Value = objSQLArray[6];

                sqlCeCommand.ExecuteNonQuery();

                sqlCeCommand.Parameters.Clear();
            }
        }
    }

    connection.Close();
}

Solution

  • Open and close are reserved Words, enclose your column names in the sql statementin Square brackets:

    [Close], [Open], [Date]