我已將微軟的SQL Server文件添加到我的項目中,並且正在運行一個SqlCommand
以將我的數據插入到該文件中。我是using System.Data.SqlClient;
。以下代碼是我如何將數據添加到我的文件。在我的程序運行完畢後,我進入我的項目中的Data Explorer並要求顯示HistQuote
的表格數據,但沒有顯示出來。任何人都可以建議我如何驗證我的INSERT
聲明正在工作。使用c將數據添加到Microsoft的SQL Server文件#
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();
}
你能告訴我們你的連接字符串嗎? – 2015-04-04 15:27:11
我的連接字符串是'Data Source =。\ SQLEXPRESS; AttachDbFilename = | DataDirectory | \ Storage.mdf;集成安全性= True;用戶實例= True' – Andraro 2015-04-04 15:47:48