Connecting to a SQL Server 2005 Database

S

Sherwood

Greetings,

I am using Visual C#.NET 2005 (Express Edition) and want to connect to a SQL
Server 2005 (Express Edition) database. When I attempt to compile the code
below, however, I receive the error message:

"Use of unassigned local variable 'con'"

System.Data.SqlClient.SqlConnection con;
con.ConnectionString = "Data Source=HOME-PC\\SQLEXPRESS;Initial
Catalog=Contacts;User ID=sa";
con.Open();

Any ideas as to what I am doing wrong?

Thanks in advance!
 
A

Arne Vajhøj

Sherwood said:
I am using Visual C#.NET 2005 (Express Edition) and want to connect to a SQL
Server 2005 (Express Edition) database. When I attempt to compile the code
below, however, I receive the error message:

"Use of unassigned local variable 'con'"

System.Data.SqlClient.SqlConnection con;
con.ConnectionString = "Data Source=HOME-PC\\SQLEXPRESS;Initial
Catalog=Contacts;User ID=sa";
con.Open();

Any ideas as to what I am doing wrong?

You are missing something like:

con = new SqlConnection();

Arne
 
M

Mudassar Hassan

Try this

System.Data.SqlClient.SqlConnection con;
con = new System.Data.SqlClient.SqlConnection();
con.ConnectionString = "Data Source=HOME-PC\\SQLEXPRESS;Initial
Catalog=Contacts;User ID=sa";
con.Open();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top