using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using System.IO; namespace ProList { public partial class MasterForm : Form { private SqlConnection conn; public MasterForm() { InitializeComponent(); } //readonly property that return the connection object public SqlConnection Conn { get { return conn; } } private void MasterForm_Load(object sender, EventArgs e) { //Establish a connection with the database //An external text file is used to store the connection string //so that when the host computer or the database is changed //the information can be updated by changing the text in the text file string connStr = File.ReadAllText("DatabaseConnectionString.txt"); try { conn = new SqlConnection(connStr); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } } |
where "DatabaseConnectionString.txt" is a text file that contains the connection string. I placed this file in the "Debug" folder of the program. The purpose of using an external file to store the connection string is when I need to switch the database after the program is deployed, I only need to edit the text file instead of the code. I also have the ChildForm class that inherits MasterForm.
The program compiles and runs without an error. But when I open the ChildForm design view in Visual Studio, the form visual elements do not appear. Instead there's a bunch of HTML codes and in the error window the following warning is generated:
"Could not find file 'C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\DatabaseConnectionString.txt'
This warning is not generated when I open the MasterForm design view, it only happens with its derived forms (classes).
Does anyone have any idea why this happens? Thanks.
Sam HobbsPosted Jan 17, 2010, 11:35 PM
Henry VuongPosted Jan 17, 2010, 9:24 PM
Henry VuongPosted Jan 17, 2010, 8:43 PM
I got it. Here's another question: How do I change the information at run time to switch the database, i.e. which file do I change? I don't want to recompile the program when this information is changed.
theLizardPosted Jan 17, 2010, 6:16 PM
Hi Henry
string path = Application.ExecutablePath;
path = path.Substring(0, path.LastIndexOf("\\")) + "\\DatabaseConnectionString.txt";
string connStr = File.ReadAllText(path);
What this will do is read the text file in the folder where the executable lives, as log as the text file is in the same folder you will be able to read it.
Sam HobbsPosted Jan 17, 2010, 6:14 PM
I don't understand why Visual Studio (VS) knows anything about the text file; I don't understand why the text file has any affect on the forms. I think that needs clarification; I assume no one else would understand.
Are you familiar with Settings? The Settings are designed to do exactly what you are doing, except Settings are easier to use since all the programming is done for you, including the setup-time modification of the data. In your project, there is probably a "Properties" folder; open that node and then double-click on "Settings.Settings". The settings are a table with fields for name, type, scope and value. You can set the type to Connection String. If you set the scope to Application, then the data is not modified by the user but can be modified by a setup program. In your program, you retrieve the connection string using: