当我把我的MySqlConnection放在其他类中并从我的其他表单调用它时,我如何创建MySqlCommand

本文关键字:其他 我的 何创建 MySqlCommand 创建 MySqlConnection 表单 调用 | 更新日期: 2023-09-27 18:27:30

优秀的程序员。任何人都可以在 c# 中帮助我。每当我想插入或执行任何查询时,我如何从我的其他窗体调用这个类。

这是我名为DBConnection的类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
namespace WindowsFormsApplication2
{
    class DBConnector
    {
        private MySqlConnection con;
        private String server;
        private String db;
        private String uid;
        private String password;
        public DBConnector()
        {
            Initialize();
        }
        private void Initialize()
        {
            server = "localhost";
            db = "test";
            uid = "root";
            password = "";
            String connectionString;
            connectionString = "SERVER=" + server + ";" + "DATABASE=" +
            db + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
            con = new MySqlConnection(connectionString);
        }
        public void DatabaseConnector()
        {
            server = "localhost";
            db = "test";
            uid = "root";
            password = "";
            String connectionString;
            connectionString = "SERVER=" + server + ";" + "DATABASE=" +
            db + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
            con = new MySqlConnection(connectionString);
        }
        public void OpenCon()
        {
            con.Open();
        }
        public void CloseCon()
        {
            con.Close();
        }
    }
}

这是我的表单按钮类,每当我按下按钮时。然后它将查询。我的问题是如何调用con = new MySqlConnection(connectionString);来创建MySqlCommand。请帮忙。我很困惑。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace WindowsFormsApplication2
{
    public partial class FrmAdd : Form 
    {
        public FrmAdd()
        {
            InitializeComponent();
        }
        private void FrmAdd_Load(object sender, EventArgs e)
        {
        }
        private void button1_Click(object sender, EventArgs e)
        {
            var mycon = new DBConnector();
            try
            {
                mycon.OpenCon();
                MessageBox.Show("Well done!");
            }
            catch (MySqlException  ex)
            {
                MessageBox.Show("You failed!" + ex.Message);
            }

            string query = "INSERT INTO info values (1,'John','Mac','P',21)";

        }
    }
}

当我把我的MySqlConnection放在其他类中并从我的其他表单调用它时,我如何创建MySqlCommand

为了访问另一个类中的MySqlConnection对象,您需要将DbConnector类标记为internalpublic,即它应该变成:

public class DbConnector{
    ...
}

您还需要在属性本身上设置访问修饰符,并添加如下所示的 getter:

public MySqlConnection con { get; }

然后,在实例化DbConnector之后的FrmAdd,您可以使用数据库对象,如 mycon.con.SomeMethod()

另外,如果它们做同样的事情,为什么您有两种方法DatabaseConnector()Initialize()

相关文章: