您的位置:首页 > 编程语言 > VB

[VB.NET]高手来帮我看一下我的代码了??

2008-12-28 22:15 369 查看



<script type="text/javascript"><!--
google_ad_client = "pub-8333940862668978";
/* 728x90, 创建于 08-11-30 */
google_ad_slot = "4485230109";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

高手来帮我看一下我的代码了??
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
__________________________________________________________________________
你的连接已经写死,硬编码

看看微软的企业库中数据库操作部分
__________________________________________________________________________
请问楼上的兄弟“微软的企业库中数据库操作部分” 在那里看哦。。给个地址哦。。
谢谢拉
__________________________________________________________________________
需要学习。看微软的企业库中数据库操作部分
__________________________________________________________________________
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息