如何在 C# 中使用本地数据库使用 SQL Server CE 执行 SQL 语句
How to execute a SQL statement with SQL Server CE using a local database in C#
我正在尝试将新记录插入到我已设置的本地数据库的 table (HistQuote
) 中。我正在使用 SQL Server CE 来创建和执行我的 SQL 语句。 table 由 8 列组成:
- 符号,数据类型:nchar(10)
- 日期,数据类型:datetime(8)
- 打开,数据类型:数值(9)
- 高,数据类型:数值型(9)
- 低,数据类型:数字(9)
- 关闭,数据类型:数值(9)
- 卷,数据类型:数字(9)
- AdjClose,数据类型:数值型(9)
但是当 运行 下面的代码时,我在
行收到以下错误
sqlCeCommand.ExecuteNonQuery():
There was an error parsing the query. [ Token line number = 1,Token line offset = 64,Token in error = Close ]
我不确定这是否是因为我的参数的数据类型与我的 table 或其他类型不同。
感谢您的帮助。
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();
}
Open 和 close 是保留字,将您在 sql 语句中的列名称括在方括号中:
[Close], [Open], [Date]
我正在尝试将新记录插入到我已设置的本地数据库的 table (HistQuote
) 中。我正在使用 SQL Server CE 来创建和执行我的 SQL 语句。 table 由 8 列组成:
- 符号,数据类型:nchar(10)
- 日期,数据类型:datetime(8)
- 打开,数据类型:数值(9)
- 高,数据类型:数值型(9)
- 低,数据类型:数字(9)
- 关闭,数据类型:数值(9)
- 卷,数据类型:数字(9)
- AdjClose,数据类型:数值型(9)
但是当 运行 下面的代码时,我在
行收到以下错误sqlCeCommand.ExecuteNonQuery():
There was an error parsing the query. [ Token line number = 1,Token line offset = 64,Token in error = Close ]
我不确定这是否是因为我的参数的数据类型与我的 table 或其他类型不同。
感谢您的帮助。
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();
}
Open 和 close 是保留字,将您在 sql 语句中的列名称括在方括号中:
[Close], [Open], [Date]