Share via


Write Data to SQL Server from a Dictionary

Question

Monday, August 26, 2013 7:45 PM

I have a Dictionary like the following; how do I write it's key values to SQL Server?

Dictionary<string, string> de = new Dictionary<string, string>();

All replies (6)

Monday, August 26, 2013 9:52 PM ✅Answered

Try the code below.  The connection string may vary depending on the credentials .  Also the server name "SQLSTANDARD" (the default value) will vary depending on the way the database is installed.

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data.SqlClient;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            const string serverName = @"myServer";            const string database = "databaseName";            const string databaseTable = "Table1";            Dictionary<string, string> de = new Dictionary<string, string>();            string strCon = string.Format("Data Source={0}\\SQLSTANDARD;Initial Catalog={1};Integrated Security=SSPI",                serverName, database);            SqlConnection conn = new SqlConnection();            conn.Open();                        string SQLstr = string.Format("INSERT INTO {0} (FIRST_NAME, LAST_NAME)" +               "VALUES ('@FIRSTNAME', '@LASTNAME')", databaseTable);            SqlCommand cmd = new SqlCommand(SQLstr, conn);            cmd.Parameters.Add("@FIRSTNAME", System.Data.SqlDbType.VarChar);            cmd.Parameters.Add("@LASTNAME", System.Data.SqlDbType.VarChar);            foreach (KeyValuePair<string,string> key in de.AsEnumerable())            {                cmd.Parameters["@FIRSTNAME"].Value = key.ToString();                cmd.Parameters["@LASTNAME"].Value = key.Value.ToString();                cmd.ExecuteNonQuery();            }            conn.Close();            cmd.Dispose();            conn.Dispose();        }    }}

jdweng


Tuesday, August 27, 2013 1:23 AM ✅Answered

Hi NBee;

Please have a look at the following Video and Walk Through on using Linq to Entity Framework from creating the model to accessing the database. The only thing you need to write code for is the query itself everything else gets created by Visual Studio by using some dialog boxes.

Once you have selected one of the technologies like the previous post has suggested or using the Entity Framework we can then assist you with further questions.

  

Fernando (MCSD)

If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.


Monday, August 26, 2013 7:57 PM

Hi NBee;

Basically you need to iterate through the keys and values of the dictionary and then using the technology that you are using such as Linq to EF, Linq to SQL or ADO .Net DataSet create a new insert and populate the fields and save your changes.

Fernando (MCSD)

If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.


Monday, August 26, 2013 8:03 PM

Do you happen to know any helpful links to Linc to SQL or ADO.Net Dataset which will guide me through the steps of taking dictionary items and save in to SQL?


Monday, August 26, 2013 8:31 PM

No sorry I do not most tutorials are not that specific like using a Dictionary<string, string>.

What version of Visual Studio's are you using? 

Fernando (MCSD)

If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.


Monday, August 26, 2013 9:01 PM

2010

I can also create a 2-dimensional array from the dictionary butm then, how do I save it to the database