| |

VerySource

 Forgot password?
 Register
Search
View: 1063|Reply: 7

Given a connection string, find the username and password in the connection string?

[Copy link]

2

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-12-23 09:00:01
| Show all posts |Read mode
Do you have to decompose the string? The details are as follows:
public static string sqlConnectionString = "packet size=4096;user id=sa;data source=" + "." + ";persist security info=True;initial catalog=plan;password=aaa";

Now I want to find the username and password in sqlConnectionString and assign them to the variables username and pwd

How to achieve it?
Reply

Use magic Report

0

Threads

21

Posts

16.00

Credits

Newbie

Rank: 1

Credits
16.00

 China

Post time: 2020-12-23 09:15:01
| Show all posts
string str = "packet size=4096;user id=sa;data source=ddd;persist security info=True;initial catalog=plan;password=aaa";
Match m = Regex.Match(str,@"user id=(\w+);.*password=(.*)[;]?");
string username = m.Groups[1].Value;
string pwd = m.Groups[2].Value;
Reply

Use magic Report

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 China

Post time: 2020-12-23 09:30:01
| Show all posts
Regular expression:
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-12-23 09:45:01
| Show all posts
With ";", then with "="
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-12-23 10:30:02
| Show all posts
public static string sqlConnectionString = "packet size=4096;user id=sa;data source=" + "." + ";persist security info=True;initial catalog=plan;password=aaa";

plan 1.
string[] str = sqlConnectionString.Split( new char[]{'=',':'});
for(int i = 0;i<str.Lenght;i++
{
  switch(str[i])
  {
    case "user id":
        username=str[i+1];i++;break;
    case"password":
        pwd=str[i+1];i++;break;
    default:
    break;
  }
}

Scenario 2.
    username = "user id=";
    pwd="password=";
    int pos1 = sqlCOnnectionString.Find(username);
    int pos2 = sqlCOnnectionString.Find(";",pos1);
     username = sqlCOnnectionString.SubString(pos1+username.Length,pos2);
   pwd....
Option 3.
Regular // MSDN is not there, I can't write it out
Reply

Use magic Report

1

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-12-23 10:45:01
| Show all posts
DbConnectionStringBuilder builder = new DbConnectionStringBuilder();
builder.ConnectionString = sqlConnectionString;
string username = builder["User ID"];
Reply

Use magic Report

0

Threads

15

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-12-23 11:00:01
| Show all posts
coldfall() Reputation: 93
DbConnectionStringBuilder builder = new DbConnectionStringBuilder();
builder.ConnectionString = sqlConnectionString;
string username = builder["User ID"];

This is the easy way
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-12-23 11:15:01
| Show all posts
coldfallis the correct solution.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list