简体   繁体   中英

VB.NET Local Database Connection/Data Extraction

I've been looking over a bunch of code snippets and I'm just not understanding what is going on there. Some help in understanding the connection, reading/manipulation, and closing process would be more than appreciated.

I keep reading all about these DataAdapters, Ole and Sql and Sqlce and I don't know which to use or whatever.

I made a database in VB.NET 2010 that came out as database.sdf and I selected the Local Database option (I assume this is relevant?). I then populated the database with a row of data with column titles and a primary key. The table contains many different columns, but for test purposes, the following is relevant:

  • DB Name: db_test
  • Table Name: Locations
  • Primary Key Column Header: CITY
  • Another column: BOUND_X
  • Info in those two cells: CITY - 'Brunswick' and BOUND_X - '5'
  • There is only one row of data, so this is either Row(0) or Row(1), because I don't know what .NET uses for its start index.

This is what I pieced together, but I'm quite sure it's just not even in the right direction...

Obviously code help would be phenomenal, but if anyone is willing to break this process down in English as well, that'd be super fantastic.

Dim conn As SqlConnection = New SqlConnection("Data Source=G:\Programming\VB.NET\TEST\TEST\db_test.sdf")

Dim ds As New db_test
Dim sql As New SqlCommand("SELECT * FROM Locations WHERE CITY='Brunswick'")

conn.Open()

(I have no idea what this is actually referring to, I am just trying to read data with these commands below that seemed to pop up a lot in forums...)
Dim sdr As SqlDataReader = sql.ExecuteReader
While sdr.Read = True
    MsgBox(sdr.Item("CITY") & " " & sdr.Item("BOUND_X"))
End While

sdr.Close()

conn.Close()

The data access code in .NET is grouped into what is ADO.NET. You can read for reference here:

http://msdn.microsoft.com/en-us/library/h43ks021(v=vs.100).aspx

Regarding the code:

  1. The SqlConnection is initialized. Connection is means of accessing data at the data source. Since your source is single-file database (SQL CE maybe), it's initialized with the path to file. SqlConnection is used by all other classes to get access to the data source.
  2. Command is initialized to get necessary data. It may be a call to the stored procedure or an SQL query. It show the way to obtain data - what data to get and how.
  3. Connection to the datasource is opened. Channel to transfer data is opened, necessary manipulations made (port opening, handshake, file opening, etc.)
  4. SqlDataReader is one of the mechanisms to read data based on the command and connection. Command takes connection (should set sql.Connection = conn) and may be executed in three different ways: ExecuteReader() - to return dataReader and fetch rows as a set; ExecuteScalar() - to execute statistical sql queries like count/avg/... that return single-row-single-column result (like '5') - returns int; ExecuteNonQuery() - to execute queries (like create or delete table, etc.) that don't return any result.
  5. So, dataReader is like StreamReader for files, just returning instead of lines read - rows fetched. It points to the current row of the whole set. Default course of action is to iterate over the dataReader using Read() and saving every dataRow into a List<> or something.
  6. Connection is closed to free resources used.

More technically correct code would look like this may be found here:

http://msdn.microsoft.com/en-us/library/dw70f090.aspx#Y533

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM