将DB的连接插入到函数内的其他每个表单中

时间:2018-01-29 05:23:54

标签: c# sql-server

namespace PCJ_System
{
    class DB_CONNECTION
    {

        public SqlConnection getConnection()
        {
            SqlConnection conn = null; ;
            try
            {
                conn = new SqlConnection("data source= DESKTOP-LKEG8FM\\SQLEXPRESS;initial catalog= PCJ_DB ; Integrated Security=True;");
                conn.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can't Open Connection !" + ex);
            }
            return conn;
        }

    }
}

这是我的表单代码:public partial class Form1:这下面的代码正常工作,但我再次输入sqlconnection。这应该是错误的编码方式。

namespace PCJ_System
{
    public partial class Form1 : Form
    {
         SqlConnection conn;
        SqlCommand cmd;
      //  SqlDataReader dr;

        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string str = "server = DESKTOP-LKEG8FM\\SQLEXPRESS;initial catalog= PCJ_DB ; Integrated Security=True;";/again and again i am calling the=is to every other forms is it the correct way /
            SqlConnection conn = new SqlConnection(str); 
          //  DB_CONNECTION x = new DB_CONNECTION();
            conn.Open();
            string GetData = "Select [FC_Rate] from Forcur where FC_TYPE ='" + comboBox1.Text + "' ";
            cmd = new SqlCommand(GetData, conn);
            var returnValue = cmd.ExecuteScalar();
            textBox1.Text = returnValue.ToString();
            conn.Close();

        }

    }
} 

如何将Dbconnection调用到每个功能。

请帮忙。

2 个答案:

答案 0 :(得分:0)

您可以采取哪种方法来避免重复代码:创建BaseForm并在其中添加常规代码然后所有表单都应该继承BaseForm

public abstract class BaseForm:Form{
        public SqlConnection getConnection()
        {
            SqlConnection conn = null; ;
            try
            {
                conn = new SqlConnection("data source= DESKTOP-LKEG8FM\\SQLEXPRESS;initial catalog= PCJ_DB ; Integrated Security=True;");
                conn.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can't Open Connection !" + ex);
            }
            return conn;
        }
}

并且您的表单应该像这样更改:

public partial class Form1 : BaseForm
{
    SqlCommand cmd;
  //  SqlDataReader dr;

    public Form1()
    {
        InitializeComponent();
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string str = "server = DESKTOP-LKEG8FM\\SQLEXPRESS;initial catalog= PCJ_DB ; Integrated Security=True;";/again and again i am calling the=is to every other forms is it the correct way /


        string GetData = "Select [FC_Rate] from Forcur where FC_TYPE ='" + comboBox1.Text + "' ";
        cmd = new SqlCommand(GetData, getConnection());
        var returnValue = cmd.ExecuteScalar();
        textBox1.Text = returnValue.ToString();
        conn.Close();

    }

}

答案 1 :(得分:0)

替换您的代码:

string str = "server = DESKTOP-LKEG8FM\\SQLEXPRESS;initial catalog= PCJ_DB ;Integrated Security=True;";
/again and again i am calling the=is to every other forms is it the correct way /
SqlConnection conn = new SqlConnection(str); 
//  DB_CONNECTION x = new DB_CONNECTION();
conn.Open();

以下内容:

DB_CONNECTION x = new DB_CONNECTION();
SqlConnection conn = x.getConnection();