Connecting SQL server 2008 to Visual studio 2008
-
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.
-
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[^]
-
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.
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.
-
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:
-
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.
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.
-
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:
-
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: