我尝试使用Visual Studio .Net 2008从MySql中获取一些数据,在此特定的连接字符串上,它给我错误,提示输入字符串的格式不正确。
Imports MySql.Data.MySqlClient
Public Class Form1
Dim Scaderea As String = "SELECT `No`,`Laps`,`Lap_Tm`, ((SELECT `Lap_Tm` from `raw` where `No` = 7 AND (`Lap_Tm`<>"""") AND `Laps`=1) - `Lap_Tm`) AS 'Diferente' FROM `raw` WHERE `No`=7 AND (`Lap_Tm`<>"""") AND Laps!=1 AND Laps>1"
Public Const cs As String = "server=************;User Id=**********;PASSWORD=**********;port=3306;Persist Security Info=True;database=circuit;Convert Zero Datetime=True;Allow Zero Datetime=True"
Public rdr As MySqlDataReader = Nothing
Public con As MySqlConnection = Nothing
Public cmd As MySqlCommand = Nothing
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DataGridView3.Rows.Clear()
DataGridView3.SuspendLayout()
'MsgBox(Scaderea)
Try
con = New MySqlConnection(cs)
con.Open()
cmd = New MySqlCommand(Scaderea, con)
rdr = cmd.ExecuteReader()
While (rdr.Read())
MsgBox(rdr.Item(0))
Dim Nr_Conc = rdr("No").ToString
Dim Tur = rdr("Laps").ToString
Dim Timp = rdr("Lap_Tm").ToString
Dim Diferenta = rdr("Diferente").ToString
DataGridView3.Rows.Add(Nr_Conc, Tur, Timp, Diferenta)
End While
con.Close()
Catch ex As Exception
If con.State = ConnectionState.Open Then con.Close()
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
con.Close()
con.Dispose()
End Try
DataGridView3.ResumeLayout()
End Sub
End Class
在PHPmyadmin上语法有效
SELECT `No`, `Laps`, `Lap_Tm`, (( SELECT `Lap_Tm` FROM `raw` WHERE `No` = 7 AND ( `Lap_Tm` <> "" ) AND `Laps` = 1 ) - `Lap_Tm`
) AS 'Diferente'
FROM
raw
WHERE
`No` = 7
AND ( `Lap_Tm` <> "" )
AND Laps != 1
AND Laps >1
结果是:
"7" "2" "00:02:46.853" "5.129"
"7" "3" "00:02:51.203" "0.779"
"7" "4" "00:02:45.291" "6.691"
答案 0 :(得分:0)
您应该始终将连接字符串与www.connectionstrings.com上的连接字符串进行比较。在这种情况下,您会有一些错误的拼写:
UserId
必须为Uid
Password
需要Pwd
Convert Zero Datetime
必须为ConvertZeroDatetime
答案 1 :(得分:0)
我通过将查询语法修改为:
Dim Scaderea As String = "SELECT No, Laps, TIME_FORMAT(Lap_Tm, ""%H:%i:%S.%f"") AS 'timp1', TIME_FORMAT(SEC_TO_TIME(ABS(( SELECT Lap_Tm FROM raw WHERE NO = 7 AND ( Lap_Tm <> '' ) AND Laps = 1 ) - Lap_Tm )) , ""%H:%i:%S.%f"") AS 'timp2' FROM raw WHERE No = 7 AND ( Lap_Tm <> '' ) AND Laps > 1"