Monday, March 19, 2012

Simple code to connect sql server 2008 with c#


 After wandering over the internet i got the most simplest code to connect c# with SQL server and get data in datatable or dataset.


  string connString = "Data Source=\SQLEXPRESS;Initial Catalog=dbname; User ID=urID; Password=urPAss";
        string sql = @"select * from table";
        SqlConnection conn = new SqlConnection(connString);
        try
        {
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            da.Fill(dt);
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: " + e);
        }
        finally
        {
            conn.Close();
        }

No comments:

Post a Comment