使用数据集删除行时出现问题

本文关键字:问题 删除行 数据集 | 更新日期: 2023-09-27 18:14:59

下面的代码不会从数据集中删除一行,也不会更新数据库....

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数据库…

使用数据集删除行时出现问题

我希望这对你有用:

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

两个变化
1st: if (dr["FirstName"].ToString().Trim() == "praveen")在您的数据库的FirstName字段中修剪空格。
2nd :使用adpt.Update(ds);更新数据库

另一种方式:

string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
                SqlConnection con = new SqlConnection();
                con.ConnectionString = cs;
                string firstName= "praveen"; // this data will get from calling method
                SqlDataAdapter adpt = new SqlDataAdapter("Select * from RegisterInfoB", con);
                DataSet ds = new DataSet();
                adpt.Fill(ds, "RegisterTable");
                string deleteQuery = string.Format("Delete from RegisterInfoB where FirstName = '{0}'", firstName);
                SqlCommand cmd = new SqlCommand(deleteQuery, con);
                adpt.DeleteCommand = cmd;
                con.Open();
                adpt.DeleteCommand.ExecuteNonQuery();
                con.Close();

同样,您可以为UpdateCommandInsertCommand编写查询,分别执行Update和Insert。

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

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

EDIT:向数据适配器添加DeleteCommand(基于来自MSDN的示例)

注意:这里需要一个Id字段而不是FirstName,但我不知道RegisterInfoB的表结构。否则,我们删除所有名为"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;

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

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/library/xzb1zw3x (v = vs.120) . aspxhttps://msdn.microsoft.com/en-us/library/y06xa2h1 (v = vs.120) . aspx

希望问题解决,试一试:

        var connectionString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
        var selectQuery = "Select * from RegisterInfoB";
        SqlConnection connection = new SqlConnection(connectionString);
        SqlCommand selectCommand = new SqlCommand(selectQuery, connection);
        SqlDataAdapter adapter = new SqlDataAdapter(selectCommand);
        SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
        DataSet dataSet = new DataSet();        
        // you can use the builder to generate the DeleteCommand
        adapter.DeleteCommand = builder.GetDeleteCommand();
        // or 
        // adapter.DeleteCommand = new SqlCommand("DELETE FROM RegisterInfoB WHERE Id= @Id", connection);
        adapter.Fill(dataSet, "RegisterInfoB");
        // you can use the foreach loop 
        foreach (DataRow current in dataSet.Tables["RegisterInfoB"].Rows)
        {
            if (current["FirstName"].ToString().ToLower() == "praveen")
            {
                current.Delete();
            }
        }
        // or the linq expression 
        // dataSet.Tables["RegisterInfoB"]
        //    .AsEnumerable()
        //    .Where(dr => dr["FirstName"].ToString().ToLower().Trim() == "praveen")
        //    .ToList()
        //    .ForEach((dr)=> dr.Delete());
        var result = adapter.Update(dataSet.Tables["RegisterInfoB"]);

还可以添加一些事件,并放置一个断点,以查看状态是否正在更改,或者是否有阻止源更改的错误:

        dataSet.Tables["RegisterInfoB"].RowDeleted += (s, e) =>
        {
            var r = e.Row.RowState;
        };
        dataSet.Tables["RegisterInfoB"].RowDeleting += (s, e) =>
        {
            var r = e.Row.RowState;
        };