DataSet未使用C#中插入的新行进行更新

时间:2016-05-02 12:56:10

标签: c# ado.net dataset disconnected

我试图在c#中插入新行(断开模式),新行成功插入但数据集未更新,所以当我搜索新行时,我找不到它,但是当我搜索旧行,我找到了。

用于插入新行的 insertBTN 按钮

用于按ID ID

搜索行的 searchByID 按钮
  public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private SqlConnection conn;
    private SqlDataAdapter adapter;
    private DataSet ds;

    private void Form1_Load(object sender, EventArgs e)
    {
        conn = new SqlConnection(@"data source=(local);initial catalog =Test;integrated security = true");
        conn.Open();
        adapter = new SqlDataAdapter("select * from Emp",conn);
        ds = new DataSet();
        adapter.Fill(ds,"Emp");
    }

    private void insertBTN_Click(object sender, EventArgs e)
    {
        try
        {
            int id = int.Parse(textBox1.Text);
            string name = textBox2.Text;
            string address = textBox3.Text;
            int age = int.Parse(textBox4.Text);
            int salary = int.Parse(textBox5.Text);

            SqlCommand command = new SqlCommand("Insert into Emp values(" + id + ",'" + name + "','" + address + "'," + age + "," + salary + ")", conn);
            command.ExecuteNonQuery();


           adapter.Update(ds,"Emp");
            MessageBox.Show("Employee added successfully");
        }
        catch (Exception exception)
        {
            MessageBox.Show(exception.Message);
        }
    }

    private void searchByID_Click(object sender, EventArgs e)
    {
        try
        {
            int id = int.Parse(textBox1.Text);
            foreach (DataRow row in ds.Tables["Emp"].Rows)
            {
                if (Convert.ToInt32(row["id"]) == id)
                {
                    textBox2.Text = Convert.ToString(row["name"]);
                    textBox3.Text = Convert.ToString(row["address"]);
                    textBox4.Text = Convert.ToString(row["age"]);
                    textBox5.Text = Convert.ToString(row["salary"]);
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

}

1 个答案:

答案 0 :(得分:0)

DataAdapter的Update方法不会再次读取,但会尝试执行从原始SELECT语句派生的INSERT,DELETE和UPDATE命令,这些命令针对DataTable中已更改RowState的行

因此,如果您想使用适配器的Update方法,可以编写

private void insertBTN_Click(object sender, EventArgs e)
{
    try
    {
        DataRow row = ds.Tables["emp"].NewRow();
        row.SetField<int>("id", int.Parse(textBox1.Text));
        row.SetField<string>("name", textBox2.Text));
        row.SetField<string>("address", textBox3.Text));
        row.SetField<int>("age", int.Parse(textBox4.Text));
        row.SetField<int>("salary", int.Parse(textBox5.Text));
        ds.Tables["emp"].Rows.Add(row);
        adapter.Update(ds,"Emp");
        MessageBox.Show("Employee added successfully");
    }
    catch (Exception exception)
    {
        MessageBox.Show(exception.Message);
    }
}

此时,您的DataSet包含添加的行,因为您已手动添加它,然后将所有内容传递给Update方法以将其写入数据库。

但是,请记住,要使用的Update方法需要存在某些条件。您可以在MSDN的DbDataAdapter.Update页面上阅读它们 主要是您需要检索表的主键,没有多个表连接在一起,并且您已使用DbCommandBuilder对象来创建所需的命令(再次在MSDN页面中的示例解释它)

与您的问题无关,但您可以更改搜索方法并避免编写循环来搜索ID

private void searchByID_Click(object sender, EventArgs e)
{
    try
    {
        int id = int.Parse(textBox1.Text);
        DataRow[] foundList = ds.Tables["Emp"].Select("Id = " + id);
        if(foundList.Length > 0)
        {
            textBox2.Text = foundList[0].Field<string>("name");
            textBox3.Text = foundList[0].Field<string>("address");
            textBox4.Text = foundList[0].Field<int>("age").ToString();
            textBox5.Text = foundList[0].Field<int>("salary").ToString();

        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }