该函数哪里出了问题? [英] Where is the wrong in this function;

查看:59
本文介绍了该函数哪里出了问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我具有此功能,但它会警告我.

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

推荐答案

如果您问我此功能有什么问题,我会说:

1.连接字符串引用不正确,并且看起来不完整

If you''re asking me what is wrong with this function, I''d say:

1. The connection string isn''t quoted correctly and looks incomplete

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


中的布尔值,所以您的函数不会返回值,是的.为什么?

因为,与subs函数不同,它们应该将结果返回给调用proc.每个函数都必须有一个return语句,这就是为什么您收到该警告.

我猜您想确定您是否已成功连接到那里的数据库.

在VB中像这样调用函数

Your function does not return a value, yes. Why?

Because, unlike subs functions are supposed to return a result to the calling proc. Every function must have a return statement and that is why you''re getting that warning.

I''m guessing you wanted to determine if you successfully connected to the database there.

Functions are called like so in VB

[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


这篇关于该函数哪里出了问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆