0
我正在嘗試向已設置的本地數據庫的表(HistQuote
)插入一條新記錄。我正在使用SQL Server CE創建並執行我的SQL語句。該表是由8列的:如何使用C#中使用本地數據庫的SQL Server CE執行SQL語句
- 符號,數據類型:NCHAR(10)
- 日期,數據類型:日期時間(8)
- 打開,數據類型:數字(9)
- 高,數據類型:數字(9)
- 低,數據類型:數字(9)
- 關閉,數據類型:數字(9)
- Volumne,數據類型:數字(9)
- AdjClose,數據類型:數字(9)
但在運行代碼時,下面我得到以下錯誤就行了
sqlCeCommand.ExecuteNonQuery():
出現錯誤解析查詢。 [令牌行號= 1,令牌行偏移= 64,令牌錯誤=關閉]
我不知道這是否是由於這樣的事實,我的參數有不同的數據類型,然後我的表或別的東西。
感謝您的幫助。
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();
}
我覺得參數事項的順序。嘗試更改添加的參數的順序以匹配查詢中的參數。 – XtremeBytes 2015-04-03 14:45:17
我已經改變了strColumns和strValues的順序,但現在得到一個不同的錯誤_解析查詢時出錯。 [令牌行號= 1,令牌行偏移量= 40,令牌出錯=打開] _ – Andraro 2015-04-03 15:05:51
來自錯誤的其「失敗」的「打開」字段。所有的數據類型映射是否正確,特別是日期時間?使用Convert.ToSingle實現和Convert.ToDateTime的日期 – XtremeBytes 2015-04-03 15:36:57