c# - 寻找为 Sql 查询和 Windows 窗体创建帮助程序类的指导

标签 c# mysql sql

我会尽量让它变得简单。

我有一个带有SELECT、INSERT、DELETE 和 UPDATE (CRUD) 按钮的工作 GUI,我需要使用帮助程序类而不仅仅是运行代码在按钮等后面。

但我什至不知道如何开始编码。如果我在另一个类(class)工作,我不明白如何为按钮或标签编写操作代码。

我尝试执行“ClassNameHere: Form1”,但出现错误:

lblInfo is inacessible due to its protection level
cmbTable is inaccessible due to its protection level

我用谷歌搜索并尝试将类更改为 public 而不是 public 等无济于事。

所以我必须得到这段代码(这只是SELECT查询)

private void fillcomboBox()
{
    try
    {
        conn = new MySqlConnection(connstring);
        conn.Open();
        MySqlCommand myCommand = new MySqlCommand("SELECT * FROM person", conn);
        MySqlDataReader myReader;
        myReader = myCommand.ExecuteReader();
        cmbTable.Items.Clear();
        while (myReader.Read())
        {
            cmbTable.Items.Add(myReader["personID"] + " | " + myReader["firstName"] + " | " + myReader["lastName"] + " | " + myReader["address"] + " | " + myReader["phoneNumber"] + " | " + myReader["postCode"] + " | " + myReader["dateOfBirth"]);
        }
    }

    catch (Exception err)
    {//handle the error with a message
        lblInfo.Text = " Error reading the database.";
        lblInfo.Text += err.Message; ;
    }

    finally
    {

    }
}

在不同的类(助手类)中工作并链接到表单,以便它可以工作但不在按钮后面......我希望这是有道理的。 但正如我所说,我什至不知道如何开始编写代码。

我会喜欢任何输入,任何东西。

谢谢。

最佳答案

使一个方法从您的数据库查询中返回一个字符串列表,并将此方法放在另一个类中(例如称为 SQLHelper)。您可以将所有与 SQL 相关的方法放在这里。使返回类型独立于 SQL 特定类。就像字符串列表、特定的人对象等等。使这个 SQL 帮助程序类成为您的 Form 类的成员,并在需要时调用它的方法。使用它的返回值来做你必须做的事情。

public class SQLHelper
{

    public IList<Person> GetPersons()
    {
    try
        {
            var result = new List<Person>();
            conn = new MySqlConnection(connstring);
            conn.Open();
            MySqlCommand myCommand = new MySqlCommand("SELECT * FROM person", conn);
            MySqlDataReader myReader;
            myReader = myCommand.ExecuteReader();
            while (myReader.Read())
            {
                result.add(new Person(){ID = myReader["personID"], ...});
            }
            return result;
        }
        catch
        {
            return null;
        }
    }   

}

public class Person
{
    public string ID {get;set;}
    public string FirstName {get;set;}
    public string LastName {get;set;}
    public string Address {get;set;}
    public string PhoneNumber {get;set;}
    public string PostCode {get;set;}
    public string DateOfBirth {get;set;}

    public override string ToString()
    {
        return ID + " | " + FirstName + " | " + "...";
    }
}

private void fillcomboBox()
{
    cmbTable.Items.Clear();
    var sqlHelper = new SQLHelper();
    var persons = sqlHelper.GetPersons();
    if(persons == null)
    {
        lblInfo.Text = " Error reading the database.";
        return;
    }
    foreach(var person in persons)
    {
        cmbTable.Items.Add(person.ToString());
    }
}

关于c# - 寻找为 Sql 查询和 Windows 窗体创建帮助程序类的指导,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18711025/

相关文章:

c# - 使用类中的一个变量将输出列表输出到数组

c# - 为什么我无法通过代码获取ftp.exe的输出?

字段小于和大于值的 MySQL SELECT

php - MySQL按两列查找多条记录

c# - 在运行时从字符串创建数学函数

c# - System.Data.dll ("Incorrect syntax near ' )'."中的“System.Data.SqlClient.SqlException”)?

mysql - 将行值添加到单列MYSQL中

c++ - WHERE 列 = 值,仅适用于 INTEGER 值

sql - 计算两个日期之间的差异并计算出现的次数

phpmyADMIN 还是 mySQL?我对 PHP 有点困惑