[C#] Save MySQL Connection
#1

Hey guys, how can i save a MySql connection that is Read by a Textbox?

2

 

Uploaded with 2

#2

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);
}
}

#3

Another option would be to save settings in the windows registry

More details about that here : 2

#4

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;
}
}



Forum Jump:


Users browsing this thread: 1 Guest(s)