ACCESS to vb.net database operations (1)

Source: Internet
Author: User
I have always wanted to write down database operations, but I have no time (it seems to be an excuse, it should be a lazy or not in the mood, huh, huh ). Recently, I found that someone still needs to ask these questions every day on CSDN. Sometimes I am lazy to answer these questions.

I have always wanted to write down database operations, but I have no time (it seems to be an excuse, it should be a lazy or not in the mood, huh, huh ). Recently, I found that someone still needs to ask these questions every day on CSDN. Sometimes I am lazy to answer these questions.

Always wanted to write downDatabaseOperationIt's just that there is no time (it seems like an excuse, it should be lazy or not in the mood, huh, huh ). Recently, CSDN

I still have to ask these questions every day, and sometimes I will be too lazy to answer these questions. So I am listening to the experts. Come on.

Once and for all, you can review it and give some help to others.

ThisDatabaseOperationI will write a series. First, write ACCESS. I don't know how long this article can be written, but I have to grade it.

In fact, ACCESSOperationAlmost all othersDatabaseThe difference is long. Are you afraid or unfamiliar? It's actually a paper tiger.

Now let's talk about the connection:

Public Function DB_CnStr () As String Return "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" & Application. StartupPath & "./FlashCard. mdb" End Function

The above code is a public function, because it needs to be used in multiple places, so I am too lazy to write it every time. It is OK to directly call it. The middle part isDatabaseConnection string,

Data Source = "& Application. StartupPath &"./FlashCard. mdb"DatabaseAddress. Do not explain the specific code?

If you have a password, you need to add an account password.

User ID = myUsername; Password = myPassword

There is too much information, and it's almost the same. Just grab it.

So what should I do?Operation? Yes,OperationDatabaseWell, it's nothing more than query, add, delete, and modify. The code is almost the same, but the name should be changed on SQL. If you don't need to talk about it, you can directly add the code.

'Here is the "query" section. I will not discuss how to query the SQL statement first. 'connect to DBConnection first. connectionString = DB_CnStr () DBConnection. open () 'starts to query Dim SQL As New System. text. stringBuilder SQL. append ("SELECT * from wordinfo") SQL. append ("where class = '"). append (Classstring ). append ("'") Dim cmd As New OleDb. oleDbCommand (SQL. toString, DBConnection) Dim RS As OleDb. oleDbDataAdapter = New OleDb. oleDbDataAdapter (cmd) DT = New DataTable RS. fil L (DT) DBConnection. close () 'indicates Rowcount = DT. rows. count If Rowcount = 0 Then MsgBox ("No Record", BoxStyle. critical, "Error") Me. close () else 'write and display data or application data here. Let's look at the end if' and start to demonstrate the "add" function. Here I just demonstrate this function, for fear that some people may not understand it, so str = New System is modified in the source code. text. stringBuilder str. append ("insert into wordinfo ([PICTURE], [ENGLISHNAME], [CHINESENAME], [VOICE], [Class])") str. append ("VALUES (PICTURE, ENGLISHNAME, CHINESENAME, VOICE, Class) ") 'above is a general SQL statement. The following sentence is critical and is an execution statement. Cmd. ExecuteNonQuery () 'is "modified. DBConnection. connectionString = DB_CnStr () DBConnection. open () Dim str As New System. text. stringBuilder str. append ("update userinfo") str. append ("SET [PASSWORD] = '"). append (Password ). append ("'") str. append ("WHERE [USERNAME] = '"). append (Username ). append ("'") Debug. writeLine ("") Debug. writeLine (str. toString) Dim cmd As New OleDb. oleDbCommand (str. toString, DBConnection) cmd. executeNonQuery () MsgBox ("Update OK", MsgBoxStyle. information) 'Okay, the last "delete" Dim str As New System. text. stringBuilder str. append ("delete from userinfo") str. append ("WHERE") str. append ("[USERNAME] = '"). append (username ). append ("'") Debug. writeLine ("") Debug. writeLine (str. toString) cmd = New OleDb. oleDbCommand (str. toString, DBConnection) cmd. executeNonQuery () MsgBox ("deleted", MsgBoxStyle. information)
I don't know if you can see it clearly. Here is an ACCESSOperation. I think you understand this,DatabaseOperationIt's time to understand more than half.

I want to take a look at the official website. Have you noticed the difference between query and Addition and insertion? Note: The column name is not enclosed in brackets in the query.

In other places, why? I don't know either. Haha! But you must add it. If you do not add ACCESS, you do not know it and cannot succeed. Remember !!

There are some other problems, such as how to read, display, or play images, sounds, and other files. This is another technical problem.

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.