I’m writing an application in Visual C++ using the .NET 3.5 Framework, connecting to a remote Microsoft SQL Server 2008 R2.
However, in order to be able to modify my database connection string without having to recompile constantly, I want to simply save the connection string to a single line text file and load it in during run-time.
Initially, for testing purposes, I had my connection string hardcoded into my query and it worked flawlessly. However, when I implemented a method to read a string in from a text file, I get:
A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or
was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections. (provider: SQL
Network Interfaces, error: 26 – Error Locating Server/Instance
Specified)
And I have no idea why. I’ve examined the string the program reads in during run-time and they are identical. Here’s the block of code pertaining to this issue:
StreamReader^ read_db_conn_string = File::OpenText(System::IO::Directory::GetCurrentDirectory() + "\\db_conn_string.txt");
String^ db_conn_string = read_db_conn_string->ReadLine();
read_db_conn_string->Close();
bindingSource_Shop->DataSource = GetData("Select * From Shop", db_conn_string);
dataGridView_Shop->DataSource = bindingSource_Shop;
Here is the GetData method:
DataTable^ GetData( String^ sqlCommand, String^ connectionString )
{
SqlConnection^ Connection = gcnew SqlConnection( connectionString );
SqlCommand^ command = gcnew SqlCommand( sqlCommand,Connection );
SqlDataAdapter^ adapter = gcnew SqlDataAdapter();
adapter->SelectCommand = command;
DataTable^ table = gcnew DataTable;
adapter->Fill( table );
return table;
}
To keep a working version in my code now, I kept the overloaded GetData method, and if I do it like this, the SQL connection works:
DataTable^ GetData(String^ sqlCommand)
{
String^ connectionString = "Data Source=SERVER\\SQLEXPRESS;Initial Catalog=Schedule;Persist Security Info=True;User ID=sa;Password=xxxxx";
SqlConnection^ Connection = gcnew SqlConnection( connectionString );
SqlCommand^ command = gcnew SqlCommand( sqlCommand,Connection );
SqlDataAdapter^ adapter = gcnew SqlDataAdapter();
adapter->SelectCommand = command;
DataTable^ table = gcnew DataTable;
adapter->Fill( table );
return table;
}
I’m afraid that it’s something really simple, but I just don’t have the insight to figure it out.
I notice that you’re escaping the backslash in the server name with double-backslash when the connection string is in the code, are you also doing this in the text file? If so, that may be causing the issue, the text file should use normal single backslashes.
Additionally, have you checked whether there is an invisible byte-order-mark at the start of your file (which may be added by some text editors when you save), which is being included in the string you read from it?
You could attempt to open the file in a hex-editor to determine whether there’s anything unexpected in the file.
It may be worth using the
connectionStringsorappSettingssections of the App.config, rather than reading from a text file, since this is one of the built-in ways to put configurable connection strings in your application.