Vb. NET and SQL database

Source: Internet
Author: User

Data is rich in information, and data is a resource.

Different languages, due to their own grammatical characteristics, There are some small differences in the connection operation of SQL database. One thing, though , is that the SQL statements are generally the same for SQL Database operations statements .

        This period is being vb. NET vb.net sql database of the increase, deletion, change, check summary, convenient for their future inquiries, but also for the first contact vb.net vb.net Realize the whole process and corresponding example of adding and deleting and changing.

Search Data : Records returned by SELECT 1, record 2, record 3 ... [or *] from Table name WHERE Field name =' "Variable name" '

Inserting Data:INSERT intoTable name(Field name1, field name2, field name3... )Values (@Field name1,@Field name2,@Field name3...) 

Update Data:UpdataTable nameSET <conditions1> <andconditions2> <andconditions2> WHERE <Field name1=Name of parameter1> <andField name2=Name of parameter2> <and...>

Delete data : Delete from< table name > WHERE < condition >

correspond to some instances

(1) Finding data

(using: Splicing 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 strip A database Connection object cmd_logon.connection = Sqlconnection_logon ' Create connection command Sqlconnection_logon.op En () ' Perform a connection, open the database ' Set query statement cmd_logon.commandtext = ' SELECT * from T_lo                             Gon WHERE CardID = ' "& User.cardid &" ' "Dim reader as Sqlclient.sqldatareader ' Set read data object reader = Cmd_logon.executereader ' object read-only Dim users as New Entity. Logonentity while (reader. Read ()) ' Cyclic reading of data If users are nothing then users = New Entity.logonentity End If users.id = reader. GetValue (reader. GetOrdinal ("UserID")) ' ReadData, assigned 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) Inserting data

(using: Parametric 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)        cmd_insert_updoing. Connection = sqlconnection_updoing        ' INSERT statement        cmd_insert_updoing. CommandText = "INSERT into t_updoing (CardID) Values (@CardID)"        ' parameter method        cmd_insert_updoing. Parameters.Add (New SqlParameter ("@CardID", User.cardid))        sqlconnection_updoing. Open ()        cmd_insert_updoing. ExecuteNonQuery ()        sqlconnection_updoing. Close ()        Return True    End Function

(3) Update data

(using: Splicing string method)

    Function Update_updown (ByVal User as Entity.mainentity) As Boolean Dim Ok_no_updown As Boolean Dim Cmd_u Pdate_updown as new SqlCommand Dim Sqlconnection_updown as New SqlConnection (Dbutil.sqlconcectstr) ' According to ' study number ', ' Status "condition Update Cmd_update_updown. Connection = Sqlconnection_updown Cmd_update_updown. CommandText = "UPDATE t_updown SET UserID = @UserID and Status = @Status WHERE" & _ "Downdatetime = @Downd Atetime and downtime = @Downtime and Consumetime = @Consumetime and "& _" Consumecash = @ConsumeCash and R Emaincash = @Remaincash "Cmd_update_updown. CommandType = CommandType.Text Sqlconnection_updown. Open () ' Connect ' to add parameter cmd_update_updown. Parameters.Add (New SqlParameter ("@Downdatetime", User.downdatetime)) Cmd_update_updown. Parameters.Add (New SqlParameter ("@Downtime", User.downtime)) Cmd_update_updown. Parameters.Add (New SqlParameter ("@Consumetime",User.timeconsume)) Cmd_update_updown. Parameters.Add (New SqlParameter ("@ConsumeCash", User.cashconsume)) Cmd_update_updown. Parameters.Add (New SqlParameter ("@Remaincash", User.cashremain)) Cmd_update_updown. Parameters.Add (New SqlParameter ("@Status", User.status)) Sqlconnection_updown. Close () Return ok_no_updown = True End Function

(4) Delete data

(using: Stitching 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, compared with the use of VB before I used a year ago, more using the parameter method. Some data have been checked to tell that the parameter method can reduce the generation of errors and enhance the safety.

In the above code, the arguments are in the way of passing entities. In vb.net, it is more automatic with the entity code, which also shows some superiority compared with the entity in C #.

Vb. NET and SQL database

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.