Vb. NET and SQL database

Source: Internet
Author: User

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

Different languages, because of their grammatical characteristics. There are some minor differences in connection operations to SQL databases. But one thing, that's. the SQL statement for the SQL database operation statement is generally the same.

This period of time is vb.net Study, to tell the truth, at the beginning of the time, it will certainly feel more difficult to start. Prior to this, you learned C #. From C # to vb.net, the bends are small. But it's a little urgent. Now some vb.net SQL database in the increase, delete, change, check summary. Convenient for their future inquiries, but also for the new contact vb.net friends to provide a small reference.

For these statements. is a skilled process, many others are used rather than memory. The following see vb.net Implementation of the whole process and the corresponding example.

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<Field name1=name of the participant1> <Field name2=name of the participant2> <...>WHERE<conditions1> <conditions2> <conditions2>

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

Some of the corresponding instances

(1) Finding data

(used: 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 () ' Run the connection.         Open database ' Build query statement cmd_logon.commandtext = ' select * from T_logon WHERE CardID = ' "& User.cardid & '"                                   Dim reader as Sqlclient.sqldatareader ' build read data Object reader = Cmd_logon.executereader ' Object read 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

(used: Method of participation)

    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)"        ' number of parameters        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

(used: 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) ' Based on ' study number ', ' Status "condition Update Cmd_update_updown. Connection = Sqlconnection_updown Cmd_update_updown.  CommandText = "UPDATE T_updown" & _ "SET Downdatetime = @Downdatetime, downtime = @Downtime, consumetime = @Consumetime "& _", Consumecash = @ConsumeCash, Remaincash = @Remaincash "& _" WHERE CardID = ' "& User.cardid &" ' "Cmd_update_updown. CommandType = CommandType.Text Sqlconnection_updown. Open () ' Opening connection ' joins the number of 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. ExecuteNonQuery () Sqlconnection_updown. Close () Return ok_no_updown = True End Function

(4) Delete data

(used: 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 the vb.net, compared with I used to use VB a year ago, more use of the method of participation. Some data have been checked to inform that the method can reduce the generation of errors and enhance the security.

In the above code, it is used to transfer entities in the same way. In vb.net, with the entity Code more self-initiative, and C # in comparison with the entity, this also embodies a certain advantage.

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.