Share via


Openning SQL connection from C# Error ServerVersion

Question

Thursday, December 11, 2008 8:57 PM

Hi,

 

I have a problem, I have read articles and I can't figure it out, I need some help.

 

The following code worked fine for me untill recently

 

SqlConnection cn = new SqlConnection("Data Source=localhost;Initial Catalog=HaDataMart;Integrated Security=True");

cn.Open();

 

Now i get the following error,

 

+  ServerVersion 'cn.ServerVersion' threw an exception of type 'System.InvalidOperationException' string {System.InvalidOperationException}

This seems very simple, I use windows authentication and the user has full writes to the SQL server.

 

If anyone has any suggestions please let me know. I have run out of ideas.

 

Thank You,

Greg

 

All replies (6)

Wednesday, March 11, 2009 9:03 PM âś…Answered

Hi, thanks for your quick reply!

The connection string is actually stored in a Config DB from where the app retrieve and then used it to open connection. 

I step into the code, and got the error something like Serverversion error, it happens around the code of this:
at System.Data.SqlClient.SqlConnection.GetOpenConnection()
   at System.Data.SqlClient.SqlConnection.get_ServerVersion()

This means the connection is not open at all, right?

The connection string that's finally passed was got in this statement:

strConnect = "data source="+datasource + ";initial catalog=" + initialcat + ";user id="+userid + ";password="+password;

Could there be a problem? But the current 2003 App can work with this well.

This connection string is a remote data soure from another party so I can not find out what sql server version it is now, maybe tomorrow...After read your earlier post, I am thinking this could be VS(.NET Framework) and SQL Server version problem, could it be?

thanks, Jin


Friday, December 12, 2008 11:35 AM

If you are cataloging this assembly into HADataMart, your connection string should simply be "Context Connection=true". If not, check to make sure you cataloged the assembly with the EXTERNAL_ACCESS permission set.

-- Thanks, Kent/DevelopMentor


Friday, December 12, 2008 5:38 PM | 1 vote

Where are you opening the connection from:
from an application outside SQL Server or from a SQLCLR assembly running inside SQL Server?

If you are opening it from a SQLCLR assembly, have a lok at Kent's answer in this thread.

If you open it from an application outside of SQL; This forum - as its title says - is about CLR code running inside the SQL Server engine, so your question should probably be answered in some other forum.

Niels


Sunday, December 14, 2008 8:43 PM

Hello,

Please, could you provide the edition and Version ( 2000/2005/2008 ) of your Sql Server instance ?

Please, could you tell us whether your instance has not been upgraded from 2005 to 2008 ?

Please,could you provide the exact and complete error message  ?

For a complete error message, please, could you have a look on this link?
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlexception.aspx

try
{
    SqlConnection cn = new SqlConnection("Data Source=localhost;Initial Catalog=HaDataMart;Integrated Security= True");
    cn.Open();
}
catch ( SqlException ex )
{
    Console.WriteLine("Error {0} ErrorCode {1} LineNumber {2}  Number {3}",ex.Message,ex.ErrorCode,ex.LineNumber,ex.Number);
    foreach ( SqlError err in ex.Errors )
    {
        Console.WriteLine("** Error : {0} LineNumber {1} Number {2},err.Message,err.LineNumber,err.Number;
    }
    if ( ex.InnerException != null )
        {
            Console.WriteLine("Inner1 : {0}",ex.InnerException.Message;
        }
}

With this code , you would be able to find the exact and complete error message.

A small remark : with an application generated with a VC# 2005 , you can't open an instance 2008 ( you have to regenerate the executable with the "good" dll ) . Please,could you tell us with what version of Visual Studio the code has been developped ).
If it is a problem VC#2005 <==> Sql Server 2008, i have frequently obtained a message like "non supported version".

We are waiting for your feedback to try to help you more efficiently

Have a nice day

PS : the remark of nielsb is clever : i am not sure that thread should be here ( it is rare to use a SqlConnection in CLR code )Please remember to click 'Mark as Answer' on the post that helped you. Unmark if it provides no help


Wednesday, March 11, 2009 7:52 PM

Hi, Normand,

I have an application, compiled in vs2003 and it can access SQL server very well. Now we upgrade the app in VS2005, it can not open the database connection anymore. The connection string is like this: {Data Source=;Initial Catalog=;User ID=;Password=}. I can not tell what version of this db, but guess it could be sql2000(?).  Can you please tell what could be the problem here?

thanks, JinDD


Wednesday, March 11, 2009 8:22 PM

Hello JinDD,

VS2003 is using .Net Framework 1.1 and VS 2005 .Net Framework 2.0.
The syntax has changed because of the arrival of the System.Data.SqlClient namespace.
I have given up VS2003 in 2005 with the arrival of VC#2005 and Sql Server Express 2005 and i did not remember the problem i faced with this change, but i know that there is no problem to connect to a Sql Server 2000 with SqlConnection
More exactly, i am always using SqlConnectionStringBuilder to build the connection string ( IntelliSence is useful ).

Please, could you tell us the error you are getting ?

You should use a try/catch , and catching the SqlException ( especially the members Message,ErrorCode,Number,Source,TargetSite and StackTrace )

We are waiting for your feedback to try to help you more efficiently

Have a nice day

PS : you will find an example of treatement of SqlException here :
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlexception.number(VS.80).aspx