vb远程连接sql
不知道你为什么要用ADOC来配置...直接用VB代码就OK了。
公共模块:
Private Function Selectsql(SQL As String) As ADODB.Recordset '返回ADODB.Recordset对象
Dim ConnStr As String
Dim Conn As ADODB.Connection
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Set Conn = New ADODB.Connection
'On Error GoTo MyErr:
ConnStr = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=登录数据库用户名(默认为sa);Password=登录数据库密码;Initial Catalog=数据库名;Data Source=服务器名(IP地址)" '这是连接SQL数据库的语句
Conn.Open ConnStr
rs.CursorLocation = adUseClient
rs.Open Trim$(SQL), Conn, adOpenDynamic, adLockOptimistic
End Function
调用:
SQL = "SELECT * FROM 用户表 WHERE 用户名='" & Combo1.Text & "' AND 密码='" & Text1.Text & "' "
Set rs = Selectsql(SQL)
⑵ vb远程连接sql2000数据库
1、你那个外网地址盯数渣14.118.27.4我凯悄能PING通.
2、你SQL数据库放在外网空间里了吗?
3、外网空间是花生壳自己虚拟的吗?这个不知道有没有与数据库相关的设置,毕指反正在SP处租用空间的时候是有数据库功能和非数据库功能区别的。
⑶ VB怎么连接 SQL 数据库
一个简单的方法:
首先,建立程序公共模块输入以下代码:
Public conn As New ADODB.Connection
'全局变量 rst 指针
Public rst As New ADODB.Recordset
'全局调用函数 打开数据库
Public Function openconn()
conn.ConnectionString = "连接信息"
'此连接信息最简单的生成办法:
'在任意窗口下建立一新ADODC控件
'选择控件属性,通用页,使用连接字符串,生成(U)
'利用该向导生成出字符串,然后将该字符串复制到"连接信息"中即可
conn.Open
End Function
'全局调用函数 关闭数据库
Public Function closeconn()
If conn.State = 1 Then
conn.Close
Set conn = Nothing
End If
End Function
然后在窗体文件中可以使用的函数:
打开数据库:openconn
注意:此函数为自定义函数,无参数,定义在公共模块中!
建立数据表连接:Set rst = conn.Execute("select * from 数据表名")
如做登陆页可以这样判断用户名密码:
Text1.Text = RTrim(rst.Fields("id")) And Text2.Text = RTrim(rst.Fields("password"))
注:其中Text1.Text为输入用户名TextBOX Text2.Text为输入密码TextBOX
数据表中 id 为保存用户名 password 为保存密码!此方法为非加密方法!
关闭数据库函数:
closeconn
Set rst = Nothing
注:此两行,必须在打开数据库的情况下使用!并且每打开一次数据库后必须先使用此两句将数据库关闭后才可打开另一数据库表文件!!!!!否则程序报错终止!
添加新数据函数
rst.AddNew
rst("字段名1") = Text1.Text
rst("字段名2") = Text2.Text
rst.Update
删除数据函数
rst.Delete
注:使用此函数前,必须保证数据库指针函数rst指向需删除数据,移动rst指针可以使用函数:rst.MoveNext或rst.MoveLast
更新数据函数
rst.Open "update 数据表名 set 字段1='" + Text1.Text + "',字段2='" + Text2.Text + "' where id='" + Text3.Text + "'", conn, 1, 3
注,其中 where id = 为更新特定数据表的查找!
如果熟悉SQL语句,还可编写出其他方式的查找,这里就不多做介绍了!
⑷ Vb如何连接远程的sql server最好有实例代码,谢谢
Private Sub Command1_Click()
' Dim excel_app As excel.Application
' Dim excel_sheet As excel.Sheets
Dim excel_app As Object
Dim excel_sheet As Object
Dim rs As ADODB.Recordset
Dim strsql As String
Dim pubconn As ADODB.Connection
Dim exfieldA As String
Dim exfieldB As String
Dim exfieldC As String
Dim exfieldD As String
'打开数据库
Set rs = New ADODB.Recordset
Set pubconn = New ADODB.Connection
pubconn = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=aaa;Data Source=(local)"
pubconn.Open
Set excel_app = CreateObject("excel.application") 'excel对象
Set excel_app = New Excel.Application
' excel_app.Visible = True
excel_app.Workbooks.Open FileName:="D:\astroboy\河南生税务支持系统\实现\字典\111.xls"
' excel_app.Worksheets(Combo1.ListIndex + 1).Activate
If Val(excel_app.Application.Version) >= 8 Then '检查excel文件的版本
Set excel_sheet = excel_app.ActiveSheet
Else
Set excel_sheet = excel_app
End If
'''创建sql表格
Dim crtstrsql As String
Dim exceltst As String
exceltst = Text1.Text
exfieldA = Trim$(excel_sheet.Cells(1, 1))
exfieldB = Trim$(excel_sheet.Cells(1, 2))
exfieldC = Trim$(excel_sheet.Cells(1, 3))
exfieldD = Trim$(excel_sheet.Cells(1, 4))
crtstrsql = ""
crtstrsql = crtstrsql & "create table " & exceltst & "(" & vbCrLf
crtstrsql = crtstrsql & exfieldA & " char(50) null," & vbCrLf
crtstrsql = crtstrsql & exfieldB & " char(6) null," & vbCrLf
crtstrsql = crtstrsql & exfieldC & " datetime null," & vbCrLf
crtstrsql = crtstrsql & exfieldD & " datetime null)"
pubconn.Execute crtstrsql
Dim new_value1 As String
Dim new_value2 As String
Dim new_value3 As String
Row = 2
Do
new_value = Trim$(excel_sheet.Cells(Row, 1)) '读取excel工作者第一列数据
new_value1 = Trim$(excel_sheet.Cells(Row, 2))
If excel_sheet.Cells(Row, 3) = "" Then
new_value2 = ""
Else
new_value2 = CDate(excel_sheet.Cells(Row, 3) & "1月")
End If
If excel_sheet.Cells(Row, 4) = "" Then
new_value3 = ""
Else
new_value3 = CDate(excel_sheet.Cells(Row, 4) & "1月")
End If
If Len(new_value) = 0 And Len(new_value1) = 0 Then Exit Do
'将这一值插入SQL数据库
strsql = "insert into " & exceltst & "(" & exfieldA & "," & exfieldB & "," & exfieldC & "," & exfieldD & ") values('" & new_value & "','" & new_value1 & "','" & new_value2 & "','" & new_value3 & "')"
pubconn.Execute strsql
Row = Row + 1 '读取下一行数据
Loop
MsgBox "传输数据完成!", vbOKOnly, "完成!"
pubconn.Close
excel_app.Quit
Set rs = Nothing
Set pubconn = Nothing
Set excel_app = Nothing
Set excel_sheet = Nothing
End Sub
或参考以下
EXCEL文件要设置固定格式,还要设置命名范围,然后才可以导入
大致思路如下:
'// 设置打开 EXCEL 文件的连接字符串
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=excel文件名;Extended Properties=Excel 8.0"
'// 以记录集的形式打开 Excel 文件,adoConn 为 ADODB.Connection 对象
adoConn.Open strConn
'// 将数据插入到指定的表中(以ODBC的方式打开SQL数据库)
strSQL = "INSERT INTO [odbc;Driver={SQL Server};Server=服务器IP;Database=数据库;UID=用户名;PWD=密码].SQL中的表名 SELECT EXCEL中的字段 FROM EXCEL工作表名"
'// 执行导入语句
adoConn.Execute strSQL, , adExecuteNoRecords
⑸ VB 用ADODB 远程连接SQL2005 数据库
在1中选择计算机A的名称;
在2中选择windowsNT集成安全设置登陆;
在3中选择数据库名称posgf;
点测试连接,弹出测试OK,点确定就行了
⑹ 如何VB远程连接sql server2000,求详细过程
Dim Con As ADODB.Connection
Set Con = New ADODB.Connection
Con.ConnectionString = "driver={sql server};server=;uid=;pwd=;Database="
Con.ConnectionTimeout = 15
Con.Open
server是服务器地址,可以是IP也可以是域名,uid是SQL用户名,pwd是SQL登录密码,database是数据库名,
ConnectionTimeout这个是响应时间
⑺ VB 远程访问SQL SERVER数据库
对于跨网段连接数据库,用VB来实现的确是差强人意。
我曾经试用几个方法。
1.只要SQL数据库是独拦神立IP的,通过ADO是可以远程连接的,旅毕不过当数据量比较大时,这方法就不怎么如意了。。
2.将程简镇亏序打包成CAB,做成BS结构的。通过ADO的远程组件RDS进行连接。详细的自己网络吧。不过这个对一般人来说有点难度。
3.可以在服务器做一个服务端,通过WINSOCK组件,传送字串,也能实现此功能。
⑻ vb连接远程sql数据库
VB连Oracle1、首先你可以新建一个ini文件(如程序名.ini文件)2、在vb中添加API函数:Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long3、在vb中添加要加载ini文件的代码:Sub LoadInitial()
Dim ret As Integer
Dim retstr As String * 256
On Error GoTo LoadInitial SysIniFile = Command$
If SysIniFile = "" Then
SysIniFile = App.Path & "\" & App.EXEName & ".ini"
End If
ret = GetPrivateProfileString("SYS INI", "UID", "", retstr, 256, SysIniFile)
If ret <> 0 Then
gUid = Left$(retstr, ret)
End If
ret = GetPrivateProfileString("SYS INI", "PASSWD", "", retstr, 256, SysIniFile)
If ret <> 0 Then
gPasswd = Left$(retstr, ret)
End If
ret = GetPrivateProfileString("SYS INI", "DBLINK", "", retstr, 256, SysIniFile)
If ret <> 0 Then
gDsn = Left$(retstr, ret)
End If
Exit Sub
LoadInitial:
MsgBox "LoadInitial: " & Error
Resume
End Sub
4、添加打开数据库的vb代码:Public Sub DbOpen()
Dim strconn As String
On Error GoTo Errorhandler:
strconn = "DATA SOURCE=" & gDsn
Set cnAdo = New ADODB.Connection
cnAdo.Provider = "MSDAORA"
With cnAdo
.CursorLocation = adUseClient
.CommandTimeout = 10
.ConnectionString = strconn
.Open strconn, gUid, gPasswd
End With
Exit Sub
Errorhandler:
MsgBox "Database connect Error:" & Err.Description
End Sub