vb如何连接sql数据库??

vb连接sql数据库... vb连接sql数据库 展开
 我来答
匿名用户
2013-08-18
展开全部
VB连接SQL模块。直接拿去用吧。

Public conn As New ADODB.Connection
Public rs As New ADODB.Recordset
Public addFlag As Boolean

Public Function OpenCn(ByVal Cip As String) As Boolean
Dim mag As String
On Error GoTo strerrmag
Set conn = New ADODB.Connection
conn.ConnectionTimeout = 25
conn.Provider = "sqloledb"
conn.Properties("data source").Value = Cip '服务器的名字
conn.Properties("initial catalog").Value = "zqoa" '库名
'conn.Properties("integrated security").Value = "SSPI" '登陆类型
conn.Properties("user id").Value = "sa"
conn.Properties("password").Value = "sa"
conn.Open
OpenCn = True
addFlag = True
Exit Function
strerrmag:
mag = "Data can't connect"
Call MsgBox(mag, vbOKOnly, "Error:Data connect")
addFlag = False
Exit Function
End Function

Public Sub cloCn()
On Error Resume Next
If conn.State <> adStateClosed Then conn.Close
Set conn = Nothing
End Sub

Public Function openRs(ByVal strsql As String) As Boolean '连接数据库记录集
Dim mag As String
Dim rpy As Boolean
On Error GoTo strerrmag
Set rs = New ADODB.Recordset
If addFlag = False Then rpy = True
With rs
.ActiveConnection = conn
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open strsql
End With
addFlag = True
openRs = True
End
'Exit Function
strerrmag:
mag = "data not connect"
Call MsgBox(mag, vbOKOnly, "error:connect")
openRs = False
End
'Exit Function
End Function
Public Sub cloRs()
On Error Resume Next
If rs.State <> adStateClosed Then rs.Clone
Set rs = Nothing
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-08-18
展开全部
Public My_Cnn As New ADODB.Connection '连接数据库
StrCnn = "Provider=SQLOLEDB.1;Password=密码;Persist Security Info=True;User ID=用户名;Initial Catalog=数据库名;Data Source=服务器名"
My_Cnn.CursorLocation = adUseClient
My_Cnn.Open StrCnn

使用数据库
Dim sql As String
Dim My_temp As New ADODB.Recordset

执行SQL语句(一般插入,删除数据)
sql = "数据库语句"
My_cnn.Execute sql

读取数据
sql = "查询语句"
My_temp.Open sql, My_cnn, adOpenDynamic, adLockOptimistic

My_temp.field("字段名")
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-08-12
展开全部
你在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;
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
梦想程序猿
2015-06-02 · 超过36用户采纳过TA的回答
知道小有建树答主
回答量:62
采纳率:0%
帮助的人:43.6万
展开全部
Imports System.Data
Imports System.Data.OleDb
Module Module1
Public Function MdbMytab(ByVal str_select As String) As DataTable
Dim conn_mdb As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=mdb.mdb")
Dim cmd As New OleDbCommand(str_select, conn_mdb)
Dim da As New OleDbDataAdapter(cmd)
Dim mytb As DataSet = New DataSet
da.Fill(mytb, 0)
MdbMytab = mytb.Tables(0)
conn_mdb.Close()
End Function
End Module
以上为模块代码

以下为窗体代码:
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim mytb As DataTable = MdbMytab("select * from 表1")
DataGridView1.DataSource = mytb
ComboBox1.DisplayMember = "姓名"
ComboBox1.DataSource = mytb
End Sub
End Class
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-08-12
展开全部
一般用的ADO对象,比较简单。
ADO对象有以下几个对象,connection(用于连接库),command(命令),recordset(记录集)。
例如:dim conn as new ADOD.connection
conn.connectionstring =""
conn.open
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式