2015-04-04 75 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

相關問題