数据库中的数据发生更改时更新变量

时间:2016-11-15 08:14:02

标签: c# mysql

这里我有三个变量,变量由数据库中的数据填充,并使用标签将其显示在屏幕上。 但是当我更改数据库中的数据时,变量不会自动更改。 我将尝试做的是创建一个变量,以便在数据库中的数据发生更改时自动更改。 但我不知道该怎么办,因为我是新手。

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;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;

namespace vendingInterface
{
    public partial class Form1 : Form
    {

        public MySqlConnection conn;
        private string server;
        private string database;
        private string uid;
        private string password;
        private string produk11;
        private string produk22;
        private string produk33;


        public Form1()
        {
            bacadatabase();
            InitializeComponent();
            info();

        }

我使用标签

显示它
        private void info()
        {
            lbl_produk1.Text = "Jumlah Produk 1 = " + produk11 + " Buah";
            lbl_produk2.Text = "Jumlah Produk 2 = " + produk22 + " Buah";
            lbl_produk3.Text = "Jumlah Produk 3 = " + produk33 + " Buah";
        }

我把数据放在数据库中

        public void bacadatabase()
        {
            string sql = "select *from vanding_machine";
            server = "localhost";
            database = "db_s2u";
            uid = "root";
            password = "";

            string connString = "SERVER=" + server + ";" + " DATABASE=" + database + ";" + " UID=" + uid + ";" + " PASSWORD=" + password + ";";  //conek SQL
            MySqlConnection conn = new MySqlConnection(connString);


            conn.Open();
            //MessageBox.Show("Koneksi Berhasil");

            //command
            MySqlCommand cmd = new MySqlCommand(sql, conn);

            //reader
            MySqlDataReader dataReader = cmd.ExecuteReader();

            while (dataReader.Read())
            {
                produk11 = dataReader["produk1"].ToString();
                produk22 = dataReader["produk2"].ToString();
                produk33 = dataReader["produk3"].ToString();
            }


            dataReader.Close();
            conn.Close();

        }

当我点击按钮时,数据将被输入数据库,并且节目中的变量将自动更新

        private void button9_Click(object sender, EventArgs e)
        {

            server = "localhost";
            database = "db_s2u";
            uid = "root";
            password = "";

            string connString = "SERVER=" + server + ";" + " DATABASE=" + database + ";" + " UID=" + uid + ";" + " PASSWORD=" + password + ";";  //conek SQL
            MySqlConnection conn = new MySqlConnection(connString);


            conn.Open();
            //MessageBox.Show("Koneksi Berhasil");


            string sql = "update vanding_machine set produk1 = @produk111;";

            MySqlCommand cmd = new MySqlCommand(sql, conn);
            cmd.Parameters.Add(new MySqlParameter("@produk111", tb_produk1.Text));

            //close data reader
            cmd.ExecuteNonQuery();

            //close connection
            conn.Close();

        }

        private void button8_Click(object sender, EventArgs e)
        {

        }

        private void button7_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click_1(object sender, EventArgs e)
        {

        }
    }
}

1 个答案:

答案 0 :(得分:1)

您必须创建计时器以检查应用程序中是否更改了数据。数据库不会将应用程序指向任何更改