⑴ vb怎么连接数据库写入数据
'在使用前需要先安装Mysql的驱动,进行正确配置
'注意:必须给出正确的服务器名、数据库名、表名、数据库连接的用户名、密码
Option Explicit
Dim Cnn As ADODB.Connection '定义ADO连接对象
Dim Records As ADODB.Recordset '定义ADO记录集对象
'连接到数据库
Function CnnOpen(ByVal ServerName As String, ByVal DBName As String, ByVal TblName As String, ByVal User As String, ByVal PWD As String) '服务器名或IP、数据库名、登录用户、密码
Dim CnnStr As String '定义连接字符串
Set Cnn = CreateObject("ADODB.Connection") '创建ADO连接对象
Cnn.CommandTimeout = 15 '设置超时时间
CnnStr = "DRIVER={MySql ODBC 5.1 Driver};SERVER=" & ServerName & ";Database=" & DBName & ";Uid=" & User & ";Pwd=" & PWD & ";Stmt=set names GBK" '
Cnn.ConnectionString = CnnStr
Cnn.Open
End Function
'关闭连接
Function CnnClose()
If Cnn.State = 1 Then
Cnn.Close
End If
End Function
'把Excel写入MySql中的数据库
Function InsertToMySql(ByVal SheetName As String, ByVal TblName As String)
Dim SqlStr As String
Dim i, j As Integer
Dim Columns, Rows As Integer
Columns = VBAProject.func_public.GetTotalColumns(SheetName)
Rows = VBAProject.func_public.GetTotalRows(SheetName)
Set Records = CreateObject("ADODB.recordset")
'取得结果集并插入数据到数据库
Set Records = CreateObject("ADODB.Recordset")
'以下语句提供了插入思路,我只是把单条记录的插入方式改为循环,以把所有的记录添加到表中
'rs.Open "insert into newtable values('" & ActiveSheet.Cells(i, 1).Value & "'," & "'" & ActiveSheet.Cells(i, 2).Value & "')", cnn, 0
For i = 2 To Rows
SqlStr = "INSERT INTO " & TblName & " values('" & Sheets(SheetName).Cells(i, 1).Value & "'" '注意:" values('",字母“v”之前是有空格的!!!
For j = 2 To Columns
SqlStr = SqlStr & ",'" & Sheets(SheetName).Cells(i, j).Value & "'"
Next
SqlStr = SqlStr & ")"
Set Records = Cnn.Execute(SqlStr) 'rs.Open SqlStr, cnn, 0 不能用这条语句实现!!!
Next
MsgBox "Insert!", vbOKOnly, "Excel To MySql"
End Function
'清除对象
Function ClearObj()
Set Cnn = Nothing
Set Records = Nothing
End Function
⑵ 怎么把VB 和数据库连接起来
ado
//使用ADO控件集动态打开ACCESS数据库列程
//本方法主要面向客户选择目标ACCESS数据库
//取代弹出的ConnectionString的设置对话框
//注:ADOTable和ADOQuery控件的连接字串不同
//着名关心CSDN结贴率爱国人士-实用库Tip No 1 - 2003-9-25
function FileName_DataBasePassWord(FileName:string; DataBasePassWord:string=''):string;
begin
FileName_DataBasePassWord:='Provider=Microsoft.Jet.OLEDB.4.0;'+
'User ID=Admin;Data Source='+
form1.OpenDialog1.FileName+ //数据库文件路径
';Mode=Share Deny None;Extended Properties="";'+
'Jet OLEDB:System database="";'+
'Jet OLEDB:Registry Path="";'+
'Jet OLEDB:Database Password='+
DataBasePassWord+ //数据库密码
';Jet OLEDB:Engine Type=5;'+
'Jet OLEDB:Database Locking Mode=1;'+
'Jet OLEDB:Global Partial Bulk Ops=2;'+
'Jet OLEDB:Global Bulk Transactions=1;'+
'Jet OLEDB:New Database Password="";'+
'Jet OLEDB:Create System Database=False;'+
'Jet OLEDB:Encrypt Database=False;'+
'Jet OLEDB:Don''t Copy Locale on Compact=False;'+
'Jet OLEDB:Compact Without Replica Repair=False;'+
'Jet OLEDB:SFP=False';
end;
procere TForm1.DynamicOpenAdoAccess_BtnClick(Sender: TObject);
begin
form1.OpenDialog1.Filter:='Text files (*.mdb)|*.MDB';
form1.OpenDialog1.DefaultExt:='MDB';
form1.OpenDialog1.InitialDir:=application.GetNamePath;
form1.OpenDialog1.Title:='指定Access数据库Mdb文件的位置';
if form1.OpenDialog1.Execute then
begin
form1.ADOConnection1.LoginPrompt:=false;
//带密码数据库的使用
//form1.ADOConnection1.ConnectionString:=FileName_DataBasePassWord(form1.OpenDialog1.FileName,'ghyghost');
//不带密码数据的使用
form1.ADOConnection1.ConnectionString:=FileName_DataBasePassWord(form1.OpenDialog1.FileName);
form1.ADOConnection1.Open;
form1.ADOQuery1.open; //ADOQuery1's SQL Properties is 'select * from [tablename]'
end;
end;
⑶ vb与sql数据库连接
一、是否修改后的密码没有生效,原密码能否连接
二、是否修改后的新密码含有特殊字符
⑷ 如何用vb连接mysql数据库
VB连接MYSQL数据的方法
博客分类:
VB
VBMySQLSQLServerSQLSocket
第一步:上网http://dev.mysql.com/downloads/connector/odbc/下载mysql-connector-odbc-5.1.6-win32.msi
第二步:安装mysql-connector-odbc-5.1.6-win32.msi
第三步:运行VB并新建一标准EXE工程, 通过菜单工程->引用 打开”引用”对话框, 找到 MicrosoftActiveX Data Objects x.x Library , 其中 x.x 是版本号, 可能会有很多个, 这里我选择的是2.5
第四步:打开代码窗口, 在 Form_Load 过程中输入下面的代码(具体说明在代码的注释中):
Vb代码
' 定义并创建数据库连接和访问对象
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
' 定义数据库连接字符串变量
Dim strCn As String
' 定义数据库连接参数变量
Dim db_host As String
Dim db_user As String
Dim db_pass As String
Dim db_data As String
' 定义 SQL 语句变量
Dim sql As String
' 初始化数据库连接变量
db_host = "localhost"
db_user = "root"
db_pass = ""
db_data = "erqiuda"
' MySQL ODBC 连接参数
'+------------+---------------------+----------------------------------+
'| 参数名 | 默认值 | 说明 |
'+------------+------------------------------------------------------–+
'| user | ODBC (on Windows) | MySQL 用户名 |
'| server | localhost | MySQL 服务器地址 |
'| database | | 默认连接数据库 |
'| option | 0 | 参数用以指定连接的工作方式 |
'| port | 3306 | 连接端口 |
'| stmt | | 一段声明, 可以在连接数据库后运行 |
'| password | | MySQL 用户密码 |
'| socket | | (略) |
'+------------+---------------------+----------------------------------+
' 详细查看官方说明
' http://dev.mysql.com/doc/refman/5.0/en/myodbc-configuration-connection-parameters.html
strCn = "DRIVER={MySQL ODBC 5.1 Driver};" & _
"SERVER=" & db_host & ";" & _
"DATABASE=" & db_data & ";" & _
"UID=" & db_user & ";PWD=" & db_pass & ";" & _
"OPTION=3;stmt=SET NAMES GB2312"
' stmt=SET NAMES GB2312
' 这句是设置数据库编码方式
' 中文操作系统需要设置成 GB2312
' 这样中文才不会有问题
' 版本要求 mysql 4.1+
' 连接数据库
cn.Open strCn
' 设置该属性, 使 recordcount 和 absolutepage 属性可用
cn.CursorLocation = adUseClient
' 访问表users
sql = "select * from users"
rs.Open sql, cn
MsgBox rs.RecordCount
⑸ 如何用vb语句连接SQL数据库
可以参考下边自己稍作修改即可:
Sub Main()
On Error GoTo On_error
'frmWelcome.Show
Set con = New ADODB.Connection
con.ConnectionString = ("Provider=SQLOLEDB;User ID=sa;PWD=;Initial Catalog=human;Data Source=.")
con.Open
con.CommandTimeout = 20
Load frmLogin
frmLogin.Show vbModal
Exit Sub
On_error:
If Err.Number = -2147467259 Or Err.Number = -2147217900 Then
Unload frmWelcome
MsgBox "检查到 SQL Server 未安装或软件所需数据库未安装..." & vbCrLf & "正在进一步检查 SQL Server 的安装情况,若已启动则开始安装数据库... ", vbInformation + vbOKOnly, "系统提示"
Shell (App.Path & "\Installdb.exe")
End
Else
MsgBox Err.Number & Err.Description, vbInformation + vbOKOnly, "系统提示"
End
End If
End Sub
⑹ vb如何连接sql数据库
1、打开代码窗口,添加引用:Imports System.Data.SqlClient。
⑺ VB如何连接SQL数据库并登录
Option Explicit
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=服务器名(默认为:MERRYCHINA)" '这是连接SQL数据库的语句
Conn.Open ConnStr
rs.CursorLocation = adUseClient
rs.Open Trim$(SQL), Conn, adOpenDynamic, adLockOptimistic
Set Selectsql = rs
'Exit Function
'MyErr:
'Set rs = Nothing
'Set Conn = Nothing '释放相关的系统资源
'MsgBox Err.Description, vbInformation, "系统提示" '显示出错信息
End Function
Private Sub Form_Load()
Dim SQL As String
Dim rs As ADODB.Recordset
Dim X As Long
On Error GoTo Err_box
SQL = " select * from 用户表"
Set rs = Selectsql(SQL)
If rs.RecordCount > 0 Then
rs.MoveFirst
For X = 1 To rs.RecordCount
Combo1.AddItem rs.Fields("用户名").Value
rs.MoveNext
Next X
Combo1.ListIndex = 0
End If
rs.Close
Exit Sub
Err_box:
End Sub
Private Sub Command1_Click()
Dim SQL As String
Dim rs As ADODB.Recordset
If Text1.Text = "" Then
MsgBox "请输入口令!", 16
Text1.SetFocus
Exit Sub
End If
If Combo1.Text = "" Then
MsgBox "请选择登录用户!", 16
Combo1.SetFocus
Exit Sub
End If
SQL = "SELECT * FROM 用户表 WHERE 用户名='" & Combo1.Text & "' AND 密码='" & Text1.Text & "' "
Set rs = Selectsql(SQL)
If rs.RecordCount > 0 Then
Form1.Show '想要打开的主窗体
MsgBox "恭喜兄弟,登录成功!", 64, "提示"
Unload Me
Else
MsgBox "口令不对,请重新输入!", 16, "提示"
Text1.SetFocus
End If
End Sub
'**********************************************************************
'说明:1) 在工程中引用Microsoft ActiveX Data Objects 2.8 Library ,其它版本也行如:2.0
' 2) 在窗体中加Texe1.text(文本框控件),Combo1.text(组合框控件),Command1(命令按钮)各一个
' 3) 在SQL Server2000中创建数据库,新建表"用户表",表中包含"ID,姓名,密码"等字段,然后将以上代码复制,OK搞定
4) 以上方式无需加载ADO控件,方便!
⑻ VB6.0 如何连接SQL 数据库
你在SQL SERVER中新建一个数据库,然后将这个数据库生成脚本文件,根据情况修改后转存为MASTER下的一个存储过程。至于数据库名和数据文件路径可以做成参数传入,然后在VB中用连到MASTER后执行这个存储过程。
用VB与建立的SQL数据库连接
1.使用ODBC DSN连接字符串
DSN=mySystemDSN;Uid=myUsername;Pwd=myPassword
----------------------------------------------
2.使用无ODBC DSN的连接字符串
(1)标准安全性
Driver={SQL Server};Server=MyServerName;DataBase=myDatabaseName; _
Uid=myUsername;Pwd=myPassword
------------------------------------------------
(2)信任连接(分两种)
1)连接字符串1
Driver={SQL Server};Server=MyServerName;DataBase=myDatabaseName;Uid=;Pwd=;
2)连接字符串2
Driver={SQL Server};Server=MyServerName;DataBase=myDatabaseName;Trusted_Connection=yes
---------------------------------------------------------------------------
(3)连接本地SQL SERVER
Driver={SQL Server};Server=(local);DataBase=myDatabaseName; _
Uid=myUsername;Pwd=myPassword
-------------------------------------------------------------
(4)连接远程SQL SERVER
Driver={SQL Server};Server=xxx.xxx.xxx.xxx; _
Address=xxx.xxx.xxx.xxx,1433; _
Network=DBMMSOCN;DataBase=myDatabaseName; _
Uid=myUsername;Pwd=myPassword
-------------------------------------------------------
3.使用OLEDB PROVIDER FOR SQL SERVER的连接字符串
(1)标准安全性
Provider=SQLOLEDB.1; _
Password=myPassword; _
User ID=myUsername; _
Initial Catalog=myDatabaseName; _
Data Source=MyServerName
-------------------------------------------
(2)信任连接
Provider=SQLOLEDB.1; _
Initial Catalog=myDatabaseName; _
Data Source=MyServerName; _
Integrated Security=SSPI
--------------------------------------
(3)连接到命名实例
Provider=SQLOLEDB.1; _
Initial Catalog=myDatabaseName; _
Data Source=myServerName\myInstanceName; _
Password=myPassword; _
User ID=myUsername
--------------------------------------
(4)连接到本地SQL SERVER
Provider=SQLOLEDB.1; _
Password=myPassword; _
User ID=myUsername; _
Initial Catalog=myDatabaseName; _
Data Source=(local)
-----------------------------------------
(5)连接到远程SQL SERVER
Provider=SQLOLEDB.1; _
Netwprk Library=DBMMSOCN; _
Data Source=xxx.xxx.xxx.xxx,1433; _
Password=myPassword; _
User ID=myUsername; _
Initial Catalog=myDatabaseName;
⑼ VB如何连接网络数据库
在工程里找到Microsoft ADO Data Control控件,拖到窗体上,事件中这么写:
Private Sub Coammand1_Click()
Adodc1.connectionstring="Driver={SQL Server};Server=服务器IP;Uid=用户名;Pwd=密码;DataBse=数据库名"
Adodc1.RecordSource="SELECT * FROM 表名 WHERE 条件"
Adodc1.Refrsh
End Sub
要进行数据操作就这样引用:
Adodc1.RecordSet.Fields("字段名")这就是操作数据字段
把Adodc1绑定到数据控件那就是绑定数据源
⑽ 在vb中要怎么与数据库连接呢
Dim r As New Recordset
Set com = New ADODB.Connection
com.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & App.Path & "\data\users.mdb;Persist Security Info=False"
Set r = New ADODB.Recordset
r.Open "select * from 表格名称 ", com, 1, 3
If Text1.Text = "" Then
MsgBox "请输入账号!", vbOKOnly + vbExclamation, "警告"
Text1.SetFocus
Else
If Text2.Text = "" Then
MsgBox "请输入密码!", vbOKOnly + vbExclamation, "警告"
Text2.SetFocus
Else
r.Find "账号='" & Text1.Text & "'"
If r.EOF = True Then
MsgBox "账号错误!", vbOKOnly + vbExclamation, "警告"
Text1.SetFocus
Else
If r.Fields!密码 = Text2 Then
MsgBox "登陆成功!"
Form6.Show
Unload Me
Else
MsgBox "密码错误!", vbOKOnly + vbExclamation, "警告"
Text2.SetFocus
End If
End If
End If