【发布时间】:2015-04-04 15:23:57
【问题描述】:
我已将 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();
}
【问题讨论】:
-
你能告诉我们你的连接字符串吗?
-
我的连接字符串是
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Storage.mdf;Integrated Security=True;User Instance=True
标签: c# sql-server insert-statement