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

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Schneier on Security
The Last Watchdog
The Last Watchdog
Cyberwarzone
Cyberwarzone
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cyber Attacks, Cyber Crime and Cyber Security
L
Lohrmann on Cybersecurity
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
The Cloudflare Blog
V
V2EX
博客园_首页
博客园 - 聂微东
Vercel News
Vercel News
人人都是产品经理
人人都是产品经理
G
GRAHAM CLULEY
T
Tenable Blog
Last Week in AI
Last Week in AI
Y
Y Combinator Blog
L
LINUX DO - 最新话题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
SecWiki News
SecWiki News
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
T
Troy Hunt's Blog
博客园 - 【当耐特】
Forbes - Security
Forbes - Security
H
Hacker News: Front Page
A
About on SuperTechFans
B
Blog RSS Feed
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
D
DataBreaches.Net
P
Privacy & Cybersecurity Law Blog
Schneier on Security
Schneier on Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Jina AI
Jina AI
D
Docker
P
Proofpoint News Feed

博客园 - Brendan

[转载]Manually configuring Microsoft Internet Information Services (IIS) IIS与TOMCAT协同工作---在IIS下运行JSP页面 AXIS部署错误解决方案集锦 使用System.Web.Mail发送Mail的错误解决方案 类的XML序列化(XML Serialization) [翻译]你可以赚钱,但你不能赚时间 Windows 2003 Server配置IIS服务器(ASP, ASP.NET)全功略 Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive 用MS SQL Server事件探查器来跟踪数据库的操作 反反编译工具——Deploy.NET C中获取当前时间的函数 Buffered I/O 与 Non-Buffered I/O性能差异的实例体验 替换函数(Substitution Function) 主定理(Master Theorem) Dynamic Programming之Longest Increasing Subsequence (LIS)问题 ADO.NET数据库连接模块 ASP.NET控件事件丢失的探究 关联(Association)设计中的扇形陷阱(Fan Traps)和断层陷阱(Chasm Traps) 简单并发控制
SQLDMO For C#(翻译)
Brendan · 2006-04-06 · via 博客园 - Brendan

By Kevin Goss
转自
http://www.csharphelp.com/archives2/archive342.html

很多时候,我需要在我的程序中得到SQL Server的详细信息。直到最近我还在使用API调用和假冒的ADO调用来获得我需要的信息。现在,我们有了SQLDMO(SQL Distributed Management Objects)。尽管没有被广泛的知晓和使用,SQLDMO提供了一个非常强大的功能集来用代码实现SQL Server所有能作的事。我举这个例子,是为了展示如何在你本地网络上取得SQL Server列表,如何连接一个Server,和如何从服务器得到表、存储过程和视图的列表。

SQLDMO对象来自与SQL Server 2000一同发售的SQLDMO.dll。这个dll本身是一个COM对象,而你必须把它引用到你的.net项目中。IDE将会为使用这个库创建必需的COM包装(wrapper)。注意:如果你在你的程序中使用“using SQLDMO”声明,你会得到一个错误。


(你必须为你的样例程序正常工作重引用这个COM对象)

在引用了这个COM对象之后,你可以开始非常简单的使用它。

所有在例子中出现的操作使用了一个或多个如下对象:

  • SQLDMO.Application
  • SQLDMO.SQLServer
  • SQLDMO.Database
  • SQLDMO.NameList

有很多可用的对象来完成诸如备份和还原的操作,但为了本文的目的,我打算保持简单使你能很容易的对SQLDMO入门。

列出你在网络的所有SQL Servers是非常容易的。首先,你需要引用SQLDMO.Application对象。然后你把一个SQLDMO.NameList的实例设为SQLDMO.Application.ListAvailableSQLServers()方法的返回值。SQLDMO.NameList是一个服务器名的COM集合。

记住,在你习惯之前,调用COM对象有点让人胆战心惊,但是它们之间的协定(convention)是非常相似的。这里有一段示例代码,它把本地网络上可用的SQL Servers列表填入一个叫cboServers的combo box中:

//get all available SQL Servers     
SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass(); 
SQLDMO.NameList sqlServers 
= sqlApp.ListAvailableSQLServers(); 
for(int i=0;i<sqlServers.Count;i++

    
object srv = sqlServers.Item(i + 1); 
    
if(srv != null
    

        
this.cboServers.Items.Add(srv);                         
    }
 
}
 
if(this.cboServers.Items.Count > 0
    
this.cboServers.SelectedIndex = 0
else 
    
this.cboServers.Text = "<No available SQL Servers>";


就像你看到得那样,这是非常简单得。只要记住COM的集合从指针1开始,不是0。 

连接到一个服务器然后得到一个数据库的列表也同样非常简单。接下来的代码将用combo box中选中的SQL Server,连上它(用text box中的用户名和密码),然后,另一个combo box就灌入了那个服务器上的数据库列表。

//get all available databases from an SQL Server 
SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass(); 
SQLDMO.SQLServer srv 
= new SQLDMO.SQLServerClass();                 
srv.Connect(
this.cboServers.SelectedItem.ToString(),this.txtUser.Text,this.txtPassword.Text); 
foreach(SQLDMO.Database db in srv.Databases) 

    
if(db.Name!=null
        
this.cboDatabase.Items.Add(db.Name); 
}


使用这个库得到类型对象列表也同样是轻而易举的事。你再一次建立一个数据库连接,然后你循环通过这个对象集合。

//Get all Stored procedures - tables are in the Tables collection, views are in the Views collection 
SQLDMO.SQLServer srv = new SQLDMO.SQLServerClass();                 
srv.Connect(
this.cboServers.SelectedItem.ToString(),this.txtUser.Text,this.txtPassword.Text); 
for(int i=0;i<srv.Databases.Count;i++

    
if(srv.Databases.Item(i+1,"dbo").Name == this.cboDatabase.SelectedItem.ToString()) 
    

        SQLDMO._Database db
= srv.Databases.Item(i+1,"dbo"); 
        
this.lstObjects.Items.Clear(); 
        
for(int j=0;j<db.StoredProcedures.Count;j++
        

            
this.lstObjects.Items.Add(db.StoredProcedures.Item(j+1,"dbo").Name); 
        }
 
        
break
    }
 
}


好了伙计,这就是我的SQLDMO入门指南。请立即下载样例代码和程序来看。就像你看到的,当你需要SQL信息和控制时,这是一种更方便的选择。编程愉快!