locked
Set Up Connectionstring Using C# RRS feed

  • Question

  • I am trying to figure out how to establish a connectionstring to my SQL Server 2008 Express DB.  I’ve been using ASP.NET for about a year and VB.Net for about a year; I know how to set up connection strings using those apps.  I’m just starting to learn C# (all self-taught).  I’m wondering if there is a good video tutorial other there, or good step-by-step-instructions about how to establish a connectionstring for beginning C3-ers.

     

    Thanks!

    Ryan--

    Tuesday, March 30, 2010 4:15 PM

Answers

  • Same as with your ASP.Net app:

     

                using (SqlConnection dbConn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa;Password=xxx"))
                {
                    using (SqlCommand dbCmd = new SqlCommand("SELECT Count(*) FROM employees", dbConn))
                    {
                        dbConn.Open();
                        Console.WriteLine("employee count: {0}", (Int32?)dbCmd.ExecuteScalar());
                    }
                }
    


    jon.stromer.galley
    • Marked as answer by ryguy72 Tuesday, March 30, 2010 9:15 PM
    Tuesday, March 30, 2010 5:06 PM

All replies

  • Same as with your ASP.Net app:

     

                using (SqlConnection dbConn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa;Password=xxx"))
                {
                    using (SqlCommand dbCmd = new SqlCommand("SELECT Count(*) FROM employees", dbConn))
                    {
                        dbConn.Open();
                        Console.WriteLine("employee count: {0}", (Int32?)dbCmd.ExecuteScalar());
                    }
                }
    


    jon.stromer.galley
    • Marked as answer by ryguy72 Tuesday, March 30, 2010 9:15 PM
    Tuesday, March 30, 2010 5:06 PM
  • Thanks jgalley!!

     

    Tuesday, March 30, 2010 9:15 PM