这个SQL命令有什么问题?

时间:2015-12-04 01:36:47

标签: mysql vb.net connector

解决方案:我注意到我传递的变量Username的长度超过了65k。这让我看到我设置了ReceiveBufferSize错误。只有在我需要检查唯一值时,这才会变得明显,并且该值是流中的最后一个值。在我的“working”命令中,Username是第二个命令。如果您处于类似情况,请使用QuickWatch并密切关注。可能是空格导致这些值不匹配。

Public Function Before_Logon(ByVal Username As String) As String
            Try
                mySqlCon.Open()
                Dim sqlCommandPreLogon As MySqlCommand = New MySqlCommand("SELECT username FROM user WHERE username=@username", mySqlCon)
                sqlCommandPreLogon.Parameters.AddWithValue("@username", Username)
                Dim dr As MySqlDataReader = sqlCommandPreLogon.ExecuteReader()
                While dr.Read
                    Return dr.Item(0).ToString
                End While
            Catch ex As Exception
            End Try
            Return 6
        End Function

我在另一个函数中有一个这样的命令,它工作正常。这个找不到任何东西。删除Try / Catch显示没有错误。

这也不起作用。

 Dim sqlCommandPreLogon As MySqlCommand = New MySqlCommand("SELECT uid FROM user WHERE username='" & Username & "'", mySqlCon)

奇怪的是,这确实有效。

 Dim sqlCommandPreLogon As MySqlCommand = New MySqlCommand("SELECT uid FROM user WHERE username='user'", mySqlCon)

为什么变量/ sql参数值不起作用,但这样做呢?

更新:通过QuickWatch查看参数时似乎没有错。第一个是工作命令,第二个是工作命令。

test    {@uname}    MySql.Data.MySqlClient.MySqlParameter
BaseName    uname   String
Collection  {MySql.Data.MySqlClient.MySqlParameterCollection}   MySql.Data.MySqlClient.MySqlParameterCollection
DbType  String {16} System.Data.DbType
Direction   Input {1}   System.Data.ParameterDirection
Encoding    Nothing System.Text.Encoding
IsNullable  FALSE   Boolean
MySqlDbType VarChar {253}   MySql.Data.MySqlClient.MySqlDbType
ParameterName   @uname  String
PossibleValues  Nothing System.Collections.IList
Precision   0   Byte
Precision (DbParameter) 0   Byte
Scale   0   Byte
Scale (DbParameter) 0   Byte
Size    65533   Integer
SourceColumn    Nothing String
SourceColumnNullMapping FALSE   Boolean
SourceVersion   Current {512}   System.Data.DataRowVersion
TypeHasBeenSet  TRUE    Boolean
Value   new {String}    Object
String  new String
ValueObject {MySql.Data.Types.MySqlString}  MySql.Data.Types.IMySqlValue
MySql.Data.Types.MySqlString    {MySql.Data.Types.MySqlString}  MySql.Data.Types.MySqlString
IsNull  TRUE    Boolean
Value       String

test    {@uname}    MySql.Data.MySqlClient.MySqlParameter
BaseName    uname   String
Collection  {MySql.Data.MySqlClient.MySqlParameterCollection}   MySql.Data.MySqlClient.MySqlParameterCollection
DbType  String {16} System.Data.DbType
Direction   Input {1}   System.Data.ParameterDirection
Encoding    Nothing System.Text.Encoding
IsNullable  FALSE   Boolean
MySqlDbType VarChar {253}   MySql.Data.MySqlClient.MySqlDbType
ParameterName   @uname  String
PossibleValues  Nothing System.Collections.IList
Precision   0   Byte
Precision (DbParameter) 0   Byte
Scale   0   Byte
Scale (DbParameter) 0   Byte
Size    3   Integer
SourceColumn    Nothing String
SourceColumnNullMapping FALSE   Boolean
SourceVersion   Current {512}   System.Data.DataRowVersion
TypeHasBeenSet  TRUE    Boolean
Value   new {String}    Object
String  new String
ValueObject {MySql.Data.Types.MySqlString}  MySql.Data.Types.IMySqlValue
MySql.Data.Types.MySqlString    {MySql.Data.Types.MySqlString}  MySql.Data.Types.MySqlString
IsNull  TRUE    Boolean
Value       String

以下是'user'' & Username & '之间的差异。

test    {@uname}    MySql.Data.MySqlClient.MySqlParameter
BaseName    uname   String
Collection  {MySql.Data.MySqlClient.MySqlParameterCollection}   MySql.Data.MySqlClient.MySqlParameterCollection
DbType  String {16} System.Data.DbType
Direction   Input {1}   System.Data.ParameterDirection
Encoding    Nothing System.Text.Encoding
IsNullable  FALSE   Boolean
MySqlDbType VarChar {253}   MySql.Data.MySqlClient.MySqlDbType
ParameterName   @uname  String
PossibleValues  Nothing System.Collections.IList
Precision   0   Byte
Precision (DbParameter) 0   Byte
Scale   0   Byte
Scale (DbParameter) 0   Byte
Size    65533   Integer
SourceColumn    Nothing String
SourceColumnNullMapping FALSE   Boolean
SourceVersion   Current {512}   System.Data.DataRowVersion
TypeHasBeenSet  TRUE    Boolean
Value   user {String}   Object
String  user    String
ValueObject {MySql.Data.Types.MySqlString}  MySql.Data.Types.IMySqlValue
MySql.Data.Types.MySqlString    {MySql.Data.Types.MySqlString}  MySql.Data.Types.MySqlString
IsNull  TRUE    Boolean
Value       String

test    {@uname}    MySql.Data.MySqlClient.MySqlParameter
BaseName    uname   String
Collection  {MySql.Data.MySqlClient.MySqlParameterCollection}   MySql.Data.MySqlClient.MySqlParameterCollection
DbType  String {16} System.Data.DbType
Direction   Input {1}   System.Data.ParameterDirection
Encoding    Nothing System.Text.Encoding
IsNullable  FALSE   Boolean
MySqlDbType VarChar {253}   MySql.Data.MySqlClient.MySqlDbType
ParameterName   @uname  String
PossibleValues  Nothing System.Collections.IList
Precision   0   Byte
Precision (DbParameter) 0   Byte
Scale   0   Byte
Scale (DbParameter) 0   Byte
Size    65533   Integer
SourceColumn    Nothing String
SourceColumnNullMapping FALSE   Boolean
SourceVersion   Current {512}   System.Data.DataRowVersion
TypeHasBeenSet  TRUE    Boolean
Value   user {String}   Object
String  user    String
ValueObject {MySql.Data.Types.MySqlString}  MySql.Data.Types.IMySqlValue
MySql.Data.Types.MySqlString    {MySql.Data.Types.MySqlString}  MySql.Data.Types.MySqlString
IsNull  TRUE    Boolean
Value       String

1 个答案:

答案 0 :(得分:1)

你说

这也不起作用。

 Dim sqlCommandPreLogon As MySqlCommand = New MySqlCommand("SELECT uid FROM user WHERE username='" & Username & "'", mySqlCon)

奇怪的是,这确实有效。

 Dim sqlCommandPreLogon As MySqlCommand = New MySqlCommand("SELECT uid FROM user WHERE username='user'", mySqlCon)

那怎么样呢

Dim sSql1 As String
Dim sSql2 As String

sSql1 = "SELECT uid FROM user WHERE username='" & Username & "'"
sSql2 = "SELECT uid FROM user WHERE username='user'"

在sSql1和sSql2上执行QuickWatch并比较它们