radiobutton:我如何从数据库中检索数据

时间:2013-03-18 18:14:03

标签: c# ms-access

Access 2003 db

Field Name          Data Type  
CriminalOffence     Text

VS 2010 C#

使用groupbox,我可以将来自radiobutton的数据存储到我的数据库中。我已经将groupbox和radiobuttons数据分别添加到我的数据库中。当我使用我的nativgation按钮时,我会看到存储在groupbox文本所在的数据。所以问题是我不知道如何正确检索radiobutton的数据。从文本框和combox中检索数据正在显示。此外,当我想插入新数据时,radiobutton不会被清除。所以......

我有两个radiobuttons,1)rBYes 2)rBNo

我有以下插入记录的方法......

private void btnInsert_Click(object sender, EventArgs e)
    {
        OleDbCommand cmd = new OleDbCommand(@"INSERT INTO Table1
                                    (ID, AgeGroup, Gender, CriminalOffence)   
                              VALUES(@txtID, @AcBAG, @cBGender, @CriminalOffence)", myCon);
        cmd.CommandType = CommandType.Text;

        cmd.Parameters.AddWithValue("@ID", txtID.Text);
        cmd.Parameters.AddWithValue("@AgeGroup", cBAG.Text);
        cmd.Parameters.AddWithValue("@Gender", cBGender.Text);

        string str = "";
        if (rBYes.Checked)
        {
            str = "Yes";
        }
        if (rBNo.Checked)
        {
            str = "No";
        }
        cmd.Parameters.AddWithValue("@CriminalOffence", SqlDbType.NVarChar).Value =   
        str;
    }

创建新记录的方法

 private void btnNew_Click(object sender, EventArgs e)
    {
        txtID.Text = "";
        cBAG.Text = "";
        cBGender.Text = "";
        rBYes.Text = "";
        rBNo.Text = "";
    }

导航按钮示例..

    private void btnNextRec_Click(object sender, EventArgs e)
    {
        this.table1BindingSource.MoveNext();
    }

...的ConnectionString

    myCon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\..
    \Database1.mdb");

请事先有人帮助我,谢谢

1 个答案:

答案 0 :(得分:0)

参数名称必须与SQL insert语句中的参数匹配。

private void btnInsert_Click(object sender, EventArgs e)
{
  OleDbCommand cmd = new OleDbCommand(@"INSERT INTO Table1
                                        (ID, AgeGroup, Gender, CriminalOffence)   
                                  VALUES(@ID, @AgeGroup, @Gender, @CriminalOffence)", myCon);
  cmd.CommandType = CommandType.Text;

  cmd.Parameters.AddWithValue("@ID", txtID.Text);
  cmd.Parameters.AddWithValue("@AgeGroup", cbAG.Text);
  cmd.Parameters.AddWithValue("@Gender", cbGender.Text);
  cmd.Parameters.AddWithValue("@CriminalOffence", ((rBYes.Checked)? "Yes":"No"));

  myCon.Open();
  cmd.ExecuteNonQuery();
  myCon.Close(); 
}