1

I have added a Microsoft's SQL Server file to my project and I am running an SqlCommand to insert my data into the file. I am using System.Data.SqlClient;. The following code is how I add data to my file. After my program finished running then I go to the Data Explorer in my project and ask to Show Table Data of HistQuote and nothing show up. Could anyone advice on how I can verify that my INSERT statement is working.

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();
}
Andraro
  • 15
  • 3

2 Answers2

1

The whole User Instance and AttachDbFileName= approach is flawed - at best! When running your app in Visual Studio, it will be copying around the .mdf file (from your App_Data directory to the output directory - typically .\bin\debug - where you app runs) and most likely, your INSERT works just fine - but you're just looking at the wrong .mdf file in the end!

If you want to stick with this approach, then try putting a breakpoint on the myConnection.Close() call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there.

The real solution in my opinion would be to

  1. install SQL Server Express (and you've already done that anyway)

  2. install SQL Server Management Studio Express

  3. create your database in SSMS Express, give it a logical name (e.g. Storage)

  4. connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. In that case, your connection string would be something like:

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

    and everything else is exactly the same as before...

Also see Aaron Bertrand's excellent blog post Bad habits to kick: using AttachDbFileName for more background info.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • I thought that it was due to something like this. For your proposed solution _install SQL Server Express_ I have installed SQL Server 2012. I have been looking online to _install SQL Server Management Studio Express_ but I have to pay attention to what version I am using. I have found the following links [SSMS Express](http://www.microsoft.com/en-us/download/details.aspx?id=8961) but its a _management tool for SQL Server 2005_ my 1st difference. Would you reccomend installing [SQL Server Express](http://www.microsoft.com/en-us/server-cloud/products/sql-server-editions/sql-server-express.aspx) – Andraro Apr 05 '15 at 09:16
  • @Andraro: if you have a full version of SQL Server 2012 installed (other than Express) then that should include the full version of Management Studio - no need to install Express and Mgmt Studio Express in that case... – marc_s Apr 05 '15 at 09:20
  • Thanks for the clarification. Would it be possible to let me know how you deteriminated the `Data Surce` of the connection string that you gave in your answer? – Andraro Apr 05 '15 at 19:30
  • For future referance you can use the following link to [Find the connection string of the DB](http://stackoverflow.com/questions/10479763/how-to-get-the-connection-string-from-a-database) – Andraro Apr 05 '15 at 19:51
0

Would something like this possibly work?

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();
        }
    }
}
}
Jack Fairfield
  • 1,876
  • 22
  • 25