views:

21

answers:

1

there is a way by which we parse any time input given by user...

here i am taking the input of a connection string from a text file

i am supposed to assume that the user who keeps this string in the text file can be trusted.

but what if he makes a typo a mistake unknowingly, that will cause the coming code to cause exception,

i would like a way to check the string for the correct format, like parse it some way to see if it is the way an connection tring should be, and then possibly use that parsed result.

edit

as requested the sample code i using, and the connection the sqladaptor using

//Connect to a remote instance of SQL Server. 
            Server srv;  
            ServerConnection conContext = new ServerConnection();
            conContext.ServerInstance = @"A-63A9D4D7E7934\SECOND";
            conContext.LoginSecure = false;
            conContext.Login = "sa";
            conContext.Password = "two";
            srv = new Server(conContext);
            Response.Write(srv.Information.Version);





            //Data Source=A-63A9D4D7E7834\SECOND;Initial Catalog=replicate;User ID=sa;Password=***********
+1  A: 

A regular expresion can't validate an incorrect password, user name, or host name, so it is possible for your user to generate a valid string that will still cause an exception.

Also, you can't build a regex that will cover every possible combination of connection arguments.

The only robust solution, would be to wrap the connection code in a try/catch block and provide an appropriate error message.

mikerobi
but is there a way for the above sample connection string,? yes i know try catch best way. but just want to learn