首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

高手来帮小弟我看一下小弟我的代码了?

2012-01-09 
高手来帮我看一下我的代码了??ImportsSystem.DataImportsSystemImportsSystem.Data.SqlClientPublicClassC

高手来帮我看一下我的代码了??
Imports   System.Data
Imports   System
Imports   System.Data.SqlClient

Public   Class   Class1
        Public   myconn   As   SqlConnection
        Public   mycomm   As   SqlCommand
        Public   mydt   As   DataTable

        Public   Function   DBopen()   As   Boolean
                Try
                        myconn   =   New   SqlConnection( "Integrated   Security=SSPI;Persist   Security   Info=False;Initial   Catalog=pubs ")
                        myconn.Open()
                Catch   ex   As   Exception
                        MessageBox.Show(ex.ToString)
                End   Try

        End   Function
        Public   Function   DBclose()   As   Boolean
                Try
                        myconn.Close()
                        myconn   =   Nothing
                        myconn.Dispose()
                Catch   ex   As   Exception
                        MessageBox.Show(ex.ToString)
                End   Try
        End   Function
End   Class

这是我在一个类中定义数据库连接,,我觉得不太好,,但是我又不会改。。

请那位大哥大姐帮我给好一点咯。。
这样定义到页面上我要用到myconn时候,,我觉得不到他的返回值哦。
总觉得这样写的是在不怎么好的
望指正。。

[解决办法]
你定义的是函数,函数是需要返回值的,而你没有明确的指定。
你可以把你的函数改成子过程,也就是 sub

另外你也可以指定函数的返回值的。如:
Imports System.Data
Imports System
Imports System.Data.SqlClient

Public Class Class1
Public myconn As SqlConnection
Public mycomm As SqlCommand
Public mydt As DataTable

Public Function DBopen() As Boolean
Try
myconn = New SqlConnection( "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=pubs ")
myconn.Open()
dbopen=true
Catch ex As Exception
MessageBox.Show(ex.ToString)
dbopen=false
End Try

End Function
Public Function DBclose() As Boolean
Try
myconn.Close()
myconn = Nothing
myconn.Dispose()
dbclose=true
Catch ex As Exception
MessageBox.Show(ex.ToString)
declose =false
End Try
End Function
End Class
[解决办法]
你的连接已经写死,硬编码

看看微软的企业库中数据库操作部分
[解决办法]
需要学习。看微软的企业库中数据库操作部分

热点排行