Click here to Skip to main content
11,492,168 members (59,342 online)
Rate this: bad
good
Please Sign up or sign in to vote.
Hey, how do i connect a database which i hav created in sql server 2012 to c# successfully. Help appreciated..
Posted 24-Feb-13 21:57pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try setting up a connection in VS with the Server Explorer pane:
1) Open Server Explorer.
2) Right click "Data connections" and select "Add connection"
3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
4) When the connection works, press "OK"
5) Highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file.
Then it's just code:
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("SELECT iD, description FROM myTable", con))
        {
        using (SqlDataReader reader = com.ExecuteReader())
            {
            while (reader.Read())
                {
                int id = (int) reader["iD"];
                string desc = (string) reader["description"];
                Console.WriteLine("ID: {0}\n    {1}", iD, desc);
                }
            }
        }
    }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You may want to start with finding the correct connection string for SQL Server 2012 here for instance: Connection strings for SQL Server 2012[^].
You may also need to configure your SQL Server for remote access if you're not running your programs on the same machine where the SQL Server lives: The minimum requirements to access a remote SQL Server 2012 Database[^].

Regards,

—Manfred
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 550
1 Maciej Los 276
2 Sascha Lefèvre 265
3 Andy Lanng 249
4 F-ES Sitecore 230
0 Sergey Alexandrovich Kryukov 9,829
1 OriginalGriff 8,530
2 Sascha Lefèvre 3,579
3 Maciej Los 3,332
4 Richard Deeming 2,370


Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100