September 27, 2007 at 8:48 am
Hi,
This is what I have in my code:
static void Main(string[] args)
{
// Create a new SqlConnectionStringBuilder and
// initialize it with a few name/value pairs.
SqlConnectionStringBuilder builder =
new SqlConnectionStringBuilder(GetConnectionString());
builder["Trusted_Connection"] = true;
Console.WriteLine(builder.ConnectionString);
SqlConnection connection = new SqlConnection(builder.ConnectionString);
Console.WriteLine("Press Enter to finish.");
Console.ReadLine();
}
private static string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file.
return "Server=.\\SQLEXPRESS;Integrated Security=SSPI;" +
"Initial Catalog=CDStore";
}
have looked at many posts, but none of them have been helpful. My application and my database are on the same machine. I have tried giving the actual name of the Server, and the complete path of the database, but to no avail.
Here is the error I am getting.
connection.ServerVersion threw an exception of type System.InvalidOperationException.
Someone in another forum suggested that the string builder might be a problem, but anyone who connects to a database, knows that this is not where the error is. I have been connecting to DB2 database using all sorts of connection strings, never received this error.
I would appreciate any help in this regard.
Thanks
September 27, 2007 at 10:51 am
Found the solution, the problem had to do with the connection. Actually I was not opening the connection, and you do not get the Version till the connection is open. So now the code is as under ..... and it works .
static void Main(string[] args)
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=CDStore;Integrated Security=true");
conn.Open();
Console.WriteLine("Press Enter to finish.");
Console.ReadLine();
conn.Close();
}
Thanks,
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply