C# 如何使用.NET中Windows窗体应用程序中的单选按钮更新Access表?
我想使用RadioButton控件插入/更新Access数据库中的行。C# 如何使用.NET中Windows窗体应用程序中的单选按钮更新Access表?,c#,.net,winforms,radio-button,sql-update,C#,.net,Winforms,Radio Button,Sql Update,我想使用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) {
例如:工人性别(男/女) 我正在使用一个用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");
}
以下是我以前做过的事情: 我有两个单选按钮,文本为M和F,分别代表男性和女性
public string GetSex()
{
return radioM.Checked ? radioM.Text : radioF.Text;
}
这称为三元运算符
阅读非常聪明的人提供的大量有用信息。拖放单选按钮:
cmd.CommandText = " INSERT INTO SISWA(NIS,NAMA,JENISKELAMIN,ALAMAT,DATE) " +
" VALUES('" + nis + "','" + nama + "',+ getSex() +,'" +
alamat + "',getDate())";
if (radiobutton1.checked==true)
drow[3]="male";
else if(radiobutton2.checked==true)
drow[3]="female";
“使用单选按钮插入数据”是什么意思?当一个单选按钮(例如男性/女性)被更改时,您希望当前人员的性别列在db中被更改吗?如果是,请选中SelectedIndexChanged on单选按钮并将更改应用于数据集…此代码具有sql注入漏洞,请参阅