1

我在网上查看了很多示例,并尝试根据这些示例调整我的程序,但没有一个对我有帮助。请您指出我应该怎么做才能解决这个问题。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SQLite;
using Restaurant_app_1.Business_Layer;

namespace Restaurant_app_1.Data_Access_Layer
{
    class PersonSQLProvider : PersonProviderBase
    {
        private SQLiteConnection _sqlConnection;
        private string _connStr = "Data Source=c:\\DataStores\\PersonDatabase.s3db;Version=3";

在这里,它尝试将数据插入数据库,当它到达 .ExecuteNonQuery() 方法时,它会跳转到 catch 并向我抛出错误。

        public override int Insert(Manager Manager)
        {
            int rc = 0;

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

                string insertQuery = "INSERT INTO PersonDatabase([_staffFirstName], [_staffLastName], [_staffAge], [_staffID], [_staffeMail], [_staffNumber], [_staffStreet], [_staffProvince], [_staffCity], [_staffPostalCode], [_staffAreaCode], [_staffPhoneNumber], [_staffType]) VALUES(" +
                                     "@First name, @Last name, @Age, @ID, @eMail, @Staff number, @Street, @Province, @City, @Postal Code, @Area code, @Number, @Type)";
                SQLiteCommand sqlCommand = new SQLiteCommand(insertQuery, _sqlConnection);
                SQLiteParameter[] sqlParams = new SQLiteParameter[]
                {
                new SQLiteParameter("@First name",DbType.String),
                new SQLiteParameter("@Last name",DbType.String),
                new SQLiteParameter("@Age",DbType.Int32),
                new SQLiteParameter("@ID",DbType.String),
                new SQLiteParameter("@eMail",DbType.String),
                new SQLiteParameter("@Staff number",DbType.String),
                new SQLiteParameter("@Street",DbType.String),
                new SQLiteParameter("@Province",DbType.String),
                new SQLiteParameter("@City",DbType.String),
                new SQLiteParameter("@Postal Code",DbType.String),
                new SQLiteParameter("@Area code",DbType.String),
                new SQLiteParameter("@Number",DbType.String),
                new SQLiteParameter("@Type",DbType.String)
                };

                sqlParams[0].Value = Manager.FirstName;
                sqlParams[1].Value = Manager.LastName;
                sqlParams[2].Value = Manager.Age;
                sqlParams[3].Value = Manager.ID;
                sqlParams[4].Value = Manager.Email;
                sqlParams[5].Value = Manager.StaffNr;
                sqlParams[6].Value = Manager.AddressStreet;
                sqlParams[7].Value = Manager.AddressProvince;
                sqlParams[8].Value = Manager.AddressCity;
                sqlParams[9].Value = Manager.AddressPostalCode;
                sqlParams[10].Value = Manager.AreaCode;
                sqlParams[11].Value = Manager.Number;
                sqlParams[12].Value = Manager.PhoneType;

                sqlCommand.Parameters.AddRange(sqlParams);
                rc = sqlCommand.ExecuteNonQuery();

                if (rc == 1)
                {
                    rc = 0;
                }
                _sqlConnection.Close();
            }
            catch (SQLiteException ex)
            {
                if (ex.ErrorCode == SQLiteErrorCode.Constraint)
                {
                    rc = -1;
                }
                else
                {
                    throw ex;  
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return rc;
        }
    }
}
4

2 回答 2

3

我认为参数名称中不能有空格

@First name

@Last name

@Staff number

@Postal Code

@Area code

这些都会有同样的问题。删除空间,它应该可以工作

@FirstName

@LastName

@StaffNumber

@PostalCode

@AreaCode

于 2013-11-11T20:19:24.657 回答
2

变量名不能包含空格。改变

@First name, @Last name,

@Firstname, @Lastname,

例如。

于 2013-11-11T20:18:53.247 回答