问题描述
大家好
我具有此功能,但它会警告我.
Hi guys
I have this function but it gives me a warning.
Public Function IsConnected(ByVal strQry As String, ByVal ver As Boolean)
Try
If myConn.State = ConnectionState.Open Then myConn.Close()
myConn.ConnectionString = Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & dbpath & '"; Jet OLEDB:database password="
myConn.Open()
myCmd.CommandText = strQry
myCmd.Connection = myConn
If ver = False Then
myDR = myCmd.ExecuteReader() For reading query
Else
myCmd.ExecuteNonQuery() For updating query
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Function
怎么了;
谢谢
What is wrong;
Thanks
推荐答案
myConn.ConnectionString = Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & dbpath & '"; Jet OLEDB:database password="
2.没有return语句,因此给出的示例甚至不应该编译,因为它被声明为函数而不是子函数
3.使用MsgBox会将功能牢固地耦合到Windows窗体应用程序,例如,您将无法在ASP.net中使用此功能
4."IsConnected"是该函数的一个完全令人误解的名称,因为它实际上并没有告诉您是否已连接.看起来它运行SQL语句.
5.以下代码:
2. There is no return statement, so the example given shouldn''t even compile because it''s declared as a function rather than a sub
3. The use of a MsgBox strongly couples the function to a windows forms application, you wouldn''t be able to use this in ASP.net for example
4. "IsConnected" is a totally misleading name for the function as it doesn''t actually tell you if you''re connected or not. It looks like it runs SQL statements.
5. The following code:
If ver = False Then
myDR = myCmd.ExecuteReader() For reading query
Else
myCmd.ExecuteNonQuery() For updating query
End If
应该简化为:
should be simplified to:
If ver Then
myCmd.ExecuteNonQuery() For updating query
Else
myDR = myCmd.ExecuteReader() For reading query
End If
因为您正在评估If
because you''re evaluating a boolean in the If
[Modifier] Function FunctionName [Args] [as type]
' Your code here.
Return [type] 'Same as declared in the heading.
End Function
像这样将其更改为子项...
Change it to a sub, like so...
Public Sub IsConnected(ByVal strQry As String, ByVal ver As Boolean)
'Your code here.
End Sub
了解有关VB中的 Subs and Functions的信息. Net 或此处
如果您的代码有其他问题,请参见解决方案1
Learn about Subs and Functions in VB.Net or here
For other things wrong with your code... See Solution 1
这篇关于该函数哪里出了问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!