<< Chapter < Page Chapter >> Page >

Dim cn As ADODB.Connection

Set cn = New ADODB.Connection

cn.Provider = "MSDASQL"

cn.ConnectionString = "DSN=Baigiang"

cn.Open

Kết nối DSN cấp thấp:

Dim cn As ADODB.Connection

Set cn = New ADODB.Connection

cn.Provider = "MSDASQL"

cn.ConnectionString = "DRIVER={SQL Server};”&_

“DATABASE=Baigiang;UID=myuser;PWD=mypassword;"

cn.Open

Trong trường hợp này việc kết nối với cơ sở dữ liệu Server được thực hiện nhanh hơn vì chương trình không cần đọc thông tin về các DSN trên máy Client, tuy nhiên thông tin về nguồn cơ sở dữ liệu lại kết chặt với chương trình đã biên dịch.

Để kết nối với cơ sở dữ liệu Access, ta dùng trình cung cấp Jet với chuỗi kết nối là đường dẫn đến tập tin .mdb

Dim cn As ADODB.Connection

Set cn = New ADODB.Connection

cn.Provider = "MicroSoft.Jet.OLEDB.4.0"

cn.ConnectionString = "d:\data\baigiang.mdb"

cn.Open

Đối với cơ sở dữ liệu SQL Server, ta có thể dùng trình cung cấp SQLOLEDB.1, trong trường hợp này, chuỗi kết nối tương tự như trường hợp kết nối dùng trình cung cấp ODBC không có DSN, tuy nhiên ta không cần xác định giá trị của DRIVER:

Dim cn as ADODB.Connection

Set cn = New ADODB.Connection

cn.Provider = “SQLOLEDB.1”

cn.ConnectionString = “DATABASE=DBHH;”&_

“SERVER=www;UID=user;PWD=user”

cn.Open

Mở và đóng nối kết nguồn dữ liệu

Để phát các yêu cầu đến nguồn dữ liệu sử dụng ADO, ta cần mở kết nối đến nguồn dữ liệu đó bằng phương thức Open của đối tượng Connection. Cú pháp đầy đủ như sau:

connection.Open [connect], [userid], [password]

Tất cả các tham số của phương thức Open đều là tùy chọn, nếu như các thông số này đã được xác định thông qua các thuộc tính khác của đối tượng Connection thì ta không cần mô tả chúng ở đây.

Khi đã hoàn thành tất cả các thao tác liên quan đến nối kết này, ta cần phải đóng nối kết một cách tường minh thông qua phương thức Close của đối tượng Connection.

connection.Close

Đóng nối kết một cách tường minh sẽ đảm bảo rằng tất cả các tài nguyên liên quan đến nối kết này trên Server cũng như Client đều được giải phóng một cách hợp lý.

Xác định vị trí con trỏ

Con trỏ (Cursor): một tập các mẩu tin được trả về cho chương trình. Vị trí con trỏ được xác định nhờ thuộc tính CursorLocation (có ở cả đối tượng Recordset). Có 2 giá trị có thể chỉ định:

  • adUseClient: con trỏ phía Client.
  • adUseServer: con trỏ phía Server (mặc định).

Thực thi các câu truy vấn hành động

Các câu truy vấn hành động (Insert, Update, Delete) được thực hiện nhờ phương thức Execute của đối tượng Connection; ngoài ra phương thức này cũng có thể được sử dụng để thực thi các thủ tục lưu trữ sẵn trong cơ sở dữ liệu hay các câu SELECT. Cú pháp phương thức này như sau:

Nếu không có kết quả trả về:

connection.Execute CommandText, RecordsAffected, Options

Có kết quả trả về:

Set recordset = connection.Execute (CommandText, RecordsAffected, Options)

Trong đó:

- connection: Đối tượng Connection.

- recordset: Đối tượng Recordset là kết quả trả về của phương thức Execute, tuy nhiên, người ta thường ít khi sử dụng cách này. Thay vào đó, người ta thường sử dụng phương thức Open của đối tượng Recordset.

- CommandText: là một chuỗi xác định câu truy vấn hành động, SELECT, thủ tục lưu trữ sẵn hay tên một bảng trong cơ sở dữ liệu.

Questions & Answers

the diagram of the digestive system
Assiatu Reply
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
what is cell divisoin?
Aron Reply
Diversity of living thing
ISCONT
what is cell division
Aron Reply
Cell division is the process by which a single cell divides into two or more daughter cells. It is a fundamental process in all living organisms and is essential for growth, development, and reproduction. Cell division can occur through either mitosis or meiosis.
AI-Robot
What is life?
Allison Reply
life is defined as any system capable of performing functions such as eating, metabolizing,excreting,breathing,moving,Growing,reproducing,and responding to external stimuli.
Mohamed
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Giáo trình visual basic. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10777/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Giáo trình visual basic' conversation and receive update notifications?

Ask