05-23-2012, 06:29 PM
|
[C#] Save MySQL Connection
|
|
05-23-2012, 06:31 PM
like this for example public void Write() { try { TextWriter tr = new StreamWriter("Settings.cfg"); tr.WriteLine("## MYSQL"); tr.WriteLine("SQL_HOST=" + textBox_host.Text); tr.WriteLine("SQL_DBASE_CHAR=" + textBox_Chrdatabase.Text); tr.WriteLine("SQL_DBASE_DATA=" + textBox_Datadatabase.Text); tr.WriteLine("SQL_USER=" + textBox_User.Text); tr.WriteLine("SQL_PASSWORD=" + textBox_password.Text); tr.Close(); } catch (Exception e) { MessageBox.Show(e.Message); } } public void Read() { try { TextReader tr = new StreamReader("Settings.cfg"); string line; while ((line = tr.ReadLine()) != null) { if (line.Contains("#") || line.Length == 0) { } else { string[] values = line.Split(=); foreach (string v in values) { if (values[0] == "SQL_HOST") host = values[1]; if (values[0] == "SQL_DBASE_CHAR") database = values[1]; if (values[0] == "SQL_DBASE_DATA") database1 = values[1]; if (values[0] == "SQL_USER") user = values[1]; if (values[0] == "SQL_PASSWORD") password = values[1]; } } } textBox_Chrdatabase.Text = database; textBox_Datadatabase.Text = database1; textBox_host.Text = host; textBox_password.Text = password; textBox_User.Text = user; tr.Close(); } catch (Exception e) { MessageBox.Show(e.Message); } }
05-23-2012, 06:39 PM
05-23-2012, 06:41 PM
Save it to a file. class ConfigManager { public struct config { public string key; public string value; }; public static List ConfigList = new List(); public void readList(string filename) { StreamReader sr = new StreamReader(filename); string line; while ((line = sr.ReadLine()) != null) { if (line[0] == #) { continue; } string[] data = line.Split(=); if (data.Length == 2) { config cfg = new config(); cfg.key = data[0]; cfg.value = data[1]; ConfigList.Add(cfg); } } } public void saveFile(string filename) { StreamWriter sw = new StreamWriter(filename); for (int i = 0; i < ConfigList.Count; i++) { config cfg = new config(); cfg = ConfigList[i]; sw.WriteLine(cfg.key+"="+cfg.value); } } public string getString(string key) { for (int i = 0; i < ConfigList.Count; i++) { config cfg = new config(); cfg = ConfigList[i]; if (cfg.key == key) return cfg.value; } return ""; } public int getInt(string key) { for (int i = 0; i < ConfigList.Count; i++) { config cfg = new config(); cfg = ConfigList[i]; if (cfg.key == key) return int.Parse(cfg.value); } return 0; } } |
|
« Next Oldest | Next Newest »
|
Users browsing this thread: 1 Guest(s)

