1

我很难知道我拥有的这个框架是否是在 VB 2008 的 gridview 的表格中显示所有用户及其角色的正确方法。我已经布置好了所有内容,我只需要确保后面的代码是正确的。我有一个带有用户登录名和密码的初始页面,以及一个允许输入正确信息的按钮,它将导致访问数据库并在 gridview 中生成所有用户及其角色的表的下一页。

看看代码,如果这是正确的想法,请告诉我。

Dim mySqlConnection As SqlConnection

mySqlConnection = New SqlConnection("Data Source=(local)\SQLExpress;" _
+"Password=myPassword; " _
    +"Persist Security Info=True;" _
    +"User ID=mcobery;" _
    +"Initial Catalog=model;")

Try
    mySqlConnection.Open()
    Console.WriteLine("Opened Connection to {0}",_
        mySqlConnection.ConnectionString)

' Close the connection explicitly
mySqlConnection.Close()
Console.WriteLine("Closed Connection. " _
    +"It is important to close connections explicitly.")

Catch
    Console.WriteLine("Couldn't Open Connection to {0}", _ 
        mySqlConnection.ConnectionString)

End Try

如您所见,这是一个框架,我将填写正确的用户名和密码。

请让我知道这是否是正确的代码。

4

1 回答 1

1

Hard to know where to start here.

First thing

Using myConnection as new SqlConnection(someConnectionString)
  myConnection.Open()
  ...

End Using

When myConnection goes out of scope it will get closed, well actually it will go back in to the connection pool. You need to read up on that.

The GC will tidy up for you, but as basic priniciple, what closes your connection if you get an exception? You said it was very important in your comment.

You create a connection, you do something, you close it, it goes out of scope, so why are you persisting security info?

Don't put your passwords in the code. They should be in the config. In fact all your connection string should. That would also help out, when you want to test in a staging environment. Or when the system boys tell you sql server and IIS are on different machines.

Why are you using sql server security by the way?

How are you going to unit test this?

Keep going there are plenty far more experienced than yourself making worse mistakes than these. If I were you though I'd pick up a best practices book. Some of it will go over your head until you learn a bit more, but get a sense of the dos and don'ts.

于 2013-02-22T02:33:58.823 回答