更新问题。

损伤

会员
已加入
2016年9月5日
留言内容
6
地点
塞尔维亚
编程经验
3-5
我有更新按钮的问题。
当我编辑数据并单击“保存”时,我将在dataGridView中获取已编辑的数据,但是当我退出表单并返回时,数据与编辑之前的数据相同。
这是我的代码:

使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;
使用MySql.Data.MySqlClient;


命名空间mitAdministrator
{
公共部分课程Katedre:表格
{
MySqlConnection连接=新的MySqlConnection("SERVER = mysql.ivanpetric.org; DATABASE = sqlmasdata; UID = *******; PASSWORD = ******;");


公共Katedre()
{
InitializeComponent();
}


private void buttonKatedreExit_Click(对象发送者,EventArgs e)
{

this.Close();
}


私人空白Katedre_Load(对象发送者,EventArgs e)
{
//禁用textBox
textBoxIndentifikacioniBroj.Enabled = false;
textBoxImeKatedre.Enabled = false;
textBoxKatedraStartID.Visible = false;
buttonKatedreCancel.Enabled = false;
buttonKatedreSave.Enabled = false;



//加载数据网格视图
loadTable();
}



公共无效loadTable()
{


MySqlCommand cmdDataBase =新的MySqlCommand("从mitAdminPanelKatedre选择*;", connection);


尝试
{
MySqlDataAdapter sda =新的MySqlDataAdapter();
sda.SelectCommand = cmdDataBase;
DataTable dbDataSet = new DataTable();
sda.Fill(dbDataSet);


mitAdminPanelKatedreBindingSource.DataSource = dbDataSet;
dataGridViewKatedre.DataSource = mitAdminPanelKatedreBindingSource;
sda.Update(dbDataSet);
}
抓住(例外)
{
MessageBox.Show(ex.Message);
}
}



private void buttonKatedreSave_Click(对象发送者,EventArgs e)
{
尝试
{
如果(textBoxIndentifikacioniBroj.Text!="" &&textBoxImeKatedre.Text!="")
{
connection.Open();


字符串查询="插入mitAdminPanelKatedre(katedraID,katedraIme)值(@ katedraID,@ katedraIme)";


使用(MySqlCommand cmd = new MySqlCommand(query,connection))
{
cmd.Parameters.AddWithValue("@katedraID",textBoxIndentifikacioniBroj.Text);
cmd.Parameters.AddWithValue("@katedraIme",textBoxImeKatedre.Text);
cmd.ExecuteNonQuery();
}


connection.Close();
MessageBox.Show("Katedra "+ textBoxImeKatedre.Text +"je uspe?no dodata u bazu!", "Uspesno insertovanje podataka。",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
抓住(前例外)
{
MessageBox.Show(ex.Message);
}





//锁定文本框
textBoxIndentifikacioniBroj.Enabled = false;
textBoxImeKatedre.Enabled = false;


//解锁按钮
buttonKatedreNext.Enabled = true;
buttonKatedrePrevious.Enabled = true;


buttonKatedreSave.Enabled = false;
buttonKatedreDelete.Enabled = true;


//刷新数据网格视图
loadTable();
}


private void buttonKatedreNew_Click(对象发送者,EventArgs e)
{
textBoxIndentifikacioniBroj.Enabled = true;
textBoxImeKatedre.Enabled = true;


textBoxIndentifikacioniBroj.Clear();
textBoxImeKatedre.Clear();


buttonKatedreNext.Enabled = false;
buttonKatedrePrevious.Enabled = false;
buttonKatedreSave.Enabled = true;
buttonKatedreDelete.Enabled = false;
}


private void buttonKatedreNext_Click(对象发送者,EventArgs e)
{
mitAdminPanelKatedreBindingSource.MoveNext();
}


private void buttonKatedrePrevious_Click(对象发送者,EventArgs e)
{
mitAdminPanelKatedreBindingSource.MovePrevious();
}


private void buttonKatedreDelete_Click(对象发送者,EventArgs e)
{
字符串查询="从mitAdminPanelKatedre中删除,其中katedraID ='"+ textBoxIndentifikacioniBroj.Text +"' and katedraIme='"+ textBoxImeKatedre.Text +"'";
MySqlCommand cmdDataBase =新的MySqlCommand(query, connection);
MySqlDataReader myReader;


尝试
{
connection.Open();
myReader = cmdDataBase.ExecuteReader();
MessageBox.Show("Katedra "+ textBoxImeKatedre.Text +"je uspe?no obrisana!", "Uspesno brisanje podataka。",MessageBoxButtons.OK,MessageBoxIcon.Information);
同时(myReader.Read())
{
}


myReader.Close();
connection.Close();


}
抓住(前例外)
{
MessageBox.Show(ex.Message);
}
最后
{
textBoxIndentifikacioniBroj.Clear();
textBoxImeKatedre.Clear();
}


loadTable();
}


private void buttonKatedreEdit_Click(对象发送者,EventArgs e)
{
textBoxIndentifikacioniBroj.Enabled = true;
textBoxImeKatedre.Enabled = true;


buttonKatedreDelete.Enabled = false;
buttonKatedreNew.Enabled = false;
buttonKatedreSave.Enabled = false;
buttonKatedreNext.Enabled = false;
buttonKatedrePrevious.Enabled = false;
dataGridViewKatedre.Enabled = false;


buttonKatedreCancel.Enabled = true;
}


private void buttonKatedreEditSave_Click(对象发送者,EventArgs e)
{
尝试
{
如果(textBoxIndentifikacioniBroj.Text!="" &&textBoxImeKatedre.Text!="")
{
connection.Open();

字符串查询="更新mitAdminPanelKatedre SET katedraID ='"+ this.textBoxIndentifikacioniBroj.Text +"',katedraIme='"+ this.textBoxImeKatedre.Text +"'WHERE katedraStartID ='"+ this.textBoxKatedraStartID.Text +"'";

使用(MySqlCommand cmd = new MySqlCommand(query,connection))
{
cmd.ExecuteNonQuery();
}

connection.Close();
MessageBox.Show("Katedra "+ textBoxImeKatedre.Text +"je uspe?no promenjena!", "Uspesno insertovanje podataka。",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
抓住(前例外)
{
MessageBox.Show(ex.Message);
}
最后
{
如果(连接!=空)
connection.Close();
}







textBoxIndentifikacioniBroj.Enabled = false;
textBoxImeKatedre.Enabled = false;


buttonKatedreDelete.Enabled = true;
buttonKatedreNew.Enabled = true;
buttonKatedreSave.Enabled = false;
buttonKatedreNext.Enabled = true;
buttonKatedrePrevious.Enabled = true;
dataGridViewKatedre.Enabled = true;


buttonKatedreCancel.Enabled = false;
}


private void buttonKatedreCancel_Click(对象发送者,EventArgs e)
{
textBoxIndentifikacioniBroj.Enabled = false;
textBoxImeKatedre.Enabled = false;


buttonKatedreDelete.Enabled = true;
buttonKatedreNew.Enabled = true;
buttonKatedreSave.Enabled = false;
buttonKatedreNext.Enabled = true;
buttonKatedrePrevious.Enabled = true;
dataGridViewKatedre.Enabled = true;


mitAdminPanelKatedreBindingSource.MoveFirst();
}
}
}
 
最佳 底部