I have the following string
string ConnectionString= "data source="MyLocalSQLServer"
;Initial Catalog=aspnetdb;
;Integrated Security=SSPI
;persist sercuity info=false
;UID=sa;
;PWD=asdasd;
;ProviderName="System.Data.SqlClient" what I need to do is extract all the relevant bits and put in a variable . How can I do it EG
string ServerName= string dbName= strUID= etc .
I have tried using "split" but not with much success anyIdeas
thanks in advance

How can I extract a string from a string?
Pentalon
Hi,
The split method is new for .net 2.0.
Here is an example:
string ConnectionString= "data source="MyLocalSQLServer"
;Initial Catalog=aspnetdb;
;Integrated Security=SSPI
;persist sercuity info=false
;UID=sa;
;PWD=asdasd;
;ProviderName="System.Data.SqlClient" ;
char[] splitter= {';'}
string[] result = ConnectionString.split(splitter) ;
Of course you have to split every item from the resulted array using = character.
Regards,
Lajos
Maya__
If you step through the code using the debugger you should see that spt1 looks something like this:
- spt1 {Dimensions:[7]} string[]
[0] "data source= MyLocalSQLServer" string
[1] "Initial Catalog=aspnetdb" string
[2] "Integrated Security=SSPI" string
[3] "persist sercuity info=false" string
[4] "UID=sa" string
[5] "PWD=asdasd" string
[ 6 ] "ProviderName=System.Data.SqlClient" string
[3] should be "security"!
We then use spt2 on each of the strings in spt1 such that we have two dimensions:
- spt2 {Dimensions:[2]} string[]
[0] "data source" string
[1] " MyLocalSQLServer" string
spt2 will never have more than two dimensions, hence the index out of bounds error you are getting.
Here's another example:
string connectionString = "data source= MyLocalSQLServer"
+
";Initial Catalog=aspnetdb"+
";Integrated Security=SSPI"+
";persist sercuity info=false"+
";UID=sa"+
";PWD=asdasd"+
";ProviderName=System.Data.SqlClient"; string[] spt1 = connectionString.Split(new Char[] { ';' }); string serverName; string dbName; string userName; string passWord; string provider; foreach (string s in spt1){
string[] spt2 = s.Split(new Char[] { '=' }); if (spt2[0] == "data source") serverName = spt2[1]; if (spt2[0] == "Initial Catalog") dbName = spt2[1]; if (spt2[0] == "UID") userName = spt2[1]; if (spt2[0] == "PWD") passWord = spt2[1]; if (spt2[0] == "ProviderName") provider = spt2[1];}
HTH
Peter Sarrett
string ConnectionString= "data source="MyLocalSQLServer"
;Initial Catalog=aspnetdb;
;Integrated Security=SSPI
;persist sercuity info=false
;UID=sa;
;PWD=asdasd;
;ProviderName="System.Data.SqlClient"
to
string ConnectionString= "data source=" + strConnectionServer
+ " ;Initial Catalog=" + strAspnetdb;
+ " ;Integrated Security=" + strSSPI
+ " ;persist sercuity info=" + boolFalse
+ " ;UID=" + strSa;
+ " ;PWD=" + strAsdasd;
+ " ;ProviderName=" + System.Data.SqlClient ;
Keep the strings that you need to change as dynamic by concatonating them. The SDK also has great string manipulations that allow you to split, capture substrings, count character places, and search for specific strings within a string too. It all starts with 'string MyString.<open list of options>'
leovernazza
An interesting request.
Split can do the job, here's an example of how:
string
provider = ""; string connectionString = "provider=Microsoft.JET.OLEDB.4.0; " + "data source = 'database.mdb'"; string[] spt1 = connectionString.Split(new Char[] { ';' }); foreach (string s in spt1){
string[] spt2 = s.Split(new Char[] { '=' }); string left = spt2[0]; // e.g. provider string right = spt2[1]; // e.g. Microsoft.JET.OLEDB.4.0 if (left == "provider"){
provider = right;
}
}
HTH
Dan Dieckmann
Steve Hirth
While not very complete, I’d take a look at this Regular Expression example and expand on it to do what you are looking for: http://www.csharp-online.net/csow/index.php title=Parse_connection_string
numbers65
Thanks for all your replies.
Just to clarify.I am writing a small app and I have a scenario where i need to disect the connection string .
Also I always wanted to write a function where you pass a string full of delimiters and get all the the words between delimeters.Is possible to write a generic function
I have another place in a program where i need to disect a string full of delimeters,therefore some generic example would be great.
I have tried the follwoing but index is out of array.
private void DisectString()
{
string connectionString = "data source= MyLocalSQLServer"
+";Initial Catalog=aspnetdb"
+ ";Integrated Security=SSPI"
+ ";persist sercuity info=false"
+";UID=sa"
+";PWD=asdasd"
+";ProviderName=System.Data.SqlClient";
string[] spt1 = connectionString.Split(new Char[] { ';' });
foreach (string s in spt1)
{
string[] spt2 = s.Split(new Char[] { '=' });
string serverName = spt2[0];
string dbName = spt2[1];
string userName = spt2[2];
string passWord = spt2[3];
string provider = spt2[4];
}
}
any more ideas how to disect a string with delimiters
thanks a lot to all the people who replied.
riet
Thanks a lot.Sorry yesterday I was in a rush and I didnt look properly.
Your code works .Thanks.Now I see how you used the split Function and I have used already in another place where i needed to disect a string.
Thanks for your time
john ewing