C# Throwing away login user when connecting to DB
Posted: 12 Mar 2009, 12:54
I am currently sitting with a very weird problem. I have a Windows service written in C#. Before I make the connection to the SQL server the user details are all there in the connection string but when it connects I get an error back saying that the login has failed for user <null>.
I added the event log entries to see what is going on so don't worry about them. Both of them throw me the correct connection string which looks like the below. Just without all the sensitive stuff.
Code: Select all
.
.
.
public System.Data.SqlClient.SqlConnection getSQLConnection(String sServer, String sDatabase, String sUsername, String sPassword)
{
String conStr = "Server=" + sServer + ";Database=" + sDatabase + ";User Id=" + sUsername + ";Password=" + sPassword + ";Trusted_Connection=True; MultipleActiveResultSets=true;";
EventLog.WriteEntry(sSource, "Returning Connection String = " + conStr, EventLogEntryType.Information);
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(conStr);
EventLog.WriteEntry(sSource, "Returning Connection = " + conn.ConnectionString, EventLogEntryType.Information);
return conn;
}
.
.
.
//Later on in the code
_sqlConnection_ = getSQLConnection(Properties.Settings.Default.SQLServer, Properties.Settings.Default.SQLDatabase, Properties.Settings.Default.SQLUser, Properties.Settings.Default.SQLPassword);
_sqlConnection_.Open();
System.Data.SqlClient.SqlCommand comm = _sqlConnection_.CreateCommand();
comm.CommandText = Properties.Settings.Default.SQLQuery;
comm.CommandType = CommandType.Text;
Code: Select all
"Server=127.0.0.1;Database=stagingtable;User Id=*****;Password=*****;Trusted_Connection=True; MultipleActiveResultSets=true;"