我想从一个类访问另一个类的mysqlconnection对象

时间:2013-07-14 13:27:21

标签: c# mysql .net

代码A(CLASS)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace eBayERPSolution
{     
    public class dbconnection
    {           
        int flag = 0;

        public int Flag
        {
            get { return flag; }    
        }

        public dbconnection()
        {
            MySqlConnection con = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["mysqlconnect"].ConnectionString);

            try
            {
                con.Open();// I WANT TO ACCESS THIS CON OBJECT IN ANOTHER FORM
                flag = 1;
            }    
            catch (Exception e)
            {
                flag = 0;
                MessageBox.Show(e.Message );    
            }               
       }    
    }
}

代码B(表格)

public  void button1_Click(object sender, EventArgs e)
{

    dbconnection check = new dbconnection();
    if (check.Flag == 0)
    {
         MessageBox.Show("Connected");
    }

    try
    {
         string query = "INSERT INTO ebayerp_sales (invdate,invno,paisapayid,phonenumber,emailid,paymentrecdate,customername,buyerid,billingaddress,shippingaddress,shippingpatner,awbno,shippingdate,status) VALUES('" + invdate.Text + "','" + int.Parse(invno.Text) + "','" + int.Parse(paisapayidtbox.Text) + "','" + int.Parse(phonenumbertbox.Text) + "','" + emailidtbox.Text + "','" + paymentrecdatetbox.Text + "','" + customernametbox.Text + "','" + buyeridtbox.Text + "','" + billingaddresstbox.Text + "','" + shippingaddresstbox.Text + "','" + shippingpatnertbox.Text + "','" + awbnotbox.Text + "','" + shippingdatetbox.Text  + "','" + statustbox.Text  + "')";
         MySqlCommand cmd = new MySqlCommand(query,---------------);//I WANT TO ACCESS CON OBJECT THERE.

3 个答案:

答案 0 :(得分:1)

只需将MySqlConnection对象发布为dbconnection类的属性。

public class dbconnection
{
    MySqlConnection connection;
    int flag = 0;

    public int Flag
    {
        get { return flag; }    
    }

    public MySqlConnection Connection
    {
        get { return this.connection; }
    }

    public dbconnection()
    {
        this.connection = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["mysqlconnect"].ConnectionString);

        try
        {
            this.connection.Open();
            flag = 1;
        }    
        catch (Exception e)
        {
            flag = 0;
            MessageBox.Show(e.Message );    
        }               
   }    
}

...并从您的Windows窗体中使用它。

var myDbConnection = new dbconnection();
MySqlCommand cmd = new MySqlCommand(query, myDbConnection.Connection);

答案 1 :(得分:1)

现在,您的MySqlConnection对象在构造函数中声明为局部变量。相反,把它变成一个属性。

public class DbConnection
{
    ...

    public MySqlConnection Connection { get; set; }

    public DbConnection()
    {
        Connection = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["mysqlconnect"].ConnectionString);
        ....
    }

答案 2 :(得分:0)

我建议你创建一个帮助类(也就是包装器),就像这样或使用它 MySqlHelper。如果您愿意,也可以使用DbConnectionDbProviderFactory获取一些通用方法。