如何使连接对象可公开访问 [英] how to make a connection object publicly accessible

查看:72
本文介绍了如何使连接对象可公开访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个数据迁移工具,并在其中一个表单中(允许您连接到源数据库的表单)我在按钮的单击事件中有以下代码连接到源数据库



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

      }





现在我遇到的问题是,conn .ConnectionString属性必须在整个程序中公开访问,因为在migratebutton click事件中,我必须在编写迁移代码时调用它。任何人都知道如何将 conn 对象及其 ConnectionString 属性公开。

注意:收到连接参数的价值来自表单中的文本框值

注意:我正在连接到SYBASE数据库



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

推荐答案

最佳做法是收集所有数据库操作一个类并在类中设置connectionstring。从那以后,您可以在整个程序中调用您在类中定义的方法。这可能是您想要的方式,但您会发现编写类并在那里执行所有数据库操作要容易得多。您可以将该类重用于其他程序,当您的数据库发生更改时,您只需要查看该类以修复或扩展您的程序



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

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()
     {
        ...
        ...
     }
  }


这篇关于如何使连接对象可公开访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆