c# winforms ввод параметров подключения к базе данных через форму
внутри программы существует два рабочих класса подключения к базе данных. когда IP адрес сервера меняется самом классе подключения нужно заново ввести новый IP адрес и заново компилировать программу. поэтому создал новую форму ввода параметров подключения. подключения происходит но дальше в формах возникает ошибка.
код формы ввода параметров подключения
public partial class Constringg : Form
{
public Constringg()
{
InitializeComponent();
}
private void Constringg_Load(object sender, EventArgs e)
{
}
private void buttonConnection_Click(object sender, EventArgs e)
{
string connectionString = string.Format("DataSource={0}; Port={1}; Initial Catalog={2}; User ID={3}; Password={4}", hostTextbox.Text, portTextbox.Text, databaseTextbox.Text, usernameTextobx.Text, passwordTextbox.Text);
try
{
SqlHelper helper = new SqlHelper(connectionString);
if (helper.IsConnection)
MessageBox.Show("connection succeeded");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Form1 fm1 = new Form1();
fm1.Show();
}
}
класс отвечающий за подключения к базе данных
class ConDatabase
{
public static MySqlConnection GetDBConnection()
{
Constringg stringCon = new Constringg();
string host = stringCon.hostTextbox.Text;
int port = Convert.ToInt32(stringCon.portTextbox.Text);
string database = stringCon.databaseTextbox.Text;
string username = stringCon.usernameTextobx.Text;
string password = stringCon.passwordTextbox.Text;
return Constring.GetDBConnection(host, port, database, username, password);
}
}
класс Constring
class Constring
{
public static MySqlConnection
GetDBConnection(string host, int port, string database, string username, string password)
{
// Connection String.
String connString = "Server=" + host + ";Database=" + database
+ ";port=" + port + ";User Id=" + username + ";password=" + password;
MySqlConnection con = new MySqlConnection(connString);
return con;
}
}