我正在使用 SQLite数据库。当我执行以下代码时,显示错误数据库已锁定。请帮我哪里错了以及如何解决数据库锁定错误 以下是我的代码:
Try
Dim i As Integer = 0
Using con As New SQLiteConnection(constring)
Dim insertsquery As String = "insert into EnergyMeterDetails(DateTime, ElectricitySupplyer, ElectricityMeterNo,ElectricityMeterModelName, MultiplyingFactor, ConsumerNo, TarrifCode, Category,TypeOfSupply, HT_LT, EnergyMeterNo, EnergyMeterName, EnergyMeterLocation, EnergyMeterModel)VALUES(@DateTime, @ElectricitySupplyer, @ElectricityMeterNo,@ElectricityMeterModelName, @MultiplyingFactor, @ConsumerNo, @TarrifCode, @Category,@TypeOfSupply, @HT_LT,@EnergyMeterNo, @EnergyMeterName, @EnergyMeterLocation, @EnergyMeterModel)"
Dim cmdsql As New SQLiteCommand(insertsquery, con)
For i = 0 To DataGridView1.Rows.Count - 2
cmdsql.Parameters.Add(New SQLiteParameter("@DateTime", Format(Now, "yyyy-MM-dd HH:mm:ss")))
cmdsql.Parameters.Add(New SQLiteParameter("@ElectricityMeterNo", DataGridView1.Rows(i).Cells(0).Value))
cmdsql.Parameters.Add(New SQLiteParameter("@ElectricitySupplyer", DataGridView1.Rows(i).Cells(1).Value))
cmdsql.Parameters.Add(New SQLiteParameter("@ElectricityMeterModelName", DataGridView1.Rows(i).Cells(2).Value))
cmdsql.Parameters.Add(New SQLiteParameter("@MultiplyingFactor", DataGridView1.Rows(i).Cells(3).Value))
cmdsql.Parameters.Add(New SQLiteParameter("@ConsumerNo", DataGridView1.Rows(i).Cells(4).Value))
cmdsql.Parameters.Add(New SQLiteParameter("@TarrifCode", DataGridView1.Rows(i).Cells(5).Value))
cmdsql.Parameters.Add(New SQLiteParameter("@Category", DataGridView1.Rows(i).Cells(6).Value))
cmdsql.Parameters.Add(New SQLiteParameter("@TypeOfSupply", DataGridView1.Rows(i).Cells(7).Value))
cmdsql.Parameters.Add(New SQLiteParameter("@HT_LT", DataGridView1.Rows(i).Cells(8).Value))
cmdsql.Parameters.Add(New SQLiteParameter("@EnergyMeterNo", DataGridView1.Rows(i).Cells(9).Value))
cmdsql.Parameters.Add(New SQLiteParameter("@EnergyMeterName", DataGridView1.Rows(i).Cells(10).Value))
cmdsql.Parameters.Add(New SQLiteParameter("@EnergyMeterLocation", DataGridView1.Rows(i).Cells(11).Value))
cmdsql.Parameters.Add(New SQLiteParameter("@EnergyMeterModel", DataGridView1.Rows(i).Cells(12).Value))
con.Open()
If cmdsql.ExecuteNonQuery() > 0 Then
MessageBox.Show("DATA ADDED SUCCESSFULLY")
status = True
End If
con.Close()
Next
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
预先感谢
答案 0 :(得分:0)
不要在循环内不断添加参数。只需将它们添加一次,然后在循环内重置值即可。
Private Sub SaveToSQLite()
Try
Dim i As Integer = 0
Using con As New SQLiteConnection(constring)
Dim insertsquery As String = "insert into EnergyMeterDetails(DateTime, ElectricitySupplyer, ElectricityMeterNo,ElectricityMeterModelName, MultiplyingFactor, ConsumerNo, TarrifCode, Category,TypeOfSupply, HT_LT, EnergyMeterNo, EnergyMeterName, EnergyMeterLocation, EnergyMeterModel)VALUES(@DateTime, @ElectricitySupplyer, @ElectricityMeterNo,@ElectricityMeterModelName, @MultiplyingFactor, @ConsumerNo, @TarrifCode, @Category,@TypeOfSupply, @HT_LT,@EnergyMeterNo, @EnergyMeterName, @EnergyMeterLocation, @EnergyMeterModel)"
Dim cmdsql As New SQLiteCommand(insertsquery, con)
cmdsql.Parameters.Add("@DateTime", DbType.String)
cmdsql.Parameters.Add("@ElectricityMeterNo", DbType.Int64)
cmdsql.Parameters.Add("@ElectricitySupplyer", DbType.String)
cmdsql.Parameters.Add("@ElectricityMeterModelName", DbType.String)
cmdsql.Parameters.Add("@MultiplyingFactor", DbType.Decimal)
cmdsql.Parameters.Add("@ConsumerNo", DbType.Int64)
cmdsql.Parameters.Add("@TarrifCode", DbType.String)
cmdsql.Parameters.Add("@Category", DbType.String)
cmdsql.Parameters.Add("@TypeOfSupply", DbType.String)
cmdsql.Parameters.Add("@HT_LT", DbType.String)
cmdsql.Parameters.Add("@EnergyMeterNo", DbType.Int64)
cmdsql.Parameters.Add("@EnergyMeterName", DbType.String)
cmdsql.Parameters.Add("@EnergyMeterLocation", DbType.String)
cmdsql.Parameters.Add("@EnergyMeterModel", DbType.String)
con.Open()
For i = 0 To DataGridView1.Rows.Count - 2
cmdsql.Parameters("@DateTime").Value = Format(Now, "yyyy-MM-dd HH:mm:ss")
cmdsql.Parameters("@ElectricityMeterNo").Value = DataGridView1.Rows(i).Cells(0).Value
cmdsql.Parameters("@ElectricitySupplyer").Value = DataGridView1.Rows(i).Cells(1).Value
cmdsql.Parameters("@ElectricityMeterModelName").Value = DataGridView1.Rows(i).Cells(2).Value
cmdsql.Parameters("@MultiplyingFactor").Value = DataGridView1.Rows(i).Cells(3).Value
cmdsql.Parameters("@ConsumerNo").Value = DataGridView1.Rows(i).Cells(4).Value
cmdsql.Parameters("@TarrifCode").Value = DataGridView1.Rows(i).Cells(5).Value
cmdsql.Parameters("@Category").Value = DataGridView1.Rows(i).Cells(6).Value
cmdsql.Parameters("@TypeOfSupply").Value = DataGridView1.Rows(i).Cells(7).Value
cmdsql.Parameters("@HT_LT").Value = DataGridView1.Rows(i).Cells(8).Value
cmdsql.Parameters("@EnergyMeterNo").Value = DataGridView1.Rows(i).Cells(9).Value
cmdsql.Parameters("@EnergyMeterName").Value = DataGridView1.Rows(i).Cells(10).Value
cmdsql.Parameters("@EnergyMeterLocation").Value = DataGridView1.Rows(i).Cells(11).Value
cmdsql.Parameters("@EnergyMeterModel").Value = DataGridView1.Rows(i).Cells(12).Value
cmdsql.ExecuteNonQuery()
status = True
Next
MessageBox.Show("DATA ADDED SUCCESSFULLY")
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
我刚刚猜到了数据类型。检查数据库中的实际类型。