用VB怎样连接数据库SQL SERVER

请说洋细... 请说洋细 展开
 我来答
flybird11
2006-05-17 · TA获得超过385个赞
知道小有建树答主
回答量:287
采纳率:0%
帮助的人:233万
展开全部
工程->引用->Microsoft ActiveX Data Objects 2.0 Library (后面为版本号)
Dim rs As New ADODB.Recordset, cn As New ADODB.Connection
'向数据库中添加数据
Private Sub Command2_Click()
If rs.State = adStateOpen Then rs.Close
rs.Open "SELECT * FROM USERPASSWORD", cn, adOpenDynamic, adLockOptimistic
rs.AddNew
rs!UID = txtUid.Text
rs!PWD = txtPwd.Text
rs!TRUENAME = txtName.Text
rs!CREATEDATE = Format(Now, "YYYY/MM/DD")
rs.Update

Command1.Value = True
End Sub

Private Sub Form_Load()
'连接 SQL SERVER 数据库
cn.ConnectionString = "Driver={SQL Server};SERVER=DataServer;DATABASE=zxzx;UID=information;PWD=information*&#"
cn.Open
'查询字符串可以上这里查
'http://www.connectionstrings.com/
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
On Error Resume Next
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
--------------------------------------------------------------------
Private Sub Form_Load()
Dim strConn As String
Dim pubConn As New ADODB.Connection
Dim rsTable As New ADODB.Recordset
Dim strSQL As String

strConn = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=develop; password=12345;Data Source=ServerName"
pubConn.Open strConn

rsTable.CursorLocation = adUseClient
strSQL = "select * from TableName"
rsTable.Open strSQL, pubConn, adOpenDynamic, adLockOptimistic
Set DataGrid1.DataSource = rsTable

End Sub
--------------------------------------------------------------------

dim cnn as new ADODB.Connection
dim rs as Adodb.recordset
dim s as string,i as long

cnn.CursorLocation = 3 '
cnn.CommandTimeout = 30
cnn.Open "Provider=SQLOLEDB;Data Source=192.168.0.11;User ID=sa;Password=sa;Initial Catalog=tempdb;Persist Security Info=True"

set rs = nothing
set rs = cnn.execute("select * from sysobjects")
while not rs.eof
for i = 0 to rs.fields.count - 1
s = s & rs(i) & vbtab
next
s = s & vbcrlf
rs.movenext
wend
msgbox s
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式