如何在C#中将数据从一种形式传输到另一种形式?

时间:2018-12-01 21:37:53

标签: c# winforms connection

我想通过其他形式从我的dbconnect类访问openConnection,但是它不能正常工作。我必须将整个Oopenconnection复制粘贴到其他页面上,以便他们访问它。

dbconnect类

 private void display_record_Click(object sender, EventArgs e)
    {
        DB_Connect connect = new DB_Connect(); // I believe this part does not work

        string show_query = "SELECT * FROM testing_tb"; 

        if (connect.OpenConnection() == true)
        {

            using (MySqlCommand cmd_DB = new MySqlCommand(show_query, connection))
            {

                try
                {

                    using (MySqlDataReader reader = cmd_DB.ExecuteReader())

                        if (reader.HasRows)

                        {

                            dt = new DataTable();
                            dt.Load(reader);
                            dataGridView1.DataSource = dt;

                        }
                        else
                        {
                            MessageBox.Show("No data record detected");
                        }

                    connect.CloseConnection();

                }
                catch (Exception ex)

                {
                    MessageBox.Show("Could not connect to database!\n" + ex, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }// end of catch

            } // end of MySQLCommand

        } // end of connection check
    }

MyForm 2 点击时,数据将显示在dataGridView上

dtconnect class

我想访问我的public bool OpenConnection(){}中的数据,这样就不必将/*/e:Cubes/e:Cube/e:Cube粘贴到我要打开连接的每种新形式中。

错误:无法连接到数据库,连接必须有效且已打开,等等。

1 个答案:

答案 0 :(得分:0)

如果您确实希望在任何地方都可以使用该连接,则可以使用以下方法:

public static class Connection
{
    private const string ConnectionString = "YOUR CONNECTION";
    public static System.Data.SqlClient.SqlConnection Conn { get; private set; } = null;

    public static void Open()
    {
        try
        {
            Conn = new System.Data.SqlClient.SqlConnection(ConnectionString);
            Conn.Open();
        }
        catch { System.Windows.MessageBox.Show("Can't connect to the server, please check if you have an internet connection."); }
    }

    public static void Close() { Conn.Close(); }
}

不建议您这样做,因为您的连接将一直处于打开状态,因此您应该这样做:

    public static class Connection
{
    private const string ConnectionString = "YOUR CONNECTION";
    public static System.Data.SqlClient.SqlConnection Conn { get; private set; } = null;

    public static void Create() { Conn = new System.Data.SqlClient.SqlConnection(ConnectionString); }
    public static void Open()
    {
        try { Conn.Open(); }
        catch { System.Windows.MessageBox.Show("Can't connect to the server, please check if you have an internet connection."); }
    }

    public static void Close() { Conn.Close(); }
}

然后在需要时打开和关闭连接。 因此,基本上,当使用查询打开连接时,将使用Connection.Open()Connection.Close()。现在,如果您正在使用表格,可以从任何地方访问它。

以下是有关连接状态的链接:https://softwareengineering.stackexchange.com/questions/142065/creating-database-connections-do-it-once-or-for-each-query