当我打开表单时,它会通过绑定源自动加载数据库。 有一个按钮可以按文本框中的值更改数据 还有一个取消这些更改的按钮(如果您在文本框中输入值并按取消它将恢复为原始数据。
我不知道如何编码取消按钮,我已成功完成其他所有操作
我目前的更改代码(编辑:)是:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Eastern_Property_Owners_Club
{
public partial class Club_Record_Maintenance : Form
{
private OleDbConnection dbConn; // Connection object
private OleDbCommand dbCmd; // Command object
private string sConnection;
private string sql;
public Club_Record_Maintenance()
{
InitializeComponent();
try
{
// Construct an object of the OleDbConnection class
// to store the connection string representing
// the type of data provider (database) and the source (actual db).
//string ConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\abc.mdb;Jet OLEDB:Database Password=password";
//OleDbConnection MyConn = new OleDbConnection(ConnStr);
// sConnection = "Provider = Microsoft.Jet.OLEDB.4.0;" + "Data Source = Student.mdb";
sConnection = "Provider = Microsoft.Jet.OLEDB.4.0;" + "Data Source = Eastern_Property_Maintenance.mdb";
dbConn = new OleDbConnection(sConnection);
dbConn = new OleDbConnection(sConnection);
dbConn.Open();
}
catch (System.Exception exc)
{
MessageBox.Show(exc.Message);
return;
}
}
private void button3_Click(object sender, EventArgs e)
{
//exits and goes to main menu
MainMenu newForm = new MainMenu();
newForm.Show();
this.Close();
}
private void Club_Record_Maintenance_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'eastern_Property_MaintenanceDataSet2.Club' table. You can move, or remove it, as needed.
this.clubTableAdapter.Fill(this.eastern_Property_MaintenanceDataSet2.Club);
}
private void button1_Click(object sender, EventArgs e)
{
try
{
//declaring value to textboxes
string Name = txtName.Text;
string Address = txtAddress.Text;
string Phone = txtPhone.Text;
//declaring parameters in the SQL code
//Field[Name] = whatever is in txtName.Text and so on
//-----
//adds values from the textboxes into the database
sql = " UPDATE Club SET "
+ " CompanyName = @Name"
+ ","
+ " CompanyAddress = @Address"
+ ","
+ " CompanyPhone = @Phone"
+ " WHERE CompanyName = @Name; ";
dbCmd = new OleDbCommand(sql, dbConn);
//adding the perameters to ever place-holder in my sql
dbCmd.Parameters.AddWithValue("@Name", txtName.Text);
dbCmd.Parameters.AddWithValue("@Address", txtAddress.Text);
dbCmd.Parameters.AddWithValue("@Phone", txtPhone.Text);
// Execute query
dbCmd.ExecuteNonQuery();
}
catch (System.Exception exc)
{
MessageBox.Show(exc.Message);
return;
}
//a message to show the button has worked
MessageBox.Show("Club has been Changed! \nRefresh the content by going back to the Main Menu");
}
private void btnCancel_Click(object sender, EventArgs e)
{
}
}
}
它工作正常,但我不知道如何编码取消按钮。
答案 0 :(得分:1)
创建三个全局变量并将bool初始化为false以在检索数据时存储数据库中的值检查数据检索方法中的bool,如下所示并存储值。
if (!oldValues)
{
// your code to store values from the database when the data is retrieved for the first time
// change oldValues to true
}
因此,当进行更改并将其存储在数据库中时,这些全局变量将包含旧值。用户按下取消您使用这些变量来使用更新查询撤消更改,就像您在更新中使用的那样,但现在在取消按钮单击事件处理程序上...您还必须再次将bool更改为false,这样数据时再次检索它会将变量中的值存储为旧值。
在表单加载事件中执行此操作:
bool oldValues = false; // that means there are not old values stored
制作这些全局变量:
string oldName;
string oldAddress;
string oldPhone;
现在添加一个名为Add Record Button
的按钮,并在单击时执行此操作:
if (!oldValues) //Checks that there are no old Values stored already
{
if (txtName.Text != "") // handles the case if you have allowed nulls in this field
{
oldName = txtName.Text;
}
if (txtAddress.Text != "") // handles the case if you have allowed nulls in this field
{
oldAddress = txtAddress.Text;
}
if (txtPhone.Text != "") // handles the case if you have allowed nulls in this field
{
oldPhone = txtPhone.Text;
}
oldValues = true; // change to true so that program knows that there are old values stored
txtName.Clear(); // Clear all text boxes on form so that user can enter new data
txtAddress.Clear();
txtPhone.Clear();
}
在“取消”按钮上执行此操作:
if (oldValues) // Check if old values are stored
{
if (oldName != "") // check if its not an empty string
{
txtName.Text = oldName;
}
else // if it is a empty string then Clear the text box
txtName.Clear();
if (oldAddress != "")
{
txtAddress.Text = oldAddress;
}
else
txtAddress.Clear();
if (oldPhone != "")
{
txtPhone.Text = oldPhone;
}
else
txtPhone.Clear();
oldValues = false; // change the oldValues flag to false so that old values can be stored again...
}