Basic SQL Server Query in C#

If you need to access a database on an MSSQL server programmatically (in C#) just to do some quick and dirty data extraction and perhaps manipulation, the following code snippet will get you started. Replace the connection string, table and columns names with whatever is appropriate for your task.

static void Main(string[] args)
{
try
{
SqlConnection thisConnection = new SqlConnection(@"Server=192.168.1.1,1433;database=NorthWind;User id=sa;Password=HIUHFuhf8;");
thisConnection.Open();
SqlCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText = "SELECT [ID], [Name] FROM Customers";
SqlDataReader thisReader = thisCommand.ExecuteReader();
while (thisReader.Read())
{
Console.WriteLine("\t{0}\t{1}", thisReader["ID"], thisReader["Name"]);
}
thisReader.Close();
thisConnection.Close();
}
catch (SqlException e)
{
Console.WriteLine(e.Message);
}
Console.ReadLine();
}