【问题标题】:SQLite insert does not work in DAL, no errors appear?SQLite 插入在 DAL 中不起作用,没有出现错误?
【发布时间】:2025-12-15 14:50:01
【问题描述】:

我是 SQlite 数据库的新手。
我正在尝试在 sqlite db 表“time_sheet_info”中插入一条新记录, 我从 .asmx WebService 调用 DAL 方法; & 没有错误; ExecuteNonQuery 返回 1;但没有数据出现。
我尝试在 WebService 中执行插入命令;它在那里工作得很好!

有什么建议吗?

**注意:我使用自动生成的代理访问 WS。

界面:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SaveDataDolCls data = new SaveDataDolCls();
            data.intProjectID = 1;
            data.intTaskID = 1;

            WebService1 wsp = new WebService1();
            wsp.saveData(data);
        }
    }

WS:

 [WebMethod]
    public void saveData(SaveDataDolCls arrData)
    {
        using (TransactionScope scope = new TransactionScope())
        {
            obj1.SaveData(arrData);
        }

    }

DAL:

SaveDataDolCls obj = new SaveDataDolCls();
    public void SaveData(SaveDataDolCls arrData)
    {
        using (SQLiteConnection conn = new SQLiteConnection())
        {
            string dbPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DB\\MySqliteDb.db");
            string connStr = "data source= " + dbPath + "; version=3;";
            conn.ConnectionString = connStr;
            conn.Open();
            using (SQLiteCommand cmd = new SQLiteCommand(conn))
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "insert into time_sheet_info (project_id, task_id) values ('1','3');";
                int i = cmd.ExecuteNonQuery();
            }
            conn.Close();
        }
    }

【问题讨论】:

  • 您的意思是它不会通过“没有数据出现”将数据插入到您的数据库中吗? ExecuteNonQuery 返回 1 实际上意味着它插入了 1 行 afaik。

标签: c# web-services sqlite data-access-layer


【解决方案1】:

您必须在 using 块的末尾调用 scope.Complete,即:

using (TransactionScope scope = new TransactionScope())
{
    obj1.SaveData(arrData);
    scope.Complete();
}

【讨论】: