list - SQLite给出异常

标签 list sqlite error-handling insert catch-block

我正在努力向数据库中添加项目列表,我想在添加更多项目时继续添加一个人的数据。这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StockTakeRedo.BL;
using System.Data.SQLite;
using System.Data;

namespace StockTakeRedo.DAL
{
    class PersonSQLProvider : PersonProviderBase
    {
        private SQLiteConnection _sqlConnection;
        private string _connStr = "Data Source=c:\\DataStores\\PersonItemDatabase.s3db;Version=3";


        public override int Insert(Person Person, List<Item> ItemList)
        {
            int rc = 0;

            try
            {



                _sqlConnection = new SQLiteConnection(_connStr);
                _sqlConnection.Open();

                string insertQuery = "INSERT INTO PersonItemDatabase([_ID], [_Age], [_Email], [_FirstName], [_LastName], [_ItemName], [_ItemCode], [_ItemPrice], [_ItemDescription]) VALUES(" +
                                     "@ID, @Age, @Email, @FirstName, @LastName, @ItemName, @ItemCode, @ItemPrice, @ItemDescription)";
                foreach (Item item in ItemList)
                {


                    SQLiteCommand sqlCommand = new SQLiteCommand(insertQuery, _sqlConnection);
                    SQLiteParameter[] sqlParams = new SQLiteParameter[]
                    {
                    new SQLiteParameter("@ID",DbType.Int64),
                    new SQLiteParameter("@Age", DbType.Int32),
                    new SQLiteParameter("@Email",DbType.String),
                    new SQLiteParameter("@FirstName",DbType.String),
                    new SQLiteParameter("@LastName",DbType.String),
                    new SQLiteParameter("@ItemName", DbType.String),
                    new SQLiteParameter("@ItemCode", DbType.Int32),
                    new SQLiteParameter("@ItemPrice", DbType.Int32),
                    //new SQLiteParameter("@Quantity",DbType.Int32),
                    new SQLiteParameter("@ItemDescription",DbType.String)
                    };

                    sqlParams[0].Value = Person.ID;
                    sqlParams[1].Value = Person.Age;
                    sqlParams[2].Value = Person.Email;
                    sqlParams[3].Value = Person.FirstName;
                    sqlParams[4].Value = Person.LastName;
                    sqlParams[5].Value = item.ItemName;
                    sqlParams[6].Value = item.ItemCode;
                    sqlParams[7].Value = item.ItemPrice;
                    sqlParams[8].Value = item.ItemDescription;

                    sqlCommand.Parameters.AddRange(sqlParams);

在下一行中,它跳转到第一个捕获,并说存在重复项。
                    rc = sqlCommand.ExecuteNonQuery();
                    if (rc == 1)
                    {
                        rc = 0;
                    }  
                }
                _sqlConnection.Close();


            }

在rc = sqlCommand.ExecuteNonQuery();之后;行,它跳转到此catch块:
            catch (SQLiteException ex)
            {
                if (ex.ErrorCode == SQLiteErrorCode.Constraint)
                {
                    rc = -1;
                }
                else
                {
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return rc;
        }
    }
}

最佳答案

没关系,我做对了,我将数据库中的ID设置为主键,我对其进行了更改,然后添加了所有数据。

关于list - SQLite给出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21555390/

相关文章:

c# - SQLite-Net Extensions - CreateTable 函数是否会改变我现有的表?

android - Rxjava : Insert only works with single but not with completable

json - jq : continue parsing ignoring invalid literals

c - 如何按字母顺序在列表中插入结构体

python - 如何通过索引替换 python 上的列表项

c - 链表队列。坚持指针(我认为)

sqlite - 确定何时解压缩数据库 zip

python - Heroku 上的 Django : relation "app_label" does not exist

php - PHP中的http_response_code()

c# - List<T> 应该是私有(private)的吗?