gpt4 book ai didi

c# - 如何使用 RadioButtons 从 .NET 中的 Windows 窗体应用程序更新 Access 表?

转载 作者:行者123 更新时间:2023-11-30 16:30:31 25 4
gpt4 key购买 nike

我想使用 RadioButton 控件从 Access 数据库插入/更新行。
例如: worker 性别(男/女)

我正在使用用 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");
}

最佳答案

拖放单选按钮:

if (radiobutton1.checked==true)
drow[3]="male";
else if(radiobutton2.checked==true)
drow[3]="female";

关于c# - 如何使用 RadioButtons 从 .NET 中的 Windows 窗体应用程序更新 Access 表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5605666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com