删除不在DataTable中提供的SQLite表中的数据 [英] Deleting Data Out of a SQLite Table that is not in DataTable provided

查看:70
本文介绍了删除不在DataTable中提供的SQLite表中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前提供了一个DataTable,其中填充了需要保存在SQLite数据库中的条目的DataRows。我目前有一个非常破碎的功能,我不知道该怎么办。我对c#非常陌生,尤其是sql。以下是该方法的错误
部分:

 using(SQLiteConnection c = new SQLiteConnection(connectionString))
{
c 。打开();
string sql ="从StoreOnlineHistory删除,其中StoreNum = @ StoreNum和dt = @ dt" ;;
using(SQLiteCommand cmd = new SQLiteCommand(sql,c))
{
cmd.CommandText = sql;
cmd.Parameters.Add(new SQLiteParameter()
{
ParameterName =" @ StoreNum",
DbType = DbType.String
});
cmd.Parameters.Add(new SQLiteParameter()
{
ParameterName =" @ dt",
DbType = DbType.String
});

尝试
{
foreach(dt.Rows中的DataRow条目)
{
cmd.Parameters [" @ StoreNum"]。值=条目.ItemArray [0]的ToString();
cmd.Parameters [" @ dt"]。Value = entry.Field< string>(" dt");

cmd.ExecuteNonQuery();

Console.WriteLine(" deleted - " + entry.Field< string>(0)+" |" + entry.Field< double>(1)+" |" + entry.Field< string>(2));
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}

c.Close();
}
}

该函数具有不正确的逻辑,因为它试图删除中的值
DataTable来自数据库。我的目标是从DataTable中删除
不是的SQLite表中的值。 



无论逻辑没有按顺序排列,代码都会出现错误,我甚至无法打印错误,因此我不知道该怎么做。 


解决方案

无论逻辑不正常,代码都保持不变抛出错误,我甚至无法打印错误,所以我不知道该怎么做。 


MessageBox 显示 ex 。message

    MessageBox 显示 ex.inner.exception.message


I am currently provided a DataTable filled with DataRows of entries that need to be kept in the SQLite DataBase. I currently have a function that is very broken, and I don't know what to do. I'm extremely new to c#, and especially sql. Here is the errored part of the method:

using (SQLiteConnection c = new SQLiteConnection(connectionString))
            {
                c.Open();
                string sql = "Delete from StoreOnlineHistory where StoreNum=@StoreNum and dt=@dt";
                using (SQLiteCommand cmd = new SQLiteCommand(sql, c))
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.Add(new SQLiteParameter()
                    {
                        ParameterName = "@StoreNum",
                        DbType = DbType.String
                    });
                    cmd.Parameters.Add(new SQLiteParameter()
                    {
                        ParameterName = "@dt",
                        DbType = DbType.String
                    });

                    try
                    {
                        foreach (DataRow entry in dt.Rows)
                        {
                            cmd.Parameters["@StoreNum"].Value = entry.ItemArray[0].ToString();
                            cmd.Parameters["@dt"].Value = entry.Field<string>("dt");

                            cmd.ExecuteNonQuery();

                            Console.WriteLine("deleted - " + entry.Field<string>(0) + " | " + entry.Field<double>(1) + " | " + entry.Field<string>(2));
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }

                    c.Close();
                }
            }

The function has improper logic, as it tries to delete values within the DataTable from the Database. My goal is to delete the values from the SQLite Table that aren't in the DataTable. 

Regardless of the logic not being in order, the code keeps throwing an error and I can't even get the error to print, so I don't know what to do. 

解决方案

Regardless of the logic not being in order, the code keeps throwing an error and I can't even get the error to print, so I don't know what to do. 

MessageBox.Show(ex.message)

if ex.inner.exception != null

   MessageBox.Show(ex.inner.exception.message)


这篇关于删除不在DataTable中提供的SQLite表中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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