Adding data to a Microsoft's SQL Server file using c#

351 Views Asked by At

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();
}
2

There are 2 best solutions below

1
On

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();
        }
    }
}
}
4
On

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.