且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

VB.Net-在Gridview中显示数据

更新时间:2023-02-16 12:12:28

您必须设置sqlcommand的连接字符串:

you have to set the connection string for the sqlcommand:

cmd.Connection = New SqlConnection(connStr)



这是在Try/Catch块中仅放置一行的另一个很好的理由.当您有多行代码时,如果不看一下StackTrace,您有时就无法分辨出错误的出处.

因此,我会将cmd.Connection.Open放在Try/Cath中,而将cmd.ExecuteReader放在Try/Catch中.

如果打开,关闭只会抛出错误.

处理不会引发错误. GridView.DataBind也不会.



This is another good reason to only put a single line within a Try/Catch block. When you have multiple lines of code, if you don''t look at the StackTrace, you can''t tell where the error came from sometimes.

So, I would put the cmd.Connection.Open in a Try/Cath and the cmd.ExecuteReader in a Try/Catch.

Close will only throw an error if Open does.

Dispose won''t throw an error. Neither will GridView.DataBind.