如何使用 RadioButtons 从 .NET 中的 Windows 窗体应用程序更新 Access 表?
我想使用 RadioButton 控件从 Access 数据库插入/更新行。
例如:工人性别(男/女)
我正在使用用 C# 编写的 WinForms 应用程序。
我的实际代码如下:
private OleDbConnection con;
private DataSet ds1;
private OleDbDataAdapter da;
int MaxRows = 0;
int inc = 0;
private void Form1_Load(object sender, EventArgs e)
{
con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\MyWorkers1.mdb");
ds1 = new DataSet();
//con.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\MyWorkers1.mdb";
string sql = "SELECT * from tblWorkers";
da = new OleDbDataAdapter(sql, con);
con.Open();
da.Fill(ds1, "MyWorkers1");
NavigateRecords();
MaxRows = ds1.Tables["MyWorkers1"].Rows.Count;
//MaxRows = ds1.Tables["MyWorkers1"].Rows[inc];
//MessageBox.Show("Database open");
con.Close();
//MessageBox.Show("Database close");
//con.Dispose();
}
private void NavigateRecords()
{
DataRow drow = ds1.Tables["MyWorkers1"].Rows[inc];
textBox1.Text = drow.ItemArray.GetValue(0).ToString();
textBox2.Text = drow.ItemArray.GetValue(1).ToString();
textBox3.Text = drow.ItemArray.GetValue(2).ToString();
}
private void btnNext_Click(object sender, EventArgs e)
{
if (inc != MaxRows - 1)
{
inc++;
NavigateRecords();
}
else
{
MessageBox.Show("No More Records");
}
}
private void btnPrevious_Click(object sender, EventArgs e)
{
if (inc > 0)
{
inc--;
NavigateRecords();
}
else
{
MessageBox.Show("First Record");
}
}
private void btnFirst_Click(object sender, EventArgs e)
{
if (inc != 0)
{
inc = 0;
NavigateRecords();
}
}
private void btnLast_Click(object sender, EventArgs e)
{
if (inc != MaxRows - 1)
{
inc = MaxRows - 1;
NavigateRecords();
}
}
private void btnAddNew_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
}
private void btnSave_Click(object sender, EventArgs e)
{
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
DataRow drow = ds1.Tables["MyWorkers1"].NewRow();
drow[0] = textBox1.Text;
drow[1] = textBox2.Text;
drow[2] = textBox3.Text;
ds1.Tables["MyWorkers1"].Rows.Add(drow);
con.Open();
da.Update(ds1, "MyWorkers1");
con.Close();
MaxRows = MaxRows + 1;
inc = MaxRows - 1;
MessageBox.Show("Record / Entry Added");
}
private void btnUpdate_Click(object sender, EventArgs e)
{
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
DataRow dRow2 = ds1.Tables["MyWorkers1"].Rows[inc];
dRow2[0] = textBox1.Text;
dRow2[1] = textBox2.Text;
dRow2[2] = textBox3.Text;
da.Update(ds1, "MyWorkers1");
MessageBox.Show("Data Updated");
}
I want to insert / update rows from an Access database, using RadioButton controls.
For eg.: Sex of worker (male / female)
I'm using a WinForms application written in C#.
My actual code is the following:
private OleDbConnection con;
private DataSet ds1;
private OleDbDataAdapter da;
int MaxRows = 0;
int inc = 0;
private void Form1_Load(object sender, EventArgs e)
{
con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\MyWorkers1.mdb");
ds1 = new DataSet();
//con.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\MyWorkers1.mdb";
string sql = "SELECT * from tblWorkers";
da = new OleDbDataAdapter(sql, con);
con.Open();
da.Fill(ds1, "MyWorkers1");
NavigateRecords();
MaxRows = ds1.Tables["MyWorkers1"].Rows.Count;
//MaxRows = ds1.Tables["MyWorkers1"].Rows[inc];
//MessageBox.Show("Database open");
con.Close();
//MessageBox.Show("Database close");
//con.Dispose();
}
private void NavigateRecords()
{
DataRow drow = ds1.Tables["MyWorkers1"].Rows[inc];
textBox1.Text = drow.ItemArray.GetValue(0).ToString();
textBox2.Text = drow.ItemArray.GetValue(1).ToString();
textBox3.Text = drow.ItemArray.GetValue(2).ToString();
}
private void btnNext_Click(object sender, EventArgs e)
{
if (inc != MaxRows - 1)
{
inc++;
NavigateRecords();
}
else
{
MessageBox.Show("No More Records");
}
}
private void btnPrevious_Click(object sender, EventArgs e)
{
if (inc > 0)
{
inc--;
NavigateRecords();
}
else
{
MessageBox.Show("First Record");
}
}
private void btnFirst_Click(object sender, EventArgs e)
{
if (inc != 0)
{
inc = 0;
NavigateRecords();
}
}
private void btnLast_Click(object sender, EventArgs e)
{
if (inc != MaxRows - 1)
{
inc = MaxRows - 1;
NavigateRecords();
}
}
private void btnAddNew_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
}
private void btnSave_Click(object sender, EventArgs e)
{
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
DataRow drow = ds1.Tables["MyWorkers1"].NewRow();
drow[0] = textBox1.Text;
drow[1] = textBox2.Text;
drow[2] = textBox3.Text;
ds1.Tables["MyWorkers1"].Rows.Add(drow);
con.Open();
da.Update(ds1, "MyWorkers1");
con.Close();
MaxRows = MaxRows + 1;
inc = MaxRows - 1;
MessageBox.Show("Record / Entry Added");
}
private void btnUpdate_Click(object sender, EventArgs e)
{
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
DataRow dRow2 = ds1.Tables["MyWorkers1"].Rows[inc];
dRow2[0] = textBox1.Text;
dRow2[1] = textBox2.Text;
dRow2[2] = textBox3.Text;
da.Update(ds1, "MyWorkers1");
MessageBox.Show("Data Updated");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
拖放单选按钮:
Drag and drop the radio buttons:
这是我之前做过的事情:
我有两个单选按钮,其文本为 M 和 F,分别代表男性和女性。
这称为三元运算符。
阅读此帖子,了解来自非常聪明的人的大量有用信息。
Here's something I did before:
I have two radio buttons with text of M and F, for Male and Female.
This is called a ternary operator.
Read this thread for lots of good information from very smart people.