Database connections in vb.net

Source: Internet
Author: User
Data | database | Database Connection Select the database adapter in the left "tool box" in the project, this example uses an ODBC data source, so select OdbcDataAdapter.
Select OdbcDataAdapter, select SelectCommand in the Properties window, expand, select Connection, select New in the Drop-down box, select Computer Database page in the pop-up window, Select the Odbcdatasource you want. Select command Text and click the "ellipsis button" next to enter the SQL statement in the pop-up window. If there are conditional arguments in the SQL statement, use the question mark. instead For example, select User,name from table where user =?. and you need to select parameters, click the "ellipsis button" next to the pop-up window select "Add", added a parameter. Modify the properties of the parameter in the Next property box. OdbcType is the type of the corresponding field for the parameter, SourceColumn is the field name for the parameter. ParameterName is the name of the parameter, here is PARAM1.

Enter the code page below to manually modify the code.
When the OdbcDataAdapter is configured, a new connection is made, and a connection control is generated, the name is OdbcConnection, and the database is connected to open it first. Use the statement Me.OdbcConnection1.Open (), use the end to turn off, Me.OdbcConnection1.Close ().

Data query out, you can put to a place equivalent to the recordset, here with the statement
Dim ODR1 as System.Data.Odbc.OdbcDataReader defines variable ODR1 to store.

Just said, the query has parameters, now to get the actual value from the interface to the parameters, with the statement
Me.OdbcSelectCommand2.Parameters ("PARAM1"). Value = Me.TextBox1.Text
Note that this is Odbcselectcommand, not odbcdataadapter2.selectcommand.
Then execute the following statement to get the recordset
ODR1 = Me.OdbcDataAdapter2.SelectCommand.ExecuteReader ()
To determine if the recordset is empty, and not empty, to read data in the Recordset through ODR1 ("Field name")
If ODR1. HasRows Then
While Odr1. Read ()
Name = ODR1 ("name")
user = ODR1 ("user")
End While
Odr1. Close ()
End If




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.