none
SQL Server 2005 (express edition) ServerVersion error on Connection Open RRS feed

  • Question

  •  

    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.

     

    I would appreciate any help in this regard.

     

    Thanks

    Thursday, September 27, 2007 12:08 AM

Answers

  • Thanks Avinob, even though I am using VS2005, 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 Smile.

     

    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 once again.
    Thursday, September 27, 2007 4:48 PM

All replies

  • hi saania

    can u tell me r u using vs2005 or 2003 for ur devlopment. if ur using vs2005 then from the serve explorer add the database to the project then go to it's propeties and find the connection strig format.Oterwise

    Insted server try using "datasource ".

    Happy coding

    Avinob

    Thursday, September 27, 2007 4:14 PM
  • Thanks Avinob, even though I am using VS2005, 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 Smile.

     

    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 once again.
    Thursday, September 27, 2007 4:48 PM