Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 15-Oct-13 23:43pm
v2

1 solution

Best practice is to collect all your database actions in a class and set the connectionstring in the class. From there on you can call the methods you define in the class throughout the program. It is possible the way you want to, but you'll find it's much easier to write a class and do all database actions there. You can reuse the class for other programs and when your database changes you only have to look at the class to repair or extend your program

C#
namespace migrationtool
{

  public partial class migrationtool : Form
  {
    private databaseactions db;

    public migrationtool()
    {
      InitializeComponent();
      db = new databaseactions();
    }
    ...
    ...

    private void src_btn_connect_Click(object sender, EventArgs e)
        {
           ...
           ...
           bool result = db.Open(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));
          
           if (result)
             MessageBox.Show("Succeeded");
           else
             MessageBox.Show("Error opening database");
        }

    private void DoSomething()
    {
      DataTable dt = new DataTable();
      if (db.IsOpen())
         dt = db.FetchData();
    }

  }
  class databaseactions
  {
     private SqlConnection conn;

     public bool Open(string sqlConnectionString)
     {
        conn = null;
        try
        {
           conn = new SqqlConnection();
           conn.ConnectionString = sqlConnectionString;
           conn.Open();
           return true;
        }
        catch
        {
           return false;
        }
     }

     public bool IsOpen()
     {
            try
            {
                return (conn.State == System.Data.ConnectionState.Open);
            }
            catch
            {
                return false;
            }
        }
     }

     public DataTable FetchData()
     {
        ...
        ...
     }
  }
 
Share this answer
 
v2
Comments
Uzoma Umekwe 16-Oct-13 6:59am    
Hi Mart, Please if you dont mind can you write some sample code to make it easier to understand u?
Mart Rijkers 16-Oct-13 7:39am    
I just added sample code
Uzoma Umekwe 17-Oct-13 10:10am    
thanks Mart..this was very helpful

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900