C# SQLite数据库锁问题
我在这个网站上读了很多关于SQLite数据库锁的帖子,但我仍然找不到造成这种情况的原因 我正在尝试在插入之前检查表中是否有记录。 当我尝试插入时,我得到了数据库锁。(这有时不是一开始就发生的,我也不明白为什么。) 这是我的密码C# SQLite数据库锁问题,c#,database,sqlite,locking,C#,Database,Sqlite,Locking,我在这个网站上读了很多关于SQLite数据库锁的帖子,但我仍然找不到造成这种情况的原因 我正在尝试在插入之前检查表中是否有记录。 当我尝试插入时,我得到了数据库锁。(这有时不是一开始就发生的,我也不明白为什么。) 这是我的密码 private void bt_save_Click(object sender, EventArgs e) { if (!SaveValidation()) // <--- I am checking for duplication
private void bt_save_Click(object sender, EventArgs e)
{
if (!SaveValidation()) // <--- I am checking for duplication
{
DialogResult = DialogResult.None;
return;
}
int result = Schedule.addSchedule( // <--- I am inserting
medicNumber,
dtp_date.Value,
Int16.Parse(cb_hour.Text),
Int16.Parse(cb_minute.Text),
Int16.Parse(cb_duration.Text),
tb_patient.Text,
tb_memo.Text);
if (result > 0)
{
MessageBox.Show("Saved succesfully.");
}
else
{
MessageBox.Show("Failed to save.");
DialogResult = DialogResult.None;
}
}
private bool SaveValidation()
{
DateTime now = DateTime.Now;
DateTime appointment;
appointment = dtp_date.Value;
appointment = appointment.AddHours(Int16.Parse(cb_hour.Text));
appointment = appointment.AddMinutes(Int16.Parse(cb_minute.Text) + 1);
if (now > appointment)
{
MessageBox.Show("Cannot make appointment in the past." + appointment.ToString());
cb_hour.Focus();
return false;
}
if (tb_patient.TextLength == 0)
{
MessageBox.Show("Patient name is missing.");
tb_patient.Focus();
return false;
}
if (!Schedule.IsScheduleAvailable(medicNumber, dtp_date.Value.Date, Int16.Parse(cb_hour.Text),
Int16.Parse(cb_minute.Text), Int16.Parse(cb_duration.Text)))
{
MessageBox.Show("Already booked schedule exists at that time.");
cb_hour.Focus();
return false;
}
return true;
}
private void bt\u save\u单击(对象发送方,事件参数e)
{
如果(!SaveValidation())//约会)
{
Show(“过去不能预约。”+appoint.ToString());
cb_hour.Focus();
返回false;
}
如果(tb_patient.TextLength==0)
{
MessageBox.Show(“缺少患者姓名”);
tb_患者。焦点();
返回false;
}
如果(!Schedule.isschedule)可用(medicNumber、dtp_date.Value.date、Int16.Parse(cb_hour.Text),
Parse(cb_minute.Text),Int16.Parse(cb_duration.Text)))
{
Show(“当时存在已预订的日程安排”);
cb_hour.Focus();
返回false;
}
返回true;
}
下面是Schedule的数据模型类
static class Schedule
{
private static SQLiteConnection DbConnection()
{
SQLiteConnectionStringBuilder builder = new SQLiteConnectionStringBuilder();
builder.DataSource = "database.sqlite";
SQLiteConnection conn = new SQLiteConnection(builder.ToString());
conn.Open();
return conn;
}
public static bool IsScheduleAvailable(int medicNo, DateTime value_date, int hour, int minute, int duration)
{
bool returnValue = false;
using (SQLiteConnection conn = DbConnection())
{
string query = "select * from tb_schedule where medicNo = @medicNo and"
+ " ( datetime(@value_date, '+'||@hour||' hour', '+'||@minute||' minute') between "
+ "datetime(value_date, '+'||hour||' hour', '+'||minute||' minute') and "
+ "datetime(value_date, '+'||hour||' hour', '+'||minute||' minute', '+'||duration||' minute')"
+ " or datetime(@value_date, '+'||@hour||' hour', '+'||@minute||' minute', '+'||@duration||' minute') between "
+ "datetime(value_date, '+'||hour||' hour', '+'||minute||' minute') and "
+ "datetime(value_date, '+'||hour||' hour', '+'||minute||' minute', '+'||duration||' minute') )";
using (SQLiteCommand cmd = new SQLiteCommand(query, conn))
{
SQLiteParameter parm0 = new SQLiteParameter("@medicNo", DbType.Int16);
SQLiteParameter parm1 = new SQLiteParameter("@value_date", DbType.DateTime);
SQLiteParameter parm2 = new SQLiteParameter("@hour", DbType.Int16);
SQLiteParameter parm3 = new SQLiteParameter("@minute", DbType.Int16);
SQLiteParameter parm4 = new SQLiteParameter("@value_date", DbType.DateTime);
SQLiteParameter parm5 = new SQLiteParameter("@hour", DbType.Int16);
SQLiteParameter parm6 = new SQLiteParameter("@minute", DbType.Int16);
SQLiteParameter parm7 = new SQLiteParameter("@duration", DbType.Int16);
parm0.Value = medicNo;
parm1.Value = value_date;
parm2.Value = hour;
parm3.Value = minute;
parm4.Value = value_date;
parm5.Value = hour;
parm6.Value = minute;
parm7.Value = duration;
cmd.Parameters.Add(parm0);
cmd.Parameters.Add(parm1);
cmd.Parameters.Add(parm2);
cmd.Parameters.Add(parm3);
cmd.Parameters.Add(parm4);
cmd.Parameters.Add(parm5);
cmd.Parameters.Add(parm6);
cmd.Parameters.Add(parm7);
using (SQLiteDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
returnValue = false;
}
else
{
returnValue = true;
}
reader.Close();
}
cmd.Dispose();
}
conn.Close();
}
//SQLiteConnection.ClearAllPools();
GC.Collect();
return returnValue;
}
public static int addSchedule(int medicNo, DateTime value_date, int hour, int minute, int duration, string patient, string memo)
{
string query = "insert into tb_schedule ( medicNo, value_date, hour, minute, duration, patient, memo )"
+ "values ( @medicNo, @value_date, @hour, @minute, @duration, @patient, @memo )";
SQLiteConnection conn = DbConnection();
SQLiteCommand cmd = new SQLiteCommand(query, conn);
SQLiteParameter parm0 = new SQLiteParameter("@medicNo", DbType.Int16);
SQLiteParameter parm1 = new SQLiteParameter("@value_date", DbType.DateTime);
SQLiteParameter parm2 = new SQLiteParameter("@hour", DbType.Int16);
SQLiteParameter parm3 = new SQLiteParameter("@minute", DbType.Int16);
SQLiteParameter parm4 = new SQLiteParameter("@duration", DbType.Int16);
SQLiteParameter parm5 = new SQLiteParameter("@patient", DbType.String);
SQLiteParameter parm6 = new SQLiteParameter("@memo", DbType.String);
parm0.Value = medicNo;
parm1.Value = value_date.Date;
parm2.Value = hour;
parm3.Value = minute;
parm4.Value = duration;
parm5.Value = patient;
parm6.Value = memo;
cmd.Parameters.Add(parm0);
cmd.Parameters.Add(parm1);
cmd.Parameters.Add(parm2);
cmd.Parameters.Add(parm3);
cmd.Parameters.Add(parm4);
cmd.Parameters.Add(parm5);
cmd.Parameters.Add(parm6);
int result = 0;
try
{
using (SQLiteTransaction transaction = conn.BeginTransaction())
{
using (cmd)
{
result = cmd.ExecuteNonQuery();
}
transaction.Commit(); // <--- This is where I get the database lock error
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
conn.Close();
cmd.Dispose();
SQLiteConnection.ClearAllPools();
GC.Collect();
return result;
}
}
静态课程表
{
私有静态SQLiteConnection DbConnection()
{
SQLiteConnectionStringBuilder=新SQLiteConnectionStringBuilder();
builder.DataSource=“database.sqlite”;
SQLiteConnection conn=新的SQLiteConnection(builder.ToString());
conn.Open();
返回连接;
}
公共静态bool IsScheduleAvailable(int medicNo、DateTime值_date、int hour、int minute、int duration)
{
bool returnValue=false;
使用(SQLiteConnection conn=DbConnection())
{
string query=“从tb\U计划中选择*,其中medicNo=@medicNo和”
+“(日期时间(@value_date,“+”|小时,“+”|分钟,“+”|小时,“+”|分钟)介于”
+“日期时间(值为日期,”+“|小时,”+“|分钟,”)和”
+“日期时间(值为日期,”+“| | |小时,”+“| | |分钟,”+“| | |持续时间| |分钟”)
+“或日期时间(@value|u date,“+”| | |小时,“+”| |分钟,“+”| | | |分钟,“+”| |持续时间| | |分钟”)介于”
+“日期时间(值为日期,”+“|小时,”+“|分钟,”)和”
+“日期时间(值为日期,”+“|小时,”+“|分钟,”+“|持续时间,”);
使用(SQLiteCommand cmd=newsqlitecommand(查询,连接))
{
SQLiteParameter parm0=新的SQLiteParameter(“@medicNo”,DbType.Int16);
SQLiteParameter parm1=新的SQLiteParameter(“@value\u date”,DbType.DateTime);
SQLiteParameter parm2=新的SQLiteParameter(“@hour”,DbType.Int16);
SQLiteParameter parm3=新的SQLiteParameter(“@minute”,DbType.Int16);
SQLiteParameter parm4=新的SQLiteParameter(“@value\u date”,DbType.DateTime);
SQLiteParameter parm5=新的SQLiteParameter(“@hour”,DbType.Int16);
SQLiteParameter parm6=新的SQLiteParameter(“@minute”,DbType.Int16);
SQLiteParameter parm7=新的SQLiteParameter(“@duration”,DbType.Int16);
parm0.Value=medicNo;
parm1.Value=价值\日期;
parm2.值=小时;
parm3.值=分钟;
parm4.Value=价值\日期;
parm5.值=小时;
parm6.值=分钟;
parm7.值=持续时间;
cmd.Parameters.Add(parm0);
cmd.Parameters.Add(parm1);
命令参数添加(parm2);
命令参数添加(parm3);
命令参数添加(parm4);
cmd.Parameters.Add(parm5);
cmd.Parameters.Add(parm6);
命令参数添加(parm7);
使用(SQLiteDataReader=cmd.ExecuteReader())
{
if(reader.HasRows)
{
returnValue=false;
}
其他的
{
returnValue=true;
}
reader.Close();
}
cmd.Dispose();
}
康涅狄格州关闭();
}
//SQLiteConnection.ClearAllPools();
GC.Collect();
返回值;
}
公共静态int addSchedule(int medicNo、DateTime值\ u date、int hour、int minute、int duration、string patient、string memo)
{
string query=“插入tb_计划(医疗、价值、日期、小时、分钟、持续时间、患者、备忘录)”
+“价值观(@medicNo、@value_date、@hour、@minute、@duration、@patient、@memo)”;
SQLiteConnection conn=DbConnection();
SQLiteCommand cmd=新的SQLiteCommand(查询,连接);
SQLiteParameter parm0=新的SQLiteParameter(“@medicNo”,DbType.Int16);
SQLiteParameter parm1=新的SQLiteParameter(“@value\u date”,DbType.DateTime);
SQLiteParameter parm2=新的SQLiteParameter(“@hour”,DbType.Int16);
SQLiteParameter parm3=新的SQLiteParameter(“@minute”,DbType.Int16);
SQLiteParameter parm4=新的SQLiteParameter(“@duration”,DbType.Int16);
SQLiteParameter parm5=新的SQLiteParameter(“@patient”,DbType.String);
SQLiteParameter parm6=新的SQLiteParameter(“@memo”,DbType.String);
parm0.Value=medicNo;
parm1.Value=Value_date.date;
parm2.值=小时;
parm3.值=分钟;
parm4.值=持续时间;
parm5.值=患者;
parm6.Value=memo;
cmd.Parameters.Add(parm0);
cmd.Parameters.Add(parm1);
命令参数添加(parm2);
命令参数添加(parm3);
命令参数添加(parm4);
命令参数