惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
H
Help Net Security
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
宝玉的分享
宝玉的分享
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 司徒正美
量子位
N
Netflix TechBlog - Medium
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
Recorded Future
Recorded Future
博客园 - 三生石上(FineUI控件)
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog
Scott Helme
Scott Helme
The Last Watchdog
The Last Watchdog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
AI
AI
WordPress大学
WordPress大学
Security Archives - TechRepublic
Security Archives - TechRepublic
Google Online Security Blog
Google Online Security Blog
U
Unit 42
V2EX - 技术
V2EX - 技术
MongoDB | Blog
MongoDB | Blog
Schneier on Security
Schneier on Security
博客园 - Franky
H
Heimdal Security Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
Cloudbric
Cloudbric
B
Blog RSS Feed
N
News | PayPal Newsroom
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园_首页
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
雷峰网
雷峰网

博客园 - columbus.yan

windows server 2008 支持 .net framework 4.0 asp.net两层架构常用 - columbus.yan - 博客园 ASP.NET中备份恢复数据库 - columbus.yan - 博客园 SQL Server连接失败错误小结 一条指令搞定“关于内存不能READ“的问题 windows2003中安装.netframework1.1 解决数据库日志文件过满的又一方法 Windows XP 注册表修改大全 两步彻底解决数据库中的日志已满的情况 T-SQL使用技巧集锦4 T-SQL使用技巧集锦3 T-SQL使用技巧集锦2 T-SQL使用技巧集锦1 在Asp.net中如何实现弹出提示对话框 C#中常用的经典文件操作方法 多条件查询存储过程 调用存储过程 经典存储过程例子 sql大全
T-SQL使用技巧集锦5
columbus.yan · 2008-11-12 · via 博客园 - columbus.yan

万能数据库连接程序!

简介: 连接各种类型数据库 及 对数据库操作的函数 

下面这部分程序可说是万能的数据库连接程序几乎可以连接所有的MS数据库,自己拿去研究吧(这个程序是“ASP网页制作教程”这本书里面的——一本好书): 
<% 
'--------------------------------------------------- 
Function GetMdbConnection( FileName ) 
Dim Provider, DBPath 

Provider = "Provider=Microsoft.Jet.OLEDB.4.0;" 
DBPath = "Data Source=" & Server.MapPath(FileName) 
Set GetMdbConnection = GetConnection( Provider & DBPath ) 
End Function 

'--------------------------------------------------- 
Function GetSecuredMdbConnection( FileName, Password ) 
Dim Provider, DBPath 

Provider = "Provider=Microsoft.Jet.OLEDB.4.0;" 
DBPath = "Data Source=" & Server.MapPath(FileName) 
Set GetSecuredMdbConnection = GetConnection( Provider & DBPath & ";Jet OLEDB:Database Password=" & Password ) End Function 

'--------------------------------------------------- 
Function GetDbcConnection( FileName ) 
Dim Driver, SourceType, DBPath 

Driver = "Driver={Microsoft Visual FoxPro Driver};" 
SourceType = "SourceType=DBC;" 
DBPath = "SourceDB=" & Server.MapPath( FileName ) 
Set GetDbcConnection = GetConnection( Driver & SourceType & DBPath ) 
End Function 

'--------------------------------------------------- 
Function GetDbfConnection( Directory ) 
Dim Driver, SourceType, DBPath 

Driver = "Driver={Microsoft Visual FoxPro Driver};" 
SourceType = "SourceType=DBF;" 
DBPath = "SourceDB=" & Server.MapPath( Directory ) 
Set GetDbfConnection = GetConnection( Driver & SourceType & DBPath ) 
End Function 

'--------------------------------------------------- 
Function GetExcelConnection( FileName ) 
Dim Driver, DBPath 

Driver = "Driver={Microsoft Excel Driver (*.xls)};" 
DBPath = "DBQ=" & Server.MapPath( FileName ) 
Set GetExcelConnection = GetConnection( Driver & "ReadOnly=0;" & DBPath ) End Function 

'--------------------------------------------------- 
Function GetTextConnection( Directory ) 
Dim Driver, DBPath 

Driver = "Driver={Microsoft Text Driver (*.txt; *.csv)};" 
DBPath = "DBQ=" & Server.MapPath( Directory ) 
Set GetTextConnection = GetConnection( Driver & DBPath ) 
End Function 

'--------------------------------------------------- 
Function GetSQLServerConnection( Computer, UserID, Password, Db ) 
Dim Params, conn 

Set GetSQLServerConnection = Nothing 
Params = "Provider=SQLOLEDB.1" 
Params = Params & ";Data Source=" & Computer 
Params = Params & ";User ID=" & UserID 
Params = Params & ";Password=" & Password 
Params = Params & ";Initial Catalog=" & Db 
Set conn = Server.CreateObject("ADODB.Connection") 
conn.Open Params 
Set GetSQLServerConnection = conn 
End Function 

'--------------------------------------------------- 
Function GetMdbRecordset( FileName, Source ) 
Set GetMdbRecordset = GetMdbRs( FileName, Source, 2, "" ) 
End Function 

'--------------------------------------------------- 
Function GetMdbStaticRecordset( FileName, Source ) 
Set GetMdbStaticRecordset = GetMdbRs( FileName, Source, 3, "" ) 
End Function 

'--------------------------------------------------- 
Function GetSecuredMdbRecordset( FileName, Source, Password ) 
Set GetSecuredMdbRecordset = GetMdbRs( FileName, Source, 2, Password ) End Function 

'--------------------------------------------------- 
Function GetSecuredMdbStaticRecordset( FileName, Source, Password ) 
Set GetSecuredMdbStaticRecordset = GetMdbRs( FileName, Source, 3, Password ) End Function 

'--------------------------------------------------- 
Function GetDbfRecordset( Directory, SQL ) 
Set GetDbfRecordset = GetOtherRs( "Dbf", Directory, SQL, 2 ) 
End Function 

'--------------------------------------------------- 
Function GetDbfStaticRecordset( Directory, SQL ) 
Set GetDbfStaticRecordset = GetOtherRs( "Dbf", Directory, SQL, 3 ) 
End Function 

'--------------------------------------------------- 
Function GetDbcRecordset( FileName, SQL ) 
Set GetDbcRecordset = GetOtherRs( "Dbc", FileName, SQL, 2 ) 
End Function 

'--------------------------------------------------- 
Function GetDbcStaticRecordset( FileName, SQL ) 
Set GetDbcStaticRecordset = GetOtherRs( "Dbc", FileName, SQL, 3 ) 
End Function 

'--------------------------------------------------- 
Function GetExcelRecordset( FileName, SQL ) 
Set GetExcelRecordset = GetOtherRs( "Excel", FileName, SQL, 2 ) 
End Function 

'--------------------------------------------------- 
Function GetExcelStaticRecordset( FileName, SQL ) 
Set GetExcelStaticRecordset = GetOtherRs( "Excel", FileName, SQL, 3 ) 
End Function 

'--------------------------------------------------- 
Function GetTextRecordset( Directory, SQL ) 
Set GetTextRecordset = GetOtherRs( "Text", Directory, SQL, 2 ) 
End Function 

'--------------------------------------------------- 
Function GetTextStaticRecordset( Directory, SQL ) 
Set GetTextStaticRecordset = GetOtherRs( "Text", Directory, SQL, 3 ) 
End Function 

'--------------------------------------------------- 
Function GetSQLServerRecordset( conn, source ) 
Dim rs 

Set rs = Server.CreateObject("ADODB.Recordset") 
rs.Open source, conn, 2, 2 
Set GetSQLServerRecordset = rs 
End Function 

'--------------------------------------------------- 
Function GetSQLServerStaticRecordset( conn, source ) 
Dim rs 

Set rs = Server.CreateObject("ADODB.Recordset") 
rs.Open source, conn, 3, 2 
Set GetSQLServerStaticRecordset = rs 
End Function 

'--------------------------------------------------- 
Function GetConnection( Param ) 
Dim conn 

On Error Resume Next 
Set GetConnection = Nothing 
Set conn = Server.CreateObject("ADODB.Connection") 
If Err.Number <> 0 Then Exit Function 

  conn.Open Param 
  If Err.Number <> 0 Then Exit Function 
  Set GetConnection = conn 
End Function 

'--------------------------------------------------- 
Function GetMdbRs( FileName, Source, Cursor, Password ) 
  Dim conn, rs 

  On Error Resume Next 
  Set GetMdbRs = Nothing 
  If Len(Password) = 0 Then 
      Set conn = GetMdbConnection( FileName ) 
  Else 
      Set conn = GetSecuredMdbConnection( FileName, Password ) 
  End If 
  If conn Is Nothing Then Exit Function 

  Set rs = Server.CreateObject("ADODB.Recordset") 
  If Err.Number <> 0 Then Exit Function 

  rs.Open source, conn, Cursor, 2 
  If Err.Number <> 0 Then Exit Function 
  Set GetMdbRs = rs 
End Function 

'--------------------------------------------------- 
Function GetOtherRs( DataType, Path, SQL, Cursor ) 
  Dim conn, rs 
  On Error Resume Next 
  Set GetOtherRs = Nothing 

  Select Case DataType 
      Case "Dbf" 
        Set conn = GetDbfConnection( Path ) 
      Case "Dbc" 
        Set conn = GetDbcConnection( Path ) 
      Case "Excel" 
        Set conn = GetExcelConnection( Path ) 
      Case "Text" 
        Set conn = GetTextConnection( Path ) 
  End Select 
  If conn Is Nothing Then Exit Function 

  Set rs = Server.CreateObject("ADODB.Recordset") 
  If Err.Number <> 0 Then Exit Function 

  rs.Open SQL, conn, Cursor, 2 
  If Err.Number <> 0 Then Exit Function 
  Set GetOtherRs = rs 
End Function 

'--------------------------------------------------- 
Function GetSQLServerRs( Computer, UserID, Password, Db, source, Cursor ) 
  Dim conn, rs 

  On Error Resume Next 
  Set GetSQLServerRs = Nothing 
  Set conn = GetSQLServerConnection( Computer, UserID, Password, Db ) 
  If conn Is Nothing Then Exit Function 

  Set rs = Server.CreateObject("ADODB.Recordset") 
  If Err.Number <> 0 Then Exit Function 

  rs.Open source, conn, Cursor, 2 
  If Err.Number <> 0 Then Exit Function 
  Set GetSQLServerRs = rs 
End Function 
%> 
使用方法是——复制下来存成一个文件,然后用#Include “文件名”就可以调用里面的子程序了。 
有什么问题可以一起探讨!!!                                  

随机记录的取得,适用于sql数据库中
Select * FROM table orDER BY NEWID()

[推荐]删除全部数据的最快捷的方法
TRUNCATE TABLE
删除表中的所有行,而不记录单个行删除操作。 

语法
TRUNCATE TABLE name

参数
name

是要截断的表的名称或要删除其全部行的表的名称。

注释
TRUNCATE TABLE 在功能上与不带 Where 子句的 Delete 语句相同:二者均删除表中的全部行。但 TRUNCATE TABLE 比 Delete 速度快,且使用的系统和事务日志资源少。 

Delete 语句每次删除一行,并在事务日志中为所删除的每行记录一项。TRUNCATE TABLE 通过释放存储表数据所用的数据页来删除数据,并且只在事务日志中记录页的释放。

TRUNCATE TABLE 删除表中的所有行,但表结构及其列、约束、索引等保持不变。新行标识所用的计数值重置为该列的种子。如果想保留标识计数值,请改用 Delete。如果要删除表定义及其数据,请使用 Drop TABLE 语句。

对于由 FOREIGN KEY 约束引用的表,不能使用 TRUNCATE TABLE,而应使用不带 Where 子句的 Delete 语句。由于 TRUNCATE TABLE 不记录在日志中,所以它不能激活触发器。 

TRUNCATE TABLE 不能用于参与了索引视图的表。

示例
下例删除 authors 表中的所有数据。

TRUNCATE TABLE authors

袖珍数据库连接查询手册
下面将简单介绍一下几种ADO连接方式:ODBC DSN,ODBC DSN-Less, 
OLE DB Provider,和"MS Remote" Provider.
1。ODBC DSN连接
I.DSN
    oConn.Open "DSN=AdvWorks;" & _ 
        "UID=Admin;" & _ 
        "PWD=;"
注意:从MDAC2.1开始就不能够在使用这样的方式了,就是只把DSN文件名放在ConnectString中
      你必须同时使用DSN,UID,PWD标志。例如下面的方式在MDAC 2.1中将会出错:
    oConn.Open "AdvWorks"

II.File DSN
    oConn.Open "FILEDSN=\somepath\mydb.dsn;" & _ 
        "UID=Admin;" & _
        "PWD=;"
III.ODBC DSN-Less Connections
a)ODBC Text Driver
    oConn.Open _
        "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
        "Dbq=\somepath\;" & _
        "Extensions=asc,csv,tab,txt;" & _
        "Persist Security Info=False"
注意:需要在SQL语句中指定使用到的文件名。例如:
    ors.Open "Select * From customer.csv", _
        oConn, adOpenStatic, adLockReadOnly, adCmdText
b)ODBC Driver for Access
    i)普通安全模式:
        oConn.Open _
            "Driver={Microsoft Access Driver (*.mdb)};" & _ 
            "Dbq=\somepath\mydb.mdb;" & _
            "Uid=Admin;" & _
            "Pwd=;"
    ii)如果使用了System database:
        oConn.Open _
            "Driver={Microsoft Access Driver (*.mdb)};" & _ 
            "Dbq=\somepath\mydb.mdb;" & _
            "SystemDB=\somepath\mydb.mdw;", _
            "admin", ""
c)ODBC Driver for SQL Server
    i)普通安全模式
        oConn.Open "Driver={SQL Server};" & _ 
            "Server=carl2;" & _
            "Database=pubs;" & _
            "Uid=sa;" & _
            "Pwd=;"
    ii)使用信任安全模式:
        oConn.Open "Driver={SQL Server};" & _ 
            "Server=carl2;" & _
            "Database=pubs;" & _
            "Uid=;" & _
            "Pwd=;"
    注意:要使用空白的Uid和Pwd           
d)ODBC Driver for oracle
    i)使用现有的Oracle ODBC Driver from Microsoft:
        oConn.Open _
            "Driver={Microsoft ODBC for oracle};" & _
            "Server=OracleServer.world;" & _
            "Uid=demo;" & _
            "Pwd=demo;"
    ii)使用老版本的Oracle ODBC Driver from Microsoft:
        oConn.Open _
            "Driver={Microsoft ODBC Driver for oracle};" & _
            "ConnectString=OracleServer.world;" & _
            "Uid=demo;" & _
            "Pwd=demo;"
IIII)使用微软的OLE DB Data Link Connections方式Data Link File (UDL)
a)使用绝对路径
    oConn.Open "File Name=\somepath\pubs.udl;" 
b)使用相对路径
    oConn.Open "File Name=pubs.udl;" 
V)OLE DB Provider Connections方式
a)OLE DB Provider for ODBC Databases
    i)Access (Jet):
        oConn.Open _
            "Provider=MSDASQL;" & _ 
            "Driver={Microsoft Access Driver (*.mdb)};" & _
            "Dbq=\somepath\mydb.mdb;" & _
            "Uid=Admin;" & _
            "Pwd=;"
    ii)SQL Server:
        oConn.Open _
            "Provider=MSDASQL;" & _  
            "Driver={SQL Server};" & _
            "Server=carl2;" & _
            "Database=pubs;" & _
            "Uid=sa;" & _
            "Pwd=;"
b)OLE DB Provider for Microsoft Jet  (Access)
    i)普通安全模式:
        oConn.Open _
            "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=\somepath\mydb.mdb;" & _ 
            "User Id=admin;" & _
            "Password=;"
    ii)如果使用了System database:
        oConn.Open _
            "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=\somepath\mydb.mdb;" & _ 
            "Jet OLEDB:System Database=system.mdw;", _
            "admin", "" 
    注意:当使用OLE DB Provider4.0版本是,需要把MDB和MDW文件转换成4.0的数据库格式
    iii)如果MDB需要一个数据库密码的话:
        oConn.Open _
            "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=\somepath\mydb.mdb;" & _ 
            "Jet OLEDB:Database Password=MyDbPassword;", _
            "admin", ""
c)OLE DB Provider for Excel Spreadsheet: 
        oConn.Open _
            "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=\somepath\expenses.xls;" & _
            "Extended Properties=""Excel 8.0;HDR=Yes;"";" 
    注意: "HDR=Yes"表示在第一行中是行标题,在provider中将不把第一行包括入recordset中
d)OLE DB Provider for SQL Server
    i)普通安全模式:
        oConn.Open "Provider=sqloledb;" & _ 
            "Network Library=DBMSSOCN;" & _
            "Data Source=carl2;" & _
            "Initial Catalog=pubs;" & _
            "User Id=sa;" & _
            "Password=;"
    ii)使用信任安全模式:
        oConn.Open "Provider=sqloledb;" & _
            Network Library=DBMSSOCN;" & _
            "Data Source=carl2;" & _
            "Initial Catalog=pubs;" & _
            "Trusted_Connection=yes;"
    注意:"Network Library=DBMSSOCN"声明OLE DB使用TCP/IP替代Named Pipes.
e)OLE DB Provider for oracle
    oConn.Open "Provider=msdaora;" & _
        "Data Source=OracleServer.world;" & _ 
        "User Id=sa;" & _
        "Password=;"
(VI)Remote OLE DB Provider Connections方式(就是我一直在研究的RDS方式哦,呵呵。):
a)MS Remote - Access (Jet)
    i)ODBC DSN:
        oConn.Open "Remote Server=http://carl2;" & _ 
            "Remote Provider=MSDASQL;" & _
            "DSN=AdvWorks;" & _
            "Uid=admin" & _
            "Pwd=;" 
    ii)OLE DB Provider:
        oConn.Open "Provider=MS Remote;" & _ 
            "Remote Server=http://carl2;" & _
            "Remote Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=\somepath\mydb.mdb;", _
            "admin", ""
    iii)OLE DB Provider自定义事务对象
        oConn.Open "Provider=MS Remote;" & _ 
            "Remote Server=http://carl2;" & _
            "Handler=MSDFMAP.Handler;" & _
            "Data Source=MyAdvworksOLEDBConnectTag;"
b)MS Remote - SQL Server
    i)ODBC DSN:
        oConn.Open "Remote Server=http://carl2;" & _ 
            "Remote Provider=MSDASQL;" & _
            "Network Library=DBMSSOCN;" & _
            "DSN=Pubs;" & _
            "Uid=sa" & _
            "Pwd=;" 
    ii)OLE DB Provider
        oConn.Open "Provider=MS Remote;" & _ 
            "Remote Server=http://carl2;" & _
            "Remote Provider=SQLOLEDB;" & _
            "Network Library=DBMSSOCN;" & _
            "Data Source=carl2;" & _
            "Initial Catalog=pubs;" & _
            "User ID=sa;" & _
            "Password=;"