tags:

views:

78

answers:

4

i am using this example to connect c# to sql server. can you please tell me what i have to include in order to be able to use sqlconnection?

it must be something like: using Sqlconnection; ???

string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=""C:\SQL Server 2000 Sample Databases\NORTHWND.MDF"";Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlConnection sqlCon = new SqlConnection(connectionString);
 sqlCon.Open();

string commandString = "SELECT * FROM Customers";
 SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);
 SqlDataReader dataReader = sqlCmd.ExecuteReader();

while (dataReader.Read())
 {
   Console.WriteLine(String.Format("{0} {1}", dataReader["CompanyName"], dataReader["ContactName"]));
 }
 dataReader.Close();
 sqlCon.Close();
+4  A: 
using System.Data;
using System.Data.SqlClient;
renick
In the code in the question, none of the classes required System.Data namespace to be included.
ydobonmai
+1  A: 

Also, check out connectionstrings.com - it's probably the most useful tech website on the internet for making sure your ADO.NET is set up right.

When you decide to move on from just using Northwind, that site will come in handy a bunch. No one I know actually remembers the syntax for a connection string.

Tejs
+2  A: 

One nice trick one when you don't know what namespace to use in using statement. Type the classname (e.g. SqlConnection, MemoryStream, FileStream, etc), then press Ctrl+.. Though it won't work if the classname doesn't match the case, e.g. Sqlconnection, sqlconnection

Michael Buen
+1  A: 

Just include the following namespace:-

using System.Data.SqlClient;
ydobonmai