0

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

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;
        }
    }
}
4

1 回答 1

0

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

于 2014-02-04T15:14:06.977 回答