這裏我有三個變量,變量由數據庫中的數據填充,並使用標籤將其顯示在屏幕上。 但是,當我更改數據庫中的數據時,變量不會自動更改。 我會嘗試做什麼是創建一個變量,以便在數據庫中的數據更改時自動更改。 但我不知道該怎麼做,因爲我是新手。當數據庫中的數據發生變化時更新變量
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)
{
}
}
}
行,所以你在某些時候讀了一些值,但你必須去檢查這些值是否改變,SQL不會通知更改 – BugFinder
@BugFinder然後我怎麼知道如果數據庫中的數據發生變化,如果sql沒有通知。你有解決方案嗎?因爲我沒有解決方案 –
你是否從你的表單插入到數據庫中,你是否檢查你的數據庫以確保你插入的是 –