Ⅰ vb6.0中用adodc控制項連接sql2005
VB連接SQL資料庫的模塊:
Public conn As New ADODB.Connection
Public rs As New ADODB.Recordset
Public addFlag As Boolean
Public Function OpenCn() 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 = "127.0.0.1" ''伺服器的名字
conn.Properties("initial catalog").Value = "chart" ''庫名
conn.Properties("integrated security").Value = "SSPI" ''登陸類型
conn.Open
OpenCn = True
addFlag = True
Exit Function
strerrmag:
mag = "Data can''t connect"
Call MsgBox(mag, vbOKCancel, "Error:Data connect")
addFlag = False
End
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
Exit Function
strerrmag:
mag = "data not connect"
Call MsgBox(mag, vbOKCancel, "error:connect")
openrs = False
End
End Function
Public Sub clors()
On Error Resume Next
If rs.State <> adStateClosed Then rs.Clone
Set rs = Nothing
End Sub
Ⅱ 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語句,還可編寫出其他方式的查找,這里就不多做介紹了!
Ⅲ 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的sql資料庫連接
先建一個工程,建立一個form1,form1中的代碼如下:
Dim rsNew As ADODB.Recordset
Dim CnNew As ADODB.Connection
Dim wsh As New WshShell
Dim serverlink As String
Private Sub Command1_Click()
Dim Mag As String
On Error GoTo strErrMag
Set CnNew = New ADODB.Connection
Set wsh = New WshShell
Text1 = wsh.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\ZJRB\SERVERNAME")
Text3 = wsh.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\ZJRB\PWD")
Text2 = wsh.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\ZJRB\USENAME")
serverlink = "Provider=SQLOLEDB.1;Password=" + Trim(Text3.Text) + ";Persist Security Info=True;User ID=" + Trim(Text2.Text) + ";Initial Catalog=master;Data Source=" + Trim(Text1.Text)
CnNew.Open serverlink
Exit Sub
strErrMag:
Mag = "資料庫連接錯誤,在下列的窗體中請輸入正確的信息!"
MsgBox Mag, vbOKOnly, "錯誤:資料庫連接"
Me.Enabled = False
Load Form2
Form2.Show
End Sub
再建一個form2,form2的代碼如下:
Dim rsNew As ADODB.Recordset
Dim CnNew As ADODB.Connection
Dim wsh As New WshShell
Dim serverlink As String
Private Sub Command1_Click()
Dim Mag As String
On Error GoTo strErrMag
Set CnNew = New ADODB.Connection
Set wsh = New WshShell
serverlink = "Provider=SQLOLEDB.1;Password=" + Trim(Text3.Text) + ";Persist Security Info=True;User ID=" + Trim(Text2.Text) + ";Initial Catalog=master;Data Source=" + Trim(Text1.Text)
CnNew.Open serverlink
MsgBox ("資料庫連接成功!"), vbInformation + vbOKOnly, "提示信息"
Set CnNew = Nothing
wsh.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\ZJRB\SERVERNAME", Trim(Text1)
wsh.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\ZJRB\PWD", Trim(Text3)
wsh.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\ZJRB\USENAME", Trim(Text2)
Form1.Enabled = True
Unload Me
Exit Sub
strErrMag:
Mag = "資料庫連接錯誤,在下列的窗體中請輸入正確的信息!"
MsgBox Mag, vbOKOnly, "錯誤:資料庫連接"
End Sub
試試看吧,這個把SQL的登錄名與密碼放在注冊表中,同樣也可以在注冊表中放軟體的試用期限!