Click here to Skip to main content
15,887,821 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Sascha Lefèvre23-Apr-15 5:56
professionalSascha Lefèvre23-Apr-15 5:56 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell23-Apr-15 6:20
Norris Chappell23-Apr-15 6:20 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Kevin Marois23-Apr-15 6:37
professionalKevin Marois23-Apr-15 6:37 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Sascha Lefèvre23-Apr-15 6:55
professionalSascha Lefèvre23-Apr-15 6:55 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell23-Apr-15 13:21
Norris Chappell23-Apr-15 13:21 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Sascha Lefèvre24-Apr-15 3:00
professionalSascha Lefèvre24-Apr-15 3:00 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell24-Apr-15 5:25
Norris Chappell24-Apr-15 5:25 
AnswerRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Sascha Lefèvre24-Apr-15 11:04
professionalSascha Lefèvre24-Apr-15 11:04 
Right, SqlBulkCopy would be a third option, but actually I've never worked with it myself.

I wrote a method for you which should work for you. You should only have to adjust some minor stuff Smile | :)

C#
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.IO;
using System.Windows.Forms;


C#
private static char[] Colon = new char[] { ',' };
private DataTable QueryStaff()
{
    const int nameColumnIndex = 1;
    const int hoursColumnIndex = 2;

    using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConsoleApplication3.Properties.Settings.daasConnectionString"].ConnectionString))
    using (var cmd = new SqlCommand("", conn))
    using (var dataAdapter = new SqlDataAdapter(cmd))
    using (var cmdBuilder = new SqlCommandBuilder(dataAdapter))
    {
        // create temporary table in database
        cmd.CommandText = "CREATE TABLE #TempTable(Name nvarchar(100) NOT NULL, Hours decimal(6, 2) NOT NULL);";
        cmd.ExecuteNonQuery();

        // create a DataTable and let the DataAdapter create appropriate columns for it
        DataTable dataTable = new DataTable();
        cmd.CommandText = "SELECT * FROM #TempTable;";
        dataAdapter.Fill(dataTable);

        // read the CSV-records into the DataTable
        dataTable.BeginLoadData();
        using (StreamReader reader = File.OpenText(@"c:\temp\table1.csv"))
        {
            string line;
            if (reader.ReadLine() != null) // skip first line (headers)
            {
                while ((line = reader.ReadLine()) != null)
                {
                    string[] columns = line.Split(Colon, StringSplitOptions.None);

                    DataRow row = dataTable.NewRow();
                    row["Name"] = columns[nameColumnIndex];
                    row["Hours"] = Decimal.Parse(columns[hoursColumnIndex], NumberFormatInfo.InvariantInfo);
                    dataTable.Rows.Add(row);
                }
            }
        }
        dataTable.EndLoadData();

        // insert the records from the DataTable into the temporary table
        dataAdapter.Update(dataTable);

        // load the result of the "main purpose" query into the DataTable
        dataTable.Clear();
        cmd.CommandText = "SELECT Tmp.Name, SUM(Tmp.Hours) FROM #TempTable AS Tmp WHERE NOT EXISTS (SELECT * FROM Stafftracking AS ST WHERE Tmp.Name = ST.Name) GROUP BY Tmp.Name;";
        dataAdapter.Fill(dataTable);

        return dataTable;
    }
}


C#
// and then somewhere:
GridView1.DataSource = QueryStaff();
GridView1.DataBind();


You have to check/adjust:
- nameColumnIndex and hoursColumnIndex
- the table- and column names in the last query string

Other notes:
- I recommended the CSV-Reader-library because reading CSV like it's done here is a brittle approach. It can't deal with colons in values.
- In case the amount of hours in your CSV-file can be a decimal value, this solution will read it correctly if a dot is being used as decimal separator (NumberFormatInfo.InvariantInfo).

If you shouldn't get it to work or if you don't understand something I've done here, please ask.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell24-Apr-15 11:57
Norris Chappell24-Apr-15 11:57 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Sascha Lefèvre24-Apr-15 12:56
professionalSascha Lefèvre24-Apr-15 12:56 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell25-Apr-15 13:28
Norris Chappell25-Apr-15 13:28 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Sascha Lefèvre25-Apr-15 13:42
professionalSascha Lefèvre25-Apr-15 13:42 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell25-Apr-15 13:50
Norris Chappell25-Apr-15 13:50 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Sascha Lefèvre25-Apr-15 14:02
professionalSascha Lefèvre25-Apr-15 14:02 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell25-Apr-15 14:08
Norris Chappell25-Apr-15 14:08 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell25-Apr-15 14:11
Norris Chappell25-Apr-15 14:11 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell25-Apr-15 14:13
Norris Chappell25-Apr-15 14:13 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Sascha Lefèvre25-Apr-15 15:11
professionalSascha Lefèvre25-Apr-15 15:11 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell25-Apr-15 15:28
Norris Chappell25-Apr-15 15:28 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell25-Apr-15 15:35
Norris Chappell25-Apr-15 15:35 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Sascha Lefèvre25-Apr-15 15:41
professionalSascha Lefèvre25-Apr-15 15:41 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell25-Apr-15 15:47
Norris Chappell25-Apr-15 15:47 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Sascha Lefèvre25-Apr-15 15:56
professionalSascha Lefèvre25-Apr-15 15:56 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Sascha Lefèvre25-Apr-15 15:37
professionalSascha Lefèvre25-Apr-15 15:37 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell25-Apr-15 15:50
Norris Chappell25-Apr-15 15:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.