Hi, i was wondering if someone could help me with two issues
1) I created a simple console program (code file and then set the properties to console app). then just typed in the namespaces i required but for some reason it wouldnt let me compile it without go to the Project -> Add reference menu and physically selecting them there as well. System.Data.SqlClient was missing but im assuming thats just because its part of the System.Data Namespace?
2) The main problem im having is with connecting to the copy of MS SQLEXPRESS 2008 r2 that i have installed on my pc. The server name is OPHIUCHUS and i have Windows Authentication selected so i dont need to enter my password in the code (so i believe)? With that in mind, im confused as to why i cant create a connection to it with the following code. Note im trying to use a ConnectionStringBuilder object.
Is there something stupid i have missed??
Code:
using System;
using System.Data;
using System.Data.SqlClient;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("****Fun with Data Readers****\n");
// START OF Updated Method Using Connection String Builder Objects
// Create A Connection String Via the ConnetionStringBuilder Object
SqlConnectionStringBuilder cnStrBuilder = new SqlConnectionStringBuilder();
cnStrBuilder.InitialCatalog = "AutoLot";
cnStrBuilder.DataSource = @"(local)\SQLEXPRESS";
cnStrBuilder.ConnectTimeout = 30;
cnStrBuilder.IntegratedSecurity = true;
using(SqlConnection cn = new SqlConnection()){
cn.ConnectionString = cnStrBuilder.ConnectionString;
cn.Open();
ShowConnectionStatus(cn);
string strSQL = "Select * From Inventory";
SqlCommand myCommand = new SqlCommand(strSQL, cn);
SqlCommand testCommand = new SqlCommand();
testCommand.Connection = cn;
testCommand.CommandText = strSQL;
SqlDataReader myDataReader = myCommand.ExecuteReader();
while (myDataReader.Read())
{
Console.WriteLine("**** Records ****");
for (int i= 0; i < myDataReader.FieldCount; i++)
{
Console.WriteLine("{0} = {1} ",
myDataReader.GetName(i),
myDataReader.GetValue(i).ToString());
}
Console.WriteLine();
}
}
}
static void ShowConnectionStatus(SqlConnection cn)
{
Console.WriteLine("***Info about your connection***");
Console.WriteLine("Database location: {0}", cn.DataSource);
Console.WriteLine("Database name: {0}", cn.Database);
Console.WriteLine("Timeout: {0}", cn.ConnectionTimeout);
Console.WriteLine("Connection state: {0}", cn.State.ToString());
}
}
Thanks in advance!!