Connecting to a SQL Server 2005 Database

  • Thread starter Thread starter Sherwood
  • Start date Start date
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!
 
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
 
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();
 
Back
Top