Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Connecting SQL server 2008 to Visual studio 2008

Connecting SQL server 2008 to Visual studio 2008

Scheduled Pinned Locked Moved .NET (Core and Framework)
questioncsharpdatabasesql-servervisual-studio
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MahaKh
    wrote on last edited by
    #1

    how can i connect visual studio 2008 application to sql server 2008 using ODBC?

    R P L 3 Replies Last reply
    0
    • M MahaKh

      how can i connect visual studio 2008 application to sql server 2008 using ODBC?

      R Offline
      R Offline
      Roger Wright
      wrote on last edited by
      #2

      Why would you want to? ODBC is the method of last resort, to be used when nothing else will work, and VS 2008 comes with classes optimized for connecting to SQL Server. The only excuse for using ODBC is if your application has to interface with other database products after you're done developing it, and you want to minimize the amount of customization required for installation. Use the classes in System.Data.Sql to access a SQL Server database. If you need to rehost the solution to another database, you might duplicate the functionality you build for SQL Server using the namespace, System.Data.Odbc and use the installer to select which method is implemented.

      Will Rogers never met me.

      P 1 Reply Last reply
      0
      • M MahaKh

        how can i connect visual studio 2008 application to sql server 2008 using ODBC?

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        First, get an ODBC driver for Sql Server 2008... http://technet.microsoft.com/en-us/library/ms131421(SQL.90).aspx[^] http://msdn.microsoft.com/en-us/library/ms130822%28SQL.90%29.aspx[^]

        1 Reply Last reply
        0
        • R Roger Wright

          Why would you want to? ODBC is the method of last resort, to be used when nothing else will work, and VS 2008 comes with classes optimized for connecting to SQL Server. The only excuse for using ODBC is if your application has to interface with other database products after you're done developing it, and you want to minimize the amount of customization required for installation. Use the classes in System.Data.Sql to access a SQL Server database. If you need to rehost the solution to another database, you might duplicate the functionality you build for SQL Server using the namespace, System.Data.Odbc and use the installer to select which method is implemented.

          Will Rogers never met me.

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Hey wow, you say that like you know what you're talking about. :-D

          Roger Wright wrote:

          Why would you want to?

          Because it's there. I don't know about the OP, but I just like to try things out -- I'll try most anything once. And I like to test my database code in a number of configurations. My first experience with Sql Server (v6 circa 1999) was with ODBC... brings back memories... ones better left forgotten.

          R 1 Reply Last reply
          0
          • M MahaKh

            how can i connect visual studio 2008 application to sql server 2008 using ODBC?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Hi,

            MahaKh wrote:

            how can i connect visual studio 2008 application to sql server 2008 using ODBC?

            Here's an example that works for me (.NET 2.0, W7, SQL 2005)

            using (OdbcConnection con = new OdbcConnection(
            "DRIVER={SQL Server};SERVER=.;Trusted_connection=yes;DATABASE=AdventureWorks;"))
            using (OdbcCommand cmd = con.CreateCommand())
            {
            con.Open();
            cmd.CommandText = @"
            SELECT FirstName
            FROM Person.Contact
            WHERE ContactID = 20";

            string 结果 = (string)cmd.ExecuteScalar();
            
            MessageBox.Show(String.Format(
                "The FirstName of Person.Contact is '{0}'.", 结果));
            

            }

            ..and this on top of that unit, between the other imports and the namespace-declaration;

            using System.Data.Odbc;

            And here's the manual[^] :)

            I are Troll :suss:

            M 1 Reply Last reply
            0
            • P PIEBALDconsult

              Hey wow, you say that like you know what you're talking about. :-D

              Roger Wright wrote:

              Why would you want to?

              Because it's there. I don't know about the OP, but I just like to try things out -- I'll try most anything once. And I like to test my database code in a number of configurations. My first experience with Sql Server (v6 circa 1999) was with ODBC... brings back memories... ones better left forgotten.

              R Offline
              R Offline
              Roger Wright
              wrote on last edited by
              #6

              I've learned a thing or two hanging around this unsavory lot. :-D As a result, I've given up driving square pegs in round holes, among other bad habits. Just because I can doesn't mean I should. ;P

              Will Rogers never met me.

              1 Reply Last reply
              0
              • L Lost User

                Hi,

                MahaKh wrote:

                how can i connect visual studio 2008 application to sql server 2008 using ODBC?

                Here's an example that works for me (.NET 2.0, W7, SQL 2005)

                using (OdbcConnection con = new OdbcConnection(
                "DRIVER={SQL Server};SERVER=.;Trusted_connection=yes;DATABASE=AdventureWorks;"))
                using (OdbcCommand cmd = con.CreateCommand())
                {
                con.Open();
                cmd.CommandText = @"
                SELECT FirstName
                FROM Person.Contact
                WHERE ContactID = 20";

                string 结果 = (string)cmd.ExecuteScalar();
                
                MessageBox.Show(String.Format(
                    "The FirstName of Person.Contact is '{0}'.", 结果));
                

                }

                ..and this on top of that unit, between the other imports and the namespace-declaration;

                using System.Data.Odbc;

                And here's the manual[^] :)

                I are Troll :suss:

                M Offline
                M Offline
                MahaKh
                wrote on last edited by
                #7

                thanks to all, but my requirement is to use OLEDB instead of ODBC....:(

                L 1 Reply Last reply
                0
                • M MahaKh

                  thanks to all, but my requirement is to use OLEDB instead of ODBC....:(

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  MahaKh wrote:

                  but my requirement is to use OLEDB instead of ODBC

                  The requirement should state that you should use a SqlConnection. Anyway, the difference between the code is relatively subtle;

                  using (OleDbConnection con = new OleDbConnection(
                  "Provider=SQLNCLI;Server=.;Database=AdventureWorks;Trusted_Connection=yes;"))
                  using (OleDbCommand cmd = con.CreateCommand())
                  {
                  con.Open();
                  cmd.CommandText = @"
                  SELECT FirstName
                  FROM Person.Contact
                  WHERE ContactID = 20";

                  string result = (string)cmd.ExecuteScalar();
                  
                  MessageBox.Show(String.Format(
                      "The FirstName of Person.Contact is '{0}'.", 
                      result
                  ));
                  

                  }

                  Code tested and verified on W7, .NET 2.0, SQL2k5

                  I are Troll :suss:

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups