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

发布于 2024-10-31 02:15:03 字数 2815 浏览 1 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

2024-11-07 02:15:03

拖放单选按钮:

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

Drag and drop the radio buttons:

if (radiobutton1.checked==true)
    drow[3]="male";
else if(radiobutton2.checked==true)
    drow[3]="female";
差↓一点笑了 2024-11-07 02:15:03

这是我之前做过的事情:

我有两个单选按钮,其文本为 M 和 F,分别代表男性和女性。

public string GetSex()
{
    return radioM.Checked ? radioM.Text : radioF.Text;
}

这称为三元运算符。

阅读此帖子,了解来自非常聪明的人的大量有用信息。

Here's something I did before:

I have two radio buttons with text of M and F, for Male and Female.

public string GetSex()
{
    return radioM.Checked ? radioM.Text : radioF.Text;
}

This is called a ternary operator.

Read this thread for lots of good information from very smart people.

岁月蹉跎了容颜 2024-11-07 02:15:03
cmd.CommandText = " INSERT INTO SISWA(NIS,NAMA,JENISKELAMIN,ALAMAT,DATE) " +
                  " VALUES('" + nis + "','" + nama + "',+ getSex() +,'" +
                                alamat + "',getDate())";
cmd.CommandText = " INSERT INTO SISWA(NIS,NAMA,JENISKELAMIN,ALAMAT,DATE) " +
                  " VALUES('" + nis + "','" + nama + "',+ getSex() +,'" +
                                alamat + "',getDate())";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文