0

I'm using System.Data.SqlClient.SqlConnectionStringBuilder to pull apart a connection string we save when loading data. In theory (although not yet in practice), the user could load the OM from one database and save to another. As the connection string is saved into the model on loading, I can compare it on saving:

Dim currentDB As New System.Data.SqlClient.SqlConnectionStringBuilder(DbF.ConStr)
Dim fileDB As New System.Data.SqlClient.SqlConnectionStringBuilder(P.ConStr)

If currentDB.DataSource <> fileDB.DataSource 
   OrElse currentDB.InitialCatalog <> fileDB.InitialCatalog Then

My question: is it also possible to load the OM from an Excel file, in which case the "connection string" saved into the OM will be the path to the file?

Is there a way to test the string to see if it is a database connection string before trying to pass it into SqlConnectionStringBuilder? That method throws, and while I could use Try around it and solve the problem, I am wondering if there is a canonical method of testing "is this string a connection string"?

2
  • If you can exclude SQl Express using LocalDb, then AFAIK a valid connection string for SqlClient to a SQL Server Database will either contain Server= and Database= or Data Source= and Initial Catalog=. However there are very many database systems out there. You may get false positives: there may be valid connection strings to other database systems that also contain these terms, but which won't work with SqlClient. I have only ever connected to MSSQL, PostgreSQL, Oracle, MySQL and MS Access. This is a very small subset. Commented Apr 22 at 19:12
  • Perhaps testing for a valid path is a better approach? All those OR clauses seems ripe for error. Commented Apr 23 at 20:25

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.