connection
Private Sub Command1_Click()On Error GoTo dealerror
Dim objCon As connection
Set objCon = New connection
strcn = "provider=SQLOLEDB.1;User ID =sa Data source=(local)"
objCon.connectionstring = strcn
objCon.open
strsql = "CREATE DATABASE 实例2"
objCon.execute strsql
MsgBox "成功创建数据库"
objCon.Close
strcn = "provider=SQLOLEDB.1;User ID =sa Data source=(local);initial ctalog=实例2"
objCon.connectionstring = strcn
objCon.open
strsql = "create table 测试表(用户名 varchar(13) not null primary key,口令 char(6) not null )"
objCon.execute strsql
MsgBox "成功创建数据库"
strsql = "insert 测试表(用户名,口令) values('admin ','123456')"
objCon.Close
Set objCon = Nothing
Command2.Enabled = True
Exit Sub
dealerror:
MsgBox "数据库创建过程出错" & Err.Description
end sub
各位大哥,小弟我初学数据库,其中的 connection 不知道为什么出错了.创建不了数据库. --------------------编程问答-------------------- Dim objCon As connection
Set objCon = New connection
你用ado怎么没有ado的声明,回来再看,只不过第一句就错了 --------------------编程问答-------------------- Dim objCon As ADODB.Connection
Set objCon = New ADODB.Connection --------------------编程问答-------------------- 呵呵,这些代码不是抄来的吧??书上抄的? --------------------编程问答-------------------- '如果是刚学习的,请把基础打好,别写个代码,都没有引用正确
'1、最基础的ado没有引用,没有声明
'2、代码建立数据库也好,建立表也好,都不要用中文,这是非常恶劣的习惯。
Public Conn As New ADODB.Connection
Public Rs As New ADODB.Recordset
Private Sub Command1_Click()
Dim strConn As String
Dim strSQL As String
strConn = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Data Source=localhost"
'打开连接
Conn.Open strConn
'错误处理
On Error GoTo ErrDB:
'开始创建数据库,加设这个数据库名为ITPub
Conn.Execute "create database [ITPub]"
'设置Connection默认数据库为这个
Conn.DefaultDatabase = "ITpub"
Me.Caption = "创建成功ITpub数据库!"
On Error GoTo Errtable:
'创建表PubUsers
strSQL = "Create table PubUsers(Userid char(4) not null primary key,UserName Varchar(13) not null)"
Conn.Execute strSQL
Me.Caption = "成功创建PubUsers表"
'插入一条数据
strSQL = "insert into PubUsers(UserID,UserName) values('S001','Susan')"
Conn.Execute strSQL
Me.Caption = "插入数据成功"
'关闭连接
Conn.Close
Set Conn = Nothing
Exit Sub
ErrDB:
Me.Caption = "创建ITpub数据库失败"
Err.Clear
Errtable:
Me.Caption = "创建表失败"
Err.Clear
Conn.Close
Set Conn = Nothing
End Sub
补充:VB , 数据库(包含打印,安装,报表)