我已将微软的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