I am currently working on a data migration tool and in one of the forms (the form that allows you connect to the source database) I have the following code in the click event of the button to connect to the source database
private void src_btn_connect_Click(object sender, EventArgs e)
{
string src_dtsource = src_dtsource_txt.Text;
string src_host = src_host_txt.Text;
string src_port = src_port_txt.Text;
string src_dbname = src_dbname_txt.Text;
string src_uid = src_uid_txt.Text;
string src_pwd = src_pwd_txt.Text;
if ((src_dtsource_txt.Text == ""))
{
MessageBox.Show("Data source is empty", "Connection Parameters", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if(src_host_txt.Text == "")
{
MessageBox.Show("Host is empty", "Connection Parameters", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if(src_port_txt.Text == "")
{
MessageBox.Show("Port is empty", "Connection Parameters", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (src_dbname_txt.Text == "")
{
MessageBox.Show("Database Name is empty", "Connection Parameters", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (src_uid_txt.Text == "")
{
MessageBox.Show("Username is empty", "Connection Parameters", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (src_pwd_txt.Text == "")
{
MessageBox.Show("Password is empty", "Connection Parameters", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
SAConnection conn = new SAConnection();
conn.ConnectionString = String.Format("eng={0};uid={1};pwd={2};dbn={3};LINKS=tcpip(HOST={4};ServerPort={5});",src_dtsource, src_uid, src_pwd, src_dbname, src_host, src_port);
try
{
conn.Open();
MessageBox.Show("Connection to Source database succeeded", "Source Database Connection", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Source Database Connection", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
Now the problem I have is, the conn.ConnectionString property has to be accessible publicly throughout the program because in the migratebutton click event i have to call it when I write the code for the migration. Anyone has a clue how both the
conn object and its
ConnectionString Property can be made public.
NB: THE VALUES OF THE CONNECTION PARAMETERS ARE RECEIVED FROM TEXTBOX VALUES IN THE FORM
NB: I AM CONNECTING TO A SYBASE DATABASE