CSharp CRUD系统将数据库中的数据显示到消息框中

时间:2018-07-08 11:02:56

标签: c# crud

我刚刚开始编码c#。我在显示消息框或类似内容时遇到了问题。我想显示一个带有按钮的消息。我有一个名为GetNameById()的函数;这应该是触发消息的功能,但我不知道如何。谁能帮我。这是我的代码。

private void Form1_Load(object sender, EventArgs e)
    {
        namemanager x = new namemanager();
        List<Class1> lijst = x.GetAllNames();

        comboBox1.DataSource = lijst;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        namemanager x = new namemanager();
        x.InsertName(textBox1.Text);

    }

    private void button1_Click(object sender, EventArgs e)
    {
        studentmanager x = new studentmanager();
        Student y = new Student();
        y.Achternaam = textBox1.Text;
        y.Voornaam = textBox2.Text;
        y.Diploma = checkBox1.Checked;

        x.InsertNaam(y);

    }


    private void button2_Click(object sender, EventArgs e)
    {
        Class1 name = new Class1();
        name.Id = 5;
        name.Name = "This Record is updated";

        namemanager x = new namemanager();
        x.UpdateGenre(name);
    }

    private void button3_Click(object sender, EventArgs e)
    {
        namemanager x = new namemanager();
        x.DeleteById(4);
    }

    private void button4_Click(object sender, EventArgs e)
    {
        namemanager nameManager = new namemanager();
        Class1 x = nameManager.GetNameById(2);

    }

这是我的另一段代码:

class namemanager
{
    MySqlConnection con;
    //CONSTRUCTOR
    public namemanager()
    {
        con = new MySqlConnection("Server=localhost;Database=cruddatabase;Uid=root;Pwd=;");
    }

    public List<Class1> GetAllNames()
    {
        con.Open();
        try
        {
            MySqlCommand cmd = new MySqlCommand("SELECT * FROM names", con);
            MySqlDataReader reader = cmd.ExecuteReader();

            List<Class1> result = new List<Class1>();

            while (reader.Read())
            {
                Class1 names = new Class1();
                names.Id = (int)reader["id"];
                names.Name = (string)reader["name"];

                result.Add(names);
            }
            return result;
        }
        finally
        {
            con.Close();
        }


    }

    //INSERT
    public void InsertName(string aName)
    {
        con.Open();
        try
        {
            MySqlCommand cmd = new MySqlCommand("INSERT INTO names(name) VALUES(@name)", con);
            cmd.Parameters.AddWithValue("@name", aName.Trim());
            cmd.ExecuteNonQuery();

        }
        finally
        {
            con.Close();
        }
    }

    //UPDATE
    public void UpdateGenre(Class1 aName)
    {
        con.Open();
        try
        {
            MySqlCommand cmd = new MySqlCommand("UPDATE names SET name = @name WHERE id = @id ", con);
            cmd.Parameters.AddWithValue("@name", aName.Name);
            cmd.Parameters.AddWithValue("@id", aName.Id);

            cmd.ExecuteNonQuery();

        }
        finally
        {
            con.Close();
        }
    }
    //DELETE

    public void DeleteById(int aId)
    {
        con.Open();
        try
        {
            MySqlCommand cmd = new MySqlCommand("DELETE FROM names WHERE id = @id", con);
            cmd.Parameters.AddWithValue("@id", aId);

            int x = cmd.ExecuteNonQuery();
        }
        finally
        {
            con.Close();
        }
    }

    public Class1 GetNameById(int aId)
    {
        con.Open();
        try
        {
            MySqlCommand cmd = new MySqlCommand("SELECT * FROM names WHERE id = @id", con);
            cmd.Parameters.AddWithValue("@id", aId);
            MySqlDataReader reader = cmd.ExecuteReader();

            if (reader.Read())
            {
                Class1 result = new Class1();
                result.Id = (int)reader["id"];
                result.Name = (string)reader["name"];

                return result;
            }
            else
            {
                throw new Exception("NIET GEVONDEN");
            }

        }
        finally
        {
            con.Close();
        }
    }
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

代码后:

Cannot get error.message because property message is missing in AxiosError [1].

 [1] 10│ export const handleRequestError = (error: (AxiosError<> | Error)) => {
     11│   // General error
     12│   if (typeof error === Error) {
     13│     errorNotification('Request Fail', error.message);
     14│     console.log('Error', error.message);
     15│   }

您需要显示信息。 x是Class1的实例,因此其属性之一是Name。因此,您可以通过x.Name引用它。

在Windows窗体中,显示对话框的调用基于名为MessageBox的类,并且具有名为Show的共享/静态功能。

因此,您的下一行将是:

Class1 x = nameManager.GetNameById(2);