Connection string for odbc in C sharp

First create a odbc driver as specified below.

goto control panel->system and security->Administrative tools->data sources(odbc)

step1)click on add

step2)select sql server

step3)specify ur sql server name and create the driver

IN C sharp write the following commands to get connect:

Odbcconnection con=new Odbcconnection(“Driver=<drivername>;Server=<servername>;UID=<username>;PWD=<passwd>;“);

if your sql server is authorized then no need of specifing user name and password

eg:    Odbcconnection con=new Odbcconnection(“Driver={SQL server};Server=shivaji-pc;“);

if not authorized then.

eg:    Odbcconnection con=new Odbcconnection(“Driver={SQL server};Server=shivaji-pc;UID=shivaji;PWD=1234;“);

sample program to execute a query:

public viod exequery( )

{

Odbcconnection con=new Odbcconnection(“Driver={SQL server};Server=shivaji-pc;“);

Odbccommand comd=new Odbccommand(“insert into student values(’21’,’shivaji’);“,con);

con.Open( );

comd.ExecuteNonQuery( );

con.Close( );

}

note:

you can also create odbc drivers for oracle and other db server in similar manner.

you can also connect directly to db server without using odbc.

<<there are many other ways to get connect>>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s