问题在使用DataSet中删除一行 [英] Issue while deleting row using DataSet

查看:199
本文介绍了问题在使用DataSet中删除一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继code不会删除数据集的行不更新数据库....

Following code does not delete a row from dataset and dont update the database....

string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
                SqlConnection con = new SqlConnection();
                con.ConnectionString = cs;
                SqlDataAdapter adpt = new SqlDataAdapter("Select *from RegisterInfoB", con);
                ds = new DataSet();
                adpt.Fill(ds, "RegisterTable");
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (dr["FirstName"] == "praveen")
                    {
                        dr.Delete();
                    }
                }
                ds.Tables[0].AcceptChanges();

如何解决这个问题?如何更新我的SQL Server数据库......由数据删除一行。

How to resolve this problem...How do I update my sql server database...by deleting a row from dataset..

推荐答案

根据特定的列值查找的行和删除:

Find rows based on specific column value and delete:

DataRow[] foundRows;
foundRows = ds.Tables["RegisterTable"].Select("FirstName = 'praveen'");
foundRows.Delete();

修改:将DeleteCommand会以数据适配器(基于例如从<一个href=\"https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.deletecommand.aspx\"相对=nofollow> MSDN )

EDIT: Adding a DeleteCommand to the data adapter (based on example from MSDN)

注:此处不是需要一个编号字段,但我不知道你的为 RegisterInfoB 表结构。否则,我们每个人都删除名为普利文。

Note: Need an Id field here instead of FirstName, but I don't know your table structure for RegisterInfoB. Otherwise, we delete everyone named "praveen".

// Create the DeleteCommand.
command = new SqlCommand("DELETE FROM RegisterInfoB WHERE FirstName = @FirstName", con);

// Add the parameters for the DeleteCommand.
parameter = command.Parameters.Add("@FirstName", SqlDbType.NChar, 25, "FirstName");
parameter.SourceVersion = DataRowVersion.Original;

// Assign the DeleteCommand to the adapter
adpt.DeleteCommand = command;

要使用数据适配器的数据集更新数据库:

To update a database with a dataset using a data adapter:

try
{
    adpt.Update(ds.Tables["RegisterTable"]);
}
catch (Exception e)
{
    // Error during Update, add code to locate error, reconcile  
    // and try to update again.
}

来源: https://msdn.microsoft .COM / EN-US /库/ xzb1zw3x(v = vs.120)的.aspx
https://msdn.microsoft.com/en-美国/库/ y06xa2h1(v = vs.120)的.aspx

这篇关于问题在使用DataSet中删除一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆