Many .Net beginners asked me what is good for books, I have seen N.NET book, I found a lot of truth ... So I wanted to write a copy.
My purpose is to "use the big white words to talk about the complex technology", be sure to make most people can understand and can be used in actual.
This book I conceive a large system, it is difficult to write, and the other is limited, I really don't have much confidence to reach my original intention, so I will choose a section from the book to blog, I want to hear everyone's opinion .
Oh, I hope that my fragile confidence will not be smashed by flying bricks.
Bitfan 2005.1.24
-------------------------------------------------- ---
5.2 Developing Database Applications Using .NET
5.2.1
User interface design
(1) Creating a new Windows form program --- MyFirstDbProgram:
Figure 5-7 New Project
(2) Design Form:
Drag a DataGrid, three button, a statusbar to the form.
Control name control Type Description DataGrid1 DataGrid is used to display data BTnConnectDB Button Connection Database BTNGetData
Button extraction data BTNShowDataInGrid
Button fill grid statusbar1
STATUSBAR shows various information
The user interface is designed is as follows:
Figure 5-8 Design Form
5.2.2
Start coding
First, you need to introduce the namespace:
Imports system.data.oledb
In this case, we use the classes in the OLEDB name space to access the database.
1 connected database
The first step of developing all database applications is connected to the database, which is implemented by the OLEDBConnection object.
Connected to the database in the program mainly has the following steps (complete code, please see the CD)
(1) Define the variable:
Private conn as oledbconnection = Nothing
(2) Create an OLEDBCONNECTION object:
CONN = New OLEDBCONNECTION
(3) Set the connection string, this connection string can query the MSDN acquisition, and the seventh chapter of this book also introduces.
Conn.connectionstring = "provider = microsoft.jet.OLEDb.4.0; data source = clients.mdb; persist security info = false"
(4) Connect the database
Conn.open ()
You can design a complete SUB process to implement the function of connecting the database:
'Connect to the database
Private sub buttonnectdb ()
IF conn is nothing then
'Creating a connection object
CONN = New OLEDBCONNECTION
END IF
'Setting the connection string
Conn.connectionstring = "provider = microsoft.jet.OLEDb.4.0; data source = clients.mdb; persist security info = false"
Try
'Connect to the database
Conn.open ()
'Display connection information
Me.statusbar1.panels (0) .text = "Database success connection"
'Enable button
Me.btnetdata.enabled = truecatch execption
'Show error information
Me.statusbar1.Panels (0) .text = ex.Message
END TRY
End Sub
Instance Run Results:
Figure 5-9 Connecting the database success
Since the database connection is a complex program and database management system interaction, there are many reasons that can cause database connections, so be sure to use TRY ... CATCH report errors. -------------------------------------------------- ---------- Please see the second part: http://blog.9cbs.net/bitfan/archive/2005/01/24/266164.aspx