数据库在insert语句上被锁定

时间:2015-10-05 18:32:33

标签: c# database sqlite

当我尝试插入我的sqlite数据库时遇到问题。我有两张桌子看起来像这样:

CREATE TABLE user(UId INTEGER PRIMARY KEY ASC AUTOINCREMENT ,name VARCHAR(100) UNIQUE,phoneNumber INTEGER UNIQUE);

CREATE TABLE 'transaction'(TId INTEGER PRIMARY KEY ASC AUTOINCREMENT , loan REAL, trackUser INTEGER,FOREIGN KEY(trackUser) REFERENCES user(UId));

我可以插入用户表而不会出现这样的问题:

INSERT INTO user (name, phoneNumber) VALUES(@name,@phoneNumber)

但是当我尝试添加一个事务时,我的数据库被锁定了。我这样做:

    public void Add(Transaction trans,User user)
    {
        _conn.Open();
        string SQL = "INSERT INTO 'transaction' (loan, trackUser) VALUES(@loan,@userID);";
        string checkUserSQL = "SELECT EXISTS(SELECT 1 FROM user WHERE UId = @UId) LIMIT 1;";
        SQLiteCommand checkUserCommand = new SQLiteCommand(checkUserSQL,_conn);
        checkUserCommand.Parameters.Add(new SQLiteParameter("@UId", user.UId));
        var reader = checkUserCommand.ExecuteReader();
        reader.Read();
        var check = reader.GetBoolean(0);
        if (!check)
        {
            throw new UserDoesNotExcistException();
        }
        SQLiteCommand transCommand = new SQLiteCommand(SQL, _conn);
        transCommand.Parameters.Add(new SQLiteParameter("@loan",trans.Loan));
        transCommand.Parameters.Add(new SQLiteParameter("@userID",user.UId));
        transCommand.ExecuteNonQuery();
        _conn.Close();
    }

select语句检查用户exicst是否运行没有问题,但是当transCommand.ExecuteNonQuery();数据库被锁定时。我是以错误的方式绑定了forign键还是为什么我的数据库被锁定了?

0 个答案:

没有答案