兄弟们啊,给我一份vb连接sql数据库实现的增删改查程序呗。别的数据库的也可以 但是要vb的,谢啦 20
3个回答
展开全部
VB使用ADO数据库可以分为有源数据库和无源数据库,即是否使用了DSN数据源,如下例:
1、在连接数据库前首先要在VB菜单中“工程”-“引用”从中选择 microsoft activeX Data objects 2.6 library和 microsoft activeX Data objects recordset 2.6两个组件,这是在连接数据前所必做的工作。
2、接下来定义ADO的连接对象,如下所示:
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
第一种方法采用的是无源的数据库连接方法
conn.ConnectionString = "Driver={sql server};server=JSZX3-11;uid=sa;pwd=;database=wzc"
conn.ConnectionTimeout = 30
conn.Open
rs.Open "select 用户名,密码 from login where 用户名='" & Trim(Combo1.Text) & "' And 密码='" & Trim(Text1.Text) & "'", conn, adOpenStatic, adLockReadOnly, adCmdText
If rs.EOF = True And rs.BOF Then
m = MsgBox("请重新登录", vbExclamation)
Text1.Text = ""
Text1.SetFocus
Else
Form1.Hide
End If
rs.Close
这是第二种方法连接数据库的方法,这一种方法是采用有源的方法与数据库连接的方法,代码如下所示:
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
conn.ConnectionString = "DSN=login;uid=sa;pwd=;"
conn.ConnectionTimeout = 30
conn.Open
rs.Open "select 用户名 from login", conn, adOpenStatic, adLockReadOnly, adCmdText
Dim i As String
For t = 0 To Val(rs.RecordCount) - 1
i = Trim(rs.Fields("用户名").Value)
rs.MoveNext
Combo1.AddItem i
Next t
rs.Close
增删查改的语句按照自己的需要填进去~
1、在连接数据库前首先要在VB菜单中“工程”-“引用”从中选择 microsoft activeX Data objects 2.6 library和 microsoft activeX Data objects recordset 2.6两个组件,这是在连接数据前所必做的工作。
2、接下来定义ADO的连接对象,如下所示:
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
第一种方法采用的是无源的数据库连接方法
conn.ConnectionString = "Driver={sql server};server=JSZX3-11;uid=sa;pwd=;database=wzc"
conn.ConnectionTimeout = 30
conn.Open
rs.Open "select 用户名,密码 from login where 用户名='" & Trim(Combo1.Text) & "' And 密码='" & Trim(Text1.Text) & "'", conn, adOpenStatic, adLockReadOnly, adCmdText
If rs.EOF = True And rs.BOF Then
m = MsgBox("请重新登录", vbExclamation)
Text1.Text = ""
Text1.SetFocus
Else
Form1.Hide
End If
rs.Close
这是第二种方法连接数据库的方法,这一种方法是采用有源的方法与数据库连接的方法,代码如下所示:
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
conn.ConnectionString = "DSN=login;uid=sa;pwd=;"
conn.ConnectionTimeout = 30
conn.Open
rs.Open "select 用户名 from login", conn, adOpenStatic, adLockReadOnly, adCmdText
Dim i As String
For t = 0 To Val(rs.RecordCount) - 1
i = Trim(rs.Fields("用户名").Value)
rs.MoveNext
Combo1.AddItem i
Next t
rs.Close
增删查改的语句按照自己的需要填进去~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
oracle的
Public Const oracle_SID As String = "orcl"
Public Const oracle_User As String = "TEST1"
Public Const oracle_Passwd As String = "TEST1"
Public myRs As Object
Private Function initConnect()
Set ORA_SE = CreateObject("OracleInProcServer.XOraSession")
Set ORA_DB = ORA_SE.DbOpenDatabase(oracle_SID, oracle_User & "/" & oracle_Passwd, 0&)
End Function
Private Function finalConnect()
Set ORA_SE = Nothing
getTableList
Set ORA_DB = Nothing
End Function
Sub getTableList()
Const strTableListSheetName As String = "TableList"
On Error GoTo BookFound_Err
Call initConnect
Dim sqlBuff As String
sqlBuff = sqlBuff & " SELECT A.TABLE_NAME"
sqlBuff = sqlBuff & " ,RTRIM(C.COMMENTS) AS TABLE_COMMENTS"
sqlBuff = sqlBuff & " ,A.COLUMN_NAME"
sqlBuff = sqlBuff & " ,RTRIM(B.COMMENTS) AS COLUMN_COMMENTS"
sqlBuff = sqlBuff & " ,A.DATA_TYPE AS COLUMN_TYPE"
sqlBuff = sqlBuff & " ,(CASE WHEN A.DATA_TYPE = 'NUMBER' THEN " _
& "A.DATA_PRECISION ELSE A.DATA_LENGTH END) AS COLUMN_LENGTH"
sqlBuff = sqlBuff & " ,A.DATA_SCALE AS COLUMN_SCALE"
sqlBuff = sqlBuff & " ,DECODE(D.CONSTRAINT_NAME,NULL,'','P') AS ISKEY"
sqlBuff = sqlBuff & " ,DECODE(A.NULLABLE,'N','Y','') AS NOTNULL"
sqlBuff = sqlBuff & " ,A.COLUMN_ID"
sqlBuff = sqlBuff & " FROM USER_TAB_COLUMNS A "
sqlBuff = sqlBuff & " INNER JOIN USER_COL_COMMENTS B"
sqlBuff = sqlBuff & " ON A.TABLE_NAME = B.TABLE_NAME"
sqlBuff = sqlBuff & " INNER JOIN USER_TAB_COMMENTS C"
sqlBuff = sqlBuff & " ON A.TABLE_NAME = C.TABLE_NAME"
sqlBuff = sqlBuff & " AND A.COLUMN_NAME = B.COLUMN_NAME"
sqlBuff = sqlBuff & " LEFT JOIN USER_CONSTRAINTS E"
sqlBuff = sqlBuff & " ON B.TABLE_NAME = E.TABLE_NAME"
sqlBuff = sqlBuff & " AND E.CONSTRAINT_TYPE = 'P'"
sqlBuff = sqlBuff & " LEFT JOIN USER_CONS_COLUMNS D"
sqlBuff = sqlBuff & " ON D.TABLE_NAME = E.TABLE_NAME"
sqlBuff = sqlBuff & " AND B.COLUMN_NAME = D.COLUMN_NAME"
sqlBuff = sqlBuff & " AND D.CONSTRAINT_NAME = E.CONSTRAINT_NAME"
sqlBuff = sqlBuff & " ORDER BY "
sqlBuff = sqlBuff & " C.TABLE_TYPE " 'TABLE VIEW
sqlBuff = sqlBuff & " ,A.TABLE_NAME "
sqlBuff = sqlBuff & " ,A.COLUMN_ID "
Set myRs = ORA_DB.DbCreateDynaset(sqlBuff, 0)
Dim iLineOutput As Integer
iLineOutput = 1
While Not myRs.EOF
chekAddSheet (strTableListSheetName)
For iField = 0 To myRs.Fields.Count - 1
ThisWorkbook.Sheets(strTableListSheetName).Cells(iLineOutput, iField + 1).Value = myRs.Fields(iField).Value
Next
iLineOutput = iLineOutput + 1
myRs.MoveNext
Wend
myRs.Close
Call finalConnect
Exit Sub
BookFound_Err:
Call finalConnect
MsgBox Error
End Sub
Public Const oracle_SID As String = "orcl"
Public Const oracle_User As String = "TEST1"
Public Const oracle_Passwd As String = "TEST1"
Public myRs As Object
Private Function initConnect()
Set ORA_SE = CreateObject("OracleInProcServer.XOraSession")
Set ORA_DB = ORA_SE.DbOpenDatabase(oracle_SID, oracle_User & "/" & oracle_Passwd, 0&)
End Function
Private Function finalConnect()
Set ORA_SE = Nothing
getTableList
Set ORA_DB = Nothing
End Function
Sub getTableList()
Const strTableListSheetName As String = "TableList"
On Error GoTo BookFound_Err
Call initConnect
Dim sqlBuff As String
sqlBuff = sqlBuff & " SELECT A.TABLE_NAME"
sqlBuff = sqlBuff & " ,RTRIM(C.COMMENTS) AS TABLE_COMMENTS"
sqlBuff = sqlBuff & " ,A.COLUMN_NAME"
sqlBuff = sqlBuff & " ,RTRIM(B.COMMENTS) AS COLUMN_COMMENTS"
sqlBuff = sqlBuff & " ,A.DATA_TYPE AS COLUMN_TYPE"
sqlBuff = sqlBuff & " ,(CASE WHEN A.DATA_TYPE = 'NUMBER' THEN " _
& "A.DATA_PRECISION ELSE A.DATA_LENGTH END) AS COLUMN_LENGTH"
sqlBuff = sqlBuff & " ,A.DATA_SCALE AS COLUMN_SCALE"
sqlBuff = sqlBuff & " ,DECODE(D.CONSTRAINT_NAME,NULL,'','P') AS ISKEY"
sqlBuff = sqlBuff & " ,DECODE(A.NULLABLE,'N','Y','') AS NOTNULL"
sqlBuff = sqlBuff & " ,A.COLUMN_ID"
sqlBuff = sqlBuff & " FROM USER_TAB_COLUMNS A "
sqlBuff = sqlBuff & " INNER JOIN USER_COL_COMMENTS B"
sqlBuff = sqlBuff & " ON A.TABLE_NAME = B.TABLE_NAME"
sqlBuff = sqlBuff & " INNER JOIN USER_TAB_COMMENTS C"
sqlBuff = sqlBuff & " ON A.TABLE_NAME = C.TABLE_NAME"
sqlBuff = sqlBuff & " AND A.COLUMN_NAME = B.COLUMN_NAME"
sqlBuff = sqlBuff & " LEFT JOIN USER_CONSTRAINTS E"
sqlBuff = sqlBuff & " ON B.TABLE_NAME = E.TABLE_NAME"
sqlBuff = sqlBuff & " AND E.CONSTRAINT_TYPE = 'P'"
sqlBuff = sqlBuff & " LEFT JOIN USER_CONS_COLUMNS D"
sqlBuff = sqlBuff & " ON D.TABLE_NAME = E.TABLE_NAME"
sqlBuff = sqlBuff & " AND B.COLUMN_NAME = D.COLUMN_NAME"
sqlBuff = sqlBuff & " AND D.CONSTRAINT_NAME = E.CONSTRAINT_NAME"
sqlBuff = sqlBuff & " ORDER BY "
sqlBuff = sqlBuff & " C.TABLE_TYPE " 'TABLE VIEW
sqlBuff = sqlBuff & " ,A.TABLE_NAME "
sqlBuff = sqlBuff & " ,A.COLUMN_ID "
Set myRs = ORA_DB.DbCreateDynaset(sqlBuff, 0)
Dim iLineOutput As Integer
iLineOutput = 1
While Not myRs.EOF
chekAddSheet (strTableListSheetName)
For iField = 0 To myRs.Fields.Count - 1
ThisWorkbook.Sheets(strTableListSheetName).Cells(iLineOutput, iField + 1).Value = myRs.Fields(iField).Value
Next
iLineOutput = iLineOutput + 1
myRs.MoveNext
Wend
myRs.Close
Call finalConnect
Exit Sub
BookFound_Err:
Call finalConnect
MsgBox Error
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
vb6.0写的,810行,10000多个字,百度发不了
功能:
建立文件夹,创建数据库,显示、添加、删除、修改、查找数据,表格,排序,导出为电子表格。
支持模糊查询、任意字段组合查询
10分不够. 至少11分
QQ
功能:
建立文件夹,创建数据库,显示、添加、删除、修改、查找数据,表格,排序,导出为电子表格。
支持模糊查询、任意字段组合查询
10分不够. 至少11分
追问
你QQ多少 我加你。。。或者你发我邮箱里fir_3@126.com
追答
2472526123
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询