2015-04-04 71 views
1

我已将微软的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(); 
} 
+0

你能告诉我们你的连接字符串吗? – 2015-04-04 15:27:11

+0

我的连接字符串是'Data Source =。\ SQLEXPRESS; AttachDbFilename = | DataDirectory | \ Storage.mdf;集成安全性= True;用户实例= True' – Andraro 2015-04-04 15:47:48

回答

0

会这样的事情可能工作?

using System; 
using System.Collections.Generic; 
using System.Data.SqlClient; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace Dataread 
{ 
    class Program 
{ 
    static void Main(string[] args) 
    { 
     using (SqlConnection connection = new SqlConnection(Settings.Default.StorageConnectionString)) 
     { 
      connection.Open(); 
      string strCmd = "Select * from [HistQuote]"; 

      using (SqlCommand sqlCommand = new SqlCommand(strCmd, connection)) 
      { 
       var rdr = new SqlDataReader(); 
       rdr = sqlCommand.ExecuteReader(); 
       while(rdr.Read()) 
       { 
        Console.WriteLine(rdr["Symbol"].ToString() + rdr["Date"].ToString() + rdr["Open"].ToString() + rdr["High"].ToString() + rdr["Low"].ToString() + rdr["Volume"].ToString() + rdr["Adj_Close"].ToString() + rdr["Close"].ToString()); 
       } 
      } 
      connection.Close(); 
     } 
    } 
} 
} 
+0

'rdr.Read()'返回false。 – Andraro 2015-04-04 21:12:53

1

整个用户实例和AttachDbFileName =方法是有缺陷的 - 在最好的!在Visual Studio中运行应用程序时,它将复制.mdf文件(从您的App_Data目录到输出目录 - 通常为.\bin\debug) - 最有可能为,您的INSERT工作得很好 - 但是您只是看着错误.mdf文件到底!

如果你想坚持这种方法,那么试着在myConnection.Close()调用上放一个断点 - 然后用SQL Server Mgmt Studio Express检查.mdf文件 - 我几乎可以确定你的数据在那里。

在我看来真正的解决方案

  1. 安装SQL Server Express(和你已经做到这一点无论如何)

  2. 安装SQL Server Management Studio中快速

  3. 中创建您的数据库SSMS Express,给它一个逻辑名称(例如Storage

  4. 使用其逻辑数据库名称连接到它(在服务器上创建时给出) - 并且不要乱用物理数据库文件和用户实例。在这种情况下,您的连接字符串将是这样的:

    Data Source=.\\SQLEXPRESS;Database=Storage;Integrated Security=True 
    

    和其他一切是正是和以前一样......

另见阿龙贝特朗的优秀博客文章Bad habits to kick: using AttachDbFileName更多背景信息。

+0

我以为这是由于这样的事情。对于您提出的解决方案_install SQL Server Express_我已经安装了SQL Server 2012.我一直在寻找_install SQL Server Management Studio Express_,但我必须注意我使用的是什么版本。我发现了以下链接[SSMS Express](http://www.microsoft.com/zh-cn/download/details.aspx?id=8961),但它是SQL Server 2005的_management工具,这是我的第一个区别。你会推荐安装[SQL Server Express](http://www.microsoft.com/en-us/server-cloud/products/sql-server-editions/sql-server-express.aspx) – Andraro 2015-04-05 09:16:36

+0

@Andraro:如果你有一个完整版本的SQL Server 2012安装(Express除外),那么应该包括完整版本的Management Studio - 无需安装Express和Mgmt Studio Express ... – 2015-04-05 09:20:08

+0

感谢您的澄清。是否有可能让我知道你是如何抑制你在答案中给出的连接字符串的“数据Surce”? – Andraro 2015-04-05 19:30:54

相关问题