开发者

How to validate the connection string comming from a text file

开发者 https://www.devze.com 2023-01-22 12:49 出处:网络
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

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;  
            S开发者_运维问答erverConnection 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=***********


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消