SQLite提供了Exception

时间:2014-02-04 14:38:58

标签: 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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

没关系,我做对了,我把数据库中的id设置为主键,然后我更改了所有数据。