我已将 Microsoft 的 SQL Server 文件添加到我的项目中,并且正在运行SqlCommand
将我的数据插入文件中。我是using System.Data.SqlClient;
。以下代码是我如何将数据添加到我的文件中。在我的程序完成运行后,我转到项目中的数据资源管理器并要求显示表数据,HistQuote
但没有任何显示。任何人都可以就我如何验证我的INSERT
陈述是否有效提供建议。
using (SqlConnection connection = new SqlConnection(Settings.Default.StorageConnectionString))
{
connection.Open();
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, [Date], [Open], High, Low, Volume, Adj_Close, [Close]) ";
string strValues = "VALUES (@Symbol, @Date, @Open, @High, @Low, @Volume, @Adj_Close, @Close)";
using (SqlCommand sqlCommand = new SqlCommand(strInsert + strColumns + strValues, connection))
{
sqlCommand.Parameters.Clear();
sqlCommand.Parameters.Add(new SqlParameter("@Symbol", SqlDbType.NChar));
sqlCommand.Parameters.Add(new SqlParameter("@Date", SqlDbType.DateTime));
sqlCommand.Parameters.Add(new SqlParameter("@Open", SqlDbType.Real));
sqlCommand.Parameters.Add(new SqlParameter("@High", SqlDbType.Real));
sqlCommand.Parameters.Add(new SqlParameter("@Low", SqlDbType.Real));
sqlCommand.Parameters.Add(new SqlParameter("@Close", SqlDbType.Real));
sqlCommand.Parameters.Add(new SqlParameter("@Volume", SqlDbType.Real));
sqlCommand.Parameters.Add(new SqlParameter("@Adj_Close", SqlDbType.Real));
sqlCommand.Parameters["@Symbol"].Size = 10;
sqlCommand.Prepare();
sqlCommand.Parameters["@Symbol"].Value = this.Quotes[intCurrentQuote].HistSymbol;
sqlCommand.Parameters["@Date"].Value = this.Quotes[intCurrentQuote].HistStockDate[intCurrentDate];
sqlCommand.Parameters["@Open"].Value = this.Quotes[intCurrentQuote].HistOpen[intCurrentDate];
sqlCommand.Parameters["@High"].Value = this.Quotes[intCurrentQuote].HistHigh[intCurrentDate];
sqlCommand.Parameters["@Low"].Value = this.Quotes[intCurrentQuote].HistLow[intCurrentDate];
sqlCommand.Parameters["@Close"].Value = this.Quotes[intCurrentQuote].HistClose[intCurrentDate];
sqlCommand.Parameters["@Volume"].Value = this.Quotes[intCurrentQuote].HistVolume[intCurrentDate];
sqlCommand.Parameters["@Adj_Close"].Value = this.Quotes[intCurrentQuote].HistAdjClose[intCurrentDate];
sqlCommand.ExecuteNonQuery();
sqlCommand.Parameters.Clear();
}
}
}
connection.Close();
}