Data | database | data source | execution
General database system programming tutorials are to create a database in the beginning of programming, and in the system to generate user data source, system data source and file data base, in the integrated development environment, through the data source to connect to the database. So, how to program without a data source. Build the database that the application will manipulate and the tables? (for example, when an application is installed, the database is generated the first time it is started).
Method: Specify the legitimate user name and password of the DBMS in the connection string of the application's ADO object, and specify the database driver and to connect to the target host.
For example, SQL SERVER connected to this computer assumes that the user name is null with the SA password.
The connection string is: Conn_str = "uid=sa;pwd=;d river={sql server};server= (local)"
The connected database can then be manipulated accordingly.
Here is a simulation of my medical management system of the original code (VB):
Public Sub generate_db ()
' Determine if there is an application's database file in the application's directory
Dim Db_file as String
Db_file = Dir (App.Path + "\test.mdf")
If db_file = "" Then
' Define the connection string
Dim Conn_str as String
Conn_str = "uid=sa;pwd=;d atabase=master;driver={sql server};server= (local)"
' Declare the object to use
Dim Conn as New ADODB. Connection
Dim cmd as New adodb.command
Dim rs as New ADODB. Recordset
' Connect the data source
Conn. Open Conn_str
' Create a query command
Cmd. CommandText = "CREATE DATABASE test on primary (Name=test,filename= '" & App.Path + "\test.mdf" & "", Size=5mb,maxsiz e=10mb,filegrowth=20%) Log on (Name=test_log,filename= ' "& App.Path +" \test_log.ldf "&" ", SIZE=2MB,MAXSIZE=4MB , filegrowth=20%) "
' Execute command (used to build the database)
Conn. Execute Cmd.commandtext, Default,-1
' Tables to be used to build the database
Cmd.commandtext = "CREATE TABLE test." [dbo]. [Db_user] ([username] [varchar] COLLATE chinese_prc_ci_as NOT NULL, [password] [varchar] COLLATE chinese_prc_ci_as NOT NULL, Permissions [varchar] (COLLATE chinese_prc_ci_as NOT NULL, [registration date] [varchar] (a) COLLATE chinese_prc_ci_as NULL) "
Conn. Execute Cmd.commandtext, Default,-1
' Give the database a root user
Cmd.commandtext = "INSERT into Test." [Dbo].db_user VALUES (' root ', ' root ', ' admin ', ' original user ')
Conn. Execute Cmd.commandtext, Default,-1
Else
MsgBox "The database file currently in use is in good condition!" ", vbOKOnly + vbinformation," hint "
End If
End Sub
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.