【发布时间】:2013-02-06 20:30:51
【问题描述】:
当我运行这个函数时
For RepeatBooking = 1 To 51
dateConvertedDateToBook = dateDateToBook.Date
dateDateToBook = dateDateToBook.AddDays(7)
strDateToBook = dateConvertedDateToBook.ToString("yyyy-MM-dd")
Try
Dim command As MySqlCommand = New MySqlCommand
Dim sqlQuery As String = "INSERT INTO bookings SET Date=" & "'" & strDateToBook & "',RoomID='" & strComputerRoomToBook & "',Length='" & intNewBookingLength & "',Period='" & intNewStartPeriod & "',UserID='" & intid & "'"
Dim reader As MySqlDataReader
SQLConnection.Open()
command.CommandText = sqlQuery
command.Connection = SQLConnection
reader = command.ExecuteReader
SQLConnection.Close()
Catch excep As Exception
MsgBox(excep.ToString)
End Try
Next
在我的程序中,我收到一条错误消息“连接属性尚未设置或为空” 我怎样才能摆脱它?
当它到达 SQLconnection.Open() 时会出现异常 我在模块顶部创建了 ServerString 和 MySQL 连接,如下所示:
Dim ServerString As String = "Server=localhost;User Id=root;Password=**********;Database=rooms"
Dim SQLConnection As MySqlConnection = New MySqlConnection
【问题讨论】:
-
哪一行出现异常?你能在你声明和初始化
SQLConnection的地方展示你的代码吗? -
不要连接 sql-strings 而是使用参数,不要使用
string代替date,使用Using-statement(或Try-Catch-Finally)来确保您的连接被关闭. -
我已经更新了我的代码
标签: mysql vb.net exception date command