VB. NET and SQL databases, and VB. NETsql Databases

Source: Internet
Author: User
Tags how to use sql

VB. NET and SQL databases, and VB. NETsql Databases

Data contains rich information. Data is a resource.

The connection operations to SQL databases are slightly different for different languages due to their respective syntax features. However, the SQL statements for SQL database operations are the same.

VB. NET is being studied during this period of time. To be honest, at the very beginning, it was really difficult to start. Before that, I learned C #, from C # to VB. NET, but it was a little anxious. Add, delete, modify, and summarize SQL databases in VB. NET to facilitate future queries and provide a small reference for friends who are new to VB. NET. These statements are a skillful process, but they are more useful than memory. Next, let's take a look at the whole process of adding, deleting, modifying, and querying in VB. NET and the corresponding instances.

Search data: Record 1, record 2, record 3... returned by SELECT ...... [Or *] FROM table name WHERE field name = '"variable name "'

Insert data: Insert into Table Name (field name 1, field name 2, field name 3 ......) Values (@ field name 1, @ field name 2, @ field name 3 ......)

Update Data: UPDATA table name SET <condition 1> <AND condition 2> <AND condition 2> WHERE <field name 1 = parameter name 1> <AND field name 2 = parameter name 2> <AND ......>

Delete data: Delete from <Table Name> WHERE <condition>

Corresponding instance

(1) Search for Data

(Using the concatenation string method)

Function selectLogon (ByVal User As Entity. logonEntity) As Entity. logonEntity Dim cmd_Logon As New SqlCommand instantiate a command object Dim sqlconnection_Logon = New SqlConnection (DbUtil. sqlConcectStr) 'instantiate a database connection object with a condition, and execute the connection by creating the connection command sqlconnection_Logon.Open, open the 'create query statement pai_logon.commandtext = "SELECT * FROM T_Logon WHERE CardID = '" & User. cardID & "'" Dim reader As SqlClient. sqlDataReader 'create a read data object reader = pai_logon.executereader 'to read Dim users As New Entity one by one. logonEntity While (reader. read () 'cyclically Read data If users Is Nothing Then users = New Entity. logonEntity End If users. ID = reader. getValue (reader. getOrdinal ("UserID") 'reads data and assigns it to the new entity users. cardID = reader. getValue (reader. getOrdinal ("CardID") users. userName = reader. getValue (reader. getOrdinal ("UserName") users. sex = reader. getValue (reader. getOrdinal ("Sex") users. department = reader. getValue (reader. getOrdinal ("Department") users. grade = reader. getValue (reader. getOrdinal ("Grade") users. inCash = reader. getValue (reader. getOrdinal ("InCash") End While sqlconnection_Logon.Close () Return users End Function

(2) Insert data

(Using the parameter method)

Function InsertUp_Doing (ByVal User As Entity. mainEntity) As Boolean Dim cmd_insert_updoing As New SqlCommand Dim sqlconnection_updoing As New SqlConnection (DbUtil. sqlConcectStr) parameters = sqlconnection_updoing 'insert statement Values = "insert into T_Updoing (CardID) Values (@ CardID)" 'Parameter Method Evaluate (New SqlParameter ("@ CardID", User. cardID) sqlconnection_updoing.Open () cmd_insert_updoing.ExecuteNonQuery () sqlconnection_updoing.Close () Return True End Function

(3) update data

(Using the concatenation string method)

Function update_updown (ByVal User As Entity. mainEntity) As Boolean Dim OK _NO_updown As Boolean Dim pai_update_updown As New SqlCommand Dim sqlconnection_updown As New SqlConnection (DbUtil. sqlConcectStr) 'Update __update_updown.connection = sqlconnection_updown according to the student id and Status conditions. Complete _update_updown.commandtext = "UPDATE T_UpDown SET UserID = @ UserID AND Status = @ Status WHERE" & _ "Downdatetime = @ Downdatetime AND Downtime = @ Downtime AND Consumetime = @ Consumetime AND "& _" ConsumeCash = @ ConsumeCash AND Remaincash = @ Remaincash "__update_updown.commandtype = CommandType. text sqlconnection_updown.Open () 'open connection' add parameter pai_update_updown.parameters.add (New SqlParameter ("@ Downdatetime", User. downdatetime) pai_update_updown.parameters.add (New SqlParameter ("@ Downtime", User. downtime) pai_update_updown.parameters.add (New SqlParameter ("@ Consumetime", User. timeconsume) __update_updown.parameters.add (New SqlParameter ("@ ConsumeCash", User. cashconsume) pai_update_updown.parameters.add (New SqlParameter ("@ Remaincash", User. cashremain) pai_update_updown.parameters.add (New SqlParameter ("@ Status", User. status) sqlconnection_updown.Close () Return OK _NO_updown = True End Function

(4) Delete data

(Using the concatenation string method [CardID = '"& User. CardID &"'])

    Function deleteUp_doing(ByVal User As Entity.MainEntity) As Boolean        Dim OK_NO_down As Boolean        Dim cmd_delete_updoing As New SqlCommand        Dim sqlconnection_delectupdoing As New SqlConnection(DbUtil.sqlConcectStr)        sqlconnection_delectupdoing.Open()        cmd_delete_updoing.Connection = sqlconnection_delectupdoing        cmd_delete_updoing.CommandText = "DELETE FROM T_Updoing WHERE CardID ='" & User.CardID & "'"        cmd_delete_updoing.ExecuteNonQuery()        sqlconnection_delectupdoing.Close()        Return OK_NO_down = True    End Function

In VB. NET, the parameter method is more used than I used VB a year ago. I checked some information and told you that using the parameter method can reduce errors and enhance security.

In the above Code, passing parameters uses the object transfer method. In VB. NET, object code is more automatic than C # entity, which also reflects some superiority.


How to use SQL statements in vbnet to query data in the database

Just answer at 50. My answer is correct.
If the form name is Form1
Add two controls, one is the twisted Button1 and the other is the table DataGridView1.
Then you can simply use my code.
You 'd better copy my code to your code window. This is too messy.
Also, you have not provided the database name, whether to log on to Windows or SQL. You need to change it in the code. I have commented out all the information you have.
I use WIndows VISTA, SQL 2005, and VB 2008.

Imports System. Data. SqlClient
Public Class Form1

Dim LeafSqlConnection As SqlConnection 'declares these SQL classes
Dim LeafSqlCommand As SqlCommand
Dim LeafSqlDataAdapter As SqlDataAdapter
Dim LeafData As able 'is a table class used to hold the table with the information you read.

Private Sub button#click (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles Button1.Click
LeafSqlConnection = New SqlConnection ("Data Source = .; initial Catalog = Your Database Name; Integrated Security = True; Pooling = False ") 'If you log on as a windows user, use this, I wrote the database name 'your database name' directly without the user name and password.
'Leafsqlconnection = New SqlConnection ("Initial Catalog = Your Database Name; User ID = sa; PWD = leafsoftpassword") 'if you use the SQL User password to log on, note that the names of these databases are directly written as 'your database name'. If you want to read other databases and change the user password
LeafSqlCommand = New SqlCommand ("Select * from table 1 Where name = 'xiaoqiang';", LeafSqlConnection) 'Remember to add symbols on both sides of the name'
LeafSqlDataAdapter = New SqlDataAdapter
LeafSqlCommand. CommandType = CommandType. Text
LeafSqlDataAdapter. SelectCommand = LeafSqlCommand
LeafData = New DataTable
LeafSqlDataAdapter. Fill (LeafData)
DataGridView1.DataSource = LeafData
End Sub
End Class... remaining full text>

Connection between vbnet and databases (pl/SQL)

Imports System. Data
Imports System. Data. SqlClient

Public Class Form1

Private Sub Form1_Load (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles MyBase. Load
Dim DBSet As DataSet
Dim ErrMsg As String
Dim cnn As SqlClient. SqlConnection
Dim cmd As New SqlClient. SqlCommand ()
Dim adpt As SqlClient. SqlDataAdapter
Dim rst As New DataSet ()
Dim sSQL As String

ErrMsg = ""

Try
Cnn = New SqlClient. SqlConnection ("data source = (local); initial catalog = urp; user id = sa; pwd = 1234 ")

SSQL = "select * from employee"

Adpt = New SqlClient. SqlDataAdapter (sSQL, cnn)
Adpt. Fill (rst)
DBSet = rst
DataGridView1.DataSource = DBSet
Catch ex As Exception
ErrMsg = ex. Message
Finally
Rst = Nothing
Cnn = Nothing
End Try

End Sub

End Class

Related Article

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.