vb6.0连接sql2000数据库的具体步骤
发布网友
发布时间:2022-05-07 03:43
我来回答
共8个回答
懂视网
时间:2022-05-07 08:04
使用ADO.NET自定义类MyDBase连接SQL Server数据库 using System; using System.Data; using System.Data.SqlClient; public class MyDBase { bool ECode=false; string ES; SqlConnection cn=new System.Data.SqlClient.SqlConnection(); DataSet Rs; publ
使用ADO.NET自定义类MyDBase连接SQL Server数据库
using System;
using System.Data;
using System.Data.SqlClient;
public class MyDBase
{
bool ECode=false;
string ES;
SqlConnection cn=new System.Data.SqlClient.SqlConnection();
DataSet Rs;
public MyDBase(string MyDBServerName,string MyDataBaseName)
{
ECode = false;
cn.ConnectionString="workstation id="+MyDBServerName+";packet size=4096;integrated security=SSPI;data source="+MyDBServerName+";persist security info=False;initial catalog="+MyDataBaseName;
try
{
cn.Open();
}
catch (Exception e)
{
ES = e.Message;
ECode = true;
}
}
public MyDBase(string MyDBServerName, string MyDataBaseName, string sUerName, string sPasswd)
{
ECode = false;
string sConn = "workstation id=" + MyDBServerName + ";packet size=4096;user id=" + sUerName + ";pwd=" + sPasswd + ";data source=" + MyDBServerName + ";persist security info=False;initial catalog=" + MyDataBaseName;
cn.ConnectionString = sConn;
try
{
cn.Open();
}
catch (Exception e)
{
ES = e.Message;
ECode = true;
}
}
public DataSet GetRecordset(string Sqls)
{
SqlCommand sqlCmd= new SqlCommand();
sqlCmd.Connection = cn;
sqlCmd.CommandText = Sqls;
try
{
SqlDataAdapter adp = new SqlDataAdapter(sqlCmd);
Rs = new DataSet();
adp.Fill(Rs);
}
catch (Exception e)
{
ES = e.Message;
ECode = true;
return null;
}
return (Rs);
}
public int ExecuteSQLScalar(string Sqls)
{
string s;
SqlCommand sqlCmd= new SqlCommand();
sqlCmd.Connection = cn;
sqlCmd.CommandText = Sqls;
sqlCmd.CommandType = CommandType.Text;
try
{
s = sqlCmd.ExecuteScalar().ToString();
}
catch (Exception e)
{
ES = e.Message;
ECode = true;
return -1;
}
return(int.Parse(s));
}
public string ExecuteSQLScalarTOstring(string Sqls)
{
string s;
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = cn;
sqlCmd.CommandText = Sqls;
sqlCmd.CommandType = CommandType.Text;
try
{
s = sqlCmd.ExecuteScalar().ToString();
}
catch (Exception e)
{
ES = e.Message;
ECode = true;
return "-1";
}
return s;
}
public string ExecuteSQLWithTrans(string Sqls)
{
string s;
SqlTransaction myTrans;
myTrans=cn.BeginTransaction();
SqlCommand sqlCmd= new SqlCommand();
sqlCmd.Connection = cn;
sqlCmd.CommandText = Sqls;
sqlCmd.CommandType = CommandType.Text;
sqlCmd.Transaction =myTrans;
sqlCmd.ExecuteNonQuery();
//Sqls="SELECT @@IDENTITY AS ID";
sqlCmd.CommandText =Sqls;
try
{
s = sqlCmd.ExecuteScalar().ToString();
}
catch (Exception e)
{
ES = e.Message;
ECode = true;
myTrans.Commit();
return "";
}
myTrans.Commit();
return(s);
}
public void ExecuteSQL(string Sqls)
{
SqlCommand sqlCmd= new SqlCommand();
sqlCmd.Connection = cn;
sqlCmd.CommandText = Sqls;
sqlCmd.CommandType = CommandType.Text;
try
{
sqlCmd.ExecuteNonQuery();
}
catch (Exception e)
{
ES = e.Message;
ECode = true;
}
}
public SqlDataReader DBDataReader(string Sqls)
{
SqlCommand sqlCmd= new SqlCommand();
sqlCmd.Connection = cn;
sqlCmd.CommandText = Sqls;
sqlCmd.CommandType = CommandType.Text;
try
{
return sqlCmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception e)
{
ES = e.Message;
ECode = true;
return null;
}
}
public void DBClose()
{
try
{
cn.Close();
}
catch (Exception e)
{
ES = e.Message;
ECode = true;
}
}
public bool ErrorCode()
{
return ECode;
}
public string ErrMessage()
{
return ES;
}
~MyDBase()
{
//if (cn.State==ConnectionState.Open ) cn.Close();
热心网友
时间:2022-05-07 05:12
VB6.0使用ADO对象连接数据库:
有偿征集原创文章,为您创收 投递文章 减小字体 增大字体
1. 定义连接对象Global DBconnect As New ADODB.Connection2. 执行连接语句If DBconnect.State = adStateOpen And Not IsEmpty(adStateOpen) Then DBconnect.Close l 连接ODBC DBconnect.ConnectionString = "dsn
1. 定义连接对象
Global DBconnect As New ADODB.Connection
2. 执行连接语句
If DBconnect.State = adStateOpen And Not IsEmpty(adStateOpen) Then DBconnect.Close
连接ODBC
DBconnect.ConnectionString = "dsn=DataAliasName;uid=UserID;pwd=Passwd;"
直接连接Access为
Dbconnect.Provider = "Microsoft.jet.OLEDB.4.0" // Access 97为3.51
DBconnect.ConnectionString = "FilePathFileName.mdb"
连接Oracle
DBconnect.Provider = "MSADORA"
DBconnect.ConnectionString = "user/mypass@servicename
连接SQL Server
DBconnect.Provider = "SQLOLEDB.1"
DBconnect.ConnectionString = "DATABASE= ;SERVER= ;UID= ;PWD= ;"
或者可以使用 DBconnect.Open "SERVER" , "USERID" , "PASSWORD"
DBConnect.Open
3. 取查询结果集
Global RS As New ADODB.Recordset
Global PS As New ADODB.Recordset
If RS.State = adStateOpen And Not IsEmpty(adStateOpen) Then RS.Close
RS.Open SQLStr, DBconnect, adOpenKeyset
4. 执行SQL语句
DBconn.Execute SQLStr
5. 关闭数据库
DBconnect.Close
VB使用ADO数据库可以分为有源数据库和无源数据库,即是否使用了DSN数据源,如下例:
1、在连接数据库前首先要在VB菜单中“工程”-“引用”从中选择 microsoft activeX Data objects 2.6 library和 microsoft activeX Data objects recordset 2.6两个组件,这是在连接数据前所必做的工作。
2、接下来定义ADO的连接对象,如下所示:
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
第一种方法采用的是无源的数据库连接方法
conn.ConnectionString = "Driver={sql server};server=JSZX3-11;uid=sa;pwd=;database=wzc"
conn.ConnectionTimeout = 30
conn.Open
rs.Open "select 用户名,密码 from login where 用户名='" & Trim(Combo1.Text) & "' And 密码='" & Trim(Text1.Text) & "'", conn, adOpenStatic, adLockReadOnly, adCmdText
If rs.EOF = True And rs.BOF Then
m = MsgBox("请重新登录", vbExclamation)
Text1.Text = ""
Text1.SetFocus
Else
Form1.Hide
End If
rs.Close
这是第二种方法连接数据库的方法,这一种方法是采用有源的方法与数据库连接的方法,代码如下所示:
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
conn.ConnectionString = "DSN=login;uid=sa;pwd=;"
conn.ConnectionTimeout = 30
conn.Open
rs.Open "select 用户名 from login", conn, adOpenStatic, adLockReadOnly, adCmdText
Dim i As String
For t = 0 To Val(rs.RecordCount) - 1
i = Trim(rs.Fields("用户名").Value)
rs.MoveNext
Combo1.AddItem i
Next t
rs.Close
连接数据库的方法很多很多,只要各位去勇敢的偿试一下,就可以感觉数据库的方法并不是很难,本人以前也很想做这方法的工作,只是学识不深,未能完成胸中大志,如今对这些方法的多少有一些了解,特意为那些初学者以供学习之用。
热心网友
时间:2022-05-07 06:30
数据库的连接可通过ADO控件实现,须在工程部件中
选择Micrsoft ADO Data Control6.0(OLEDB),然后在窗体
中添加ADO控件.
利用ADO连接数据库有两种方法,具体如下:
1) 通过ADODC属性页实现连接
在ADODC属性页中选择生成按钮,进入数据链接属性对话框;然后选择该对话框中的连接属性页,选择或输入服务器名称和数据库等重要信息;最后测试连接,连接成功后,按确定按钮,返回到属性页对话框,可获得连接字符串,如下例:
Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Science_File;Data Source=Data_Server
其中sa是用户名;Science_File是数据库名;Data_Server是数据库名。
通过下列语句,即可连接到指定的数据库:
dim odbcstr as String, adocon As New ADODB.Connection
odbcstr = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Science_File;Data Source=Data_Server"
adocon.Open odbcstr '连接到数据库
2) 直接使用连接语句实现
连接数据库的语句如下:
Dim ado as ADODC
ado.ConnectionString = "Provider=SQLOLEDB.1;Password=" & User_Pwd & ";Persist Security Info=True;User ID=" & User_Name & ";Initial Catalog=" & Data_Name & ";Data Source=" & server_name
其中User-Pwd是用户密码;User_Name是用户名;Data_Name是数据库名;server_name是服务器名。
0回答者: xiaoqinhuiyuan - 二级 2008-5-4 19:46
我个人喜欢用第一种。比较直观,不过第二种也较为简单。
热心网友
时间:2022-05-07 08:05
有现成的《学生信息管理信息系统》,SQL 2000数据库,加HI发给你
热心网友
时间:2022-05-07 09:56
下面是SQL数据库连接代码,请参考:
Public
conn
As
New
ADODB.Connection
conn.ConnectionString
=
"driver={SQL
Server};server=服务器名称;uid=sa;pwd=密码;database=数据库名称"
conn.Open
热心网友
时间:2022-05-07 12:04
1.Access数据库的DSN-less连接方法:
set adocon=Server.Createobject("adodb.connection")
adoconn.Open"Driver={Microsoft Access Driver(*.mdb)};DBQ="& _
Server.MapPath("数据库所在路径")
2.Access OLE DB连接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Provider=Microsoft.Jet.OLEDB.4.0;"& _
"Data Source=" & Server.MapPath("数据库所在路径")
3.SQL server连接方法:
set adocon=server.createobject("adodb.recordset")
adocon.Open"Driver={SQL Server};Server=(Local);UID=***;PWD=***;"& _
"database=数据库名;"
4.SQL server OLE DB连接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"provider=SQLOLEDB.1;Data Source=RITANT4;"& _
"user ID=***;Password=***;"& _
"inital Catalog=数据库名"
5.Oracle 连接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft odbc for oracle};server=oraclesever.world;uid=admin;pwd=pass;"
6.Oracle OLE DB 连接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Provider=OraOLEDB.Oracle;data source=dbname;user id=admin;password=pass;"
7.dBase 连接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft dbase driver(*.dbf)};driverid=277;dbq=------------;"
8.mySQL 连接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={mysql};database=yourdatabase;uid=username;pwd=yourpassword;option=16386;"
9.Visual Foxpro 连接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft Visual Foxpro driver};sourcetype=DBC;sourceDB=*.dbc;Exclusive=No;"
10.MS text 连接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft text driver(*.txt; *.csv)};dbq=-----;"&_
"extensions=asc,csv,tab,txt;Persist SecurityInfo=false;"
11.MS text OLE DB 连接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Provider=microsof.jet.oledb.4.0;data source=your_path;"&_
"Extended Properties'text;FMT=Delimited'"
回答者:zhequ - 助理 二级 1-5 10:12
<%
set conn=server.create.createobject("adodb.connection")
conn.open "DSN=esinc;UID=admin;PWD=;"
%>
上面是用本地服务器
<%
set conn=server.create.createobject("adodb.connection")
conn.open "driver=driver do microsoft access (*.mdb);UID=admin;PWD=;DBQ="&server.mappath("esinc.mdb")
%>
这个是在其他地方也可以用 esinc是mdb的文件名自己改
热心网友
时间:2022-05-07 14:29
我有视频教程,hi我就给你哦~!
热心网友
时间:2022-05-07 17:10
SQL SERVER 2000 数据库代码比较简单,无非数据库连接语句不同,其他和Access数据库没有大的区别。
如果“程序要移动到其他机子上”,移动到的机子上必须同样安装SQL SERVER 2000 数据库,而且服务器名称、数据库名称必须和原服务器名称、数据库名称相同,如果有数据库密码,必须保持一致。
下面是SQL数据库连接代码,请参考:
Public conn As New ADODB.Connection
conn.ConnectionString = "driver={SQL Server};server=服务器名称;uid=sa;pwd=密码;database=数据库名称"
conn.Open