且构网

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

将参数发送到存储过程 vb.net

更新时间:2023-02-13 15:27:53

如果您只是读取数据,请查看 SqlDataReader:

If you're just reading data then checkout the SqlDataReader:

Dim reader As SqlDataReader
reader = cmd.ExecuteReader()
While reader.Read
    //Do stuff with reader
End While

如果您正在执行更新或插入操作,则可以使用 SqlCommand 类的 ExecuteNonQuery() 方法.

If you are doing an update or an insert then you can use the ExecuteNonQuery() method of the SqlCommand class.

SqlCommand 有一个添加参数的简写:

SqlCommand has a shorthand for adding parameters:

cmd.Parameters.AddWithValue("@MyParamName", myParamValue)

您可能会觉得有用.

是的,每次需要与数据库交互时都应该打开和关闭数据库连接.仔细阅读 Using 语句,这将帮助您很好地完成这项工作.

And yes you should open and close a database connection every time you need to interact with the database. Read up on the Using statement, which will help you to do this nice and neatly.