Hi guys, I got a few questions regarding connection strings.
Lets take this code as an example:
try {
strConnect = "";
dbConn = new MySqlConnection(strConnect);
dbConn.Open()
if (dbConn.State.ToString() != "Open")
{
MessageBox.Show("Could not open database connection");
return;
}
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
Assume the following setup on server:
1) server is localhost
2) database name is test
3) root user with password 'password'
In each of the test cases below, NO exceptions were thrown and the connection state is "Open"
strConnect = "";
strConnect = "server=;uid=;pwd=;database=";
strConnect = "server=localhost;uid=;pwd=;database=";
strConnect = "server=127.0.0.1;uid=;pwd=;database=";
strConnect = "server=;uid=root;pwd=;database=";
strConnect = "server=;uid=;pwd=;database=test";
In each of the cases below, an exception is thrown
strConnect = "server=foobar;uid=;pwd=;database=";
strConnect = "server=localhost;uid=;pwd=;database=foobar";
strConnect = "server=;uid=;pwd=;database=mytest"
strConnect = "server=localhost;uid=root;pwd=;database=test";
Why isn't Open throwing exceptions when the connecton string isn't fully qualified? To me it doesn't make sense to have a valid open connection on empty connection strings, supplying just a IP address, giving just a valid username, etc.
Lets take this code as an example:
try {
strConnect = "";
dbConn = new MySqlConnection(strConnect);
dbConn.Open()
if (dbConn.State.ToString() != "Open")
{
MessageBox.Show("Could not open database connection");
return;
}
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
Assume the following setup on server:
1) server is localhost
2) database name is test
3) root user with password 'password'
In each of the test cases below, NO exceptions were thrown and the connection state is "Open"
strConnect = "";
strConnect = "server=;uid=;pwd=;database=";
strConnect = "server=localhost;uid=;pwd=;database=";
strConnect = "server=127.0.0.1;uid=;pwd=;database=";
strConnect = "server=;uid=root;pwd=;database=";
strConnect = "server=;uid=;pwd=;database=test";
In each of the cases below, an exception is thrown
strConnect = "server=foobar;uid=;pwd=;database=";
strConnect = "server=localhost;uid=;pwd=;database=foobar";
strConnect = "server=;uid=;pwd=;database=mytest"
strConnect = "server=localhost;uid=root;pwd=;database=test";
Why isn't Open throwing exceptions when the connecton string isn't fully qualified? To me it doesn't make sense to have a valid open connection on empty connection strings, supplying just a IP address, giving just a valid username, etc.