利用SQLDMO操控SQL Server
在开发基于SQL Server数据库的应用程序时,经常会遇到需要知道SQL Server数据库对象的相关信息的情况,例如SQL Server中有哪些数据库,这些数据库中有哪些数据表,每个数据表有哪些字段,这些字段是什么属性等。利用SQL-DMO(分布式管理对象)来访问SQL Server数据库。SQL-DMO是为Windows 9x与 Windows NT提供的32位OLE对象,它为与OLE兼容的应用程序提供了SQL Server管理功能的接口,开发者通过DMO就可以直接访问SQL Server所有的数据对象。
不知道大家注意过没有,有好多代码生成器在操作数据库的时候就是利用SQLDMO对象来显示数据库的(包括数据库服务器、数据库、表、视图、存储过程)。但是我上次写的那个简单的代码生成器却没有利用这个对象,我是直接利用SQL语句来操作的,大家不妨在看看那个“代码生成器”。好了,废话少说,下面就介绍一下怎么利用SQLDMO操作数据的。
一、获取局域网数据库服务器
首先要定义一个SQLDMO.Application 对象
private SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass();
然后再定义一个SQLDMO.NameList 对象
SQLDMO.NameList sqlServers=null;
cboServers是下列列表框的ID,下面是将数据库服务器显示在下拉列表框中的代码:
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);
}
}
二、获取服务器中的数据库列表
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);
}
this.cboDatabase.Sorted = true;
if(this.cboDatabase.Items.Count >0)
{
this.cboDatabase.SelectedIndex = 0;
this.cboDatabase.Enabled = true;
this.groupBox1.Enabled = true;
}
else
{
this.groupBox1.Enabled = false;
this.cboDatabase.Enabled = false;
this.cboDatabase.Text = "<No databases found>";
}
三、获取数据库中的表
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.Tables.Count;j++)
{
this.lstObjects.Items.Add(db.Tables.Item(j+1,"dbo").Name);
}
this.Cursor = Cursors.Default;
return;
}
}
四、获取数据库中的存储过程
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);
}
this.Cursor = Cursors.Default;
return;
}
}
五、获取数据库中的视图
SQLDMO.SQLServer srv = new SQLDMO.SQLServerClass();
srv.Connect(this.cboServers.SelectedText,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.Views.Count;j++)
{
this.lstObjects.Items.Add(db.Views.Item(j+1,"dbo").Name);
}
this.Cursor = Cursors.Default;
return;
}
}
这些都是最基本的操作,有了这些大家还可以操作表中的字段、数据类型、字段长度等信息。利用这些信息和.NET框架中的CodeDOM大家就可以轻而易举实现自己的代码生成器了。怎么样,简单吧,哈哈!
不知道大家注意过没有,有好多代码生成器在操作数据库的时候就是利用SQLDMO对象来显示数据库的(包括数据库服务器、数据库、表、视图、存储过程)。但是我上次写的那个简单的代码生成器却没有利用这个对象,我是直接利用SQL语句来操作的,大家不妨在看看那个“代码生成器”。好了,废话少说,下面就介绍一下怎么利用SQLDMO操作数据的。
一、获取局域网数据库服务器
首先要定义一个SQLDMO.Application 对象
private SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass();
然后再定义一个SQLDMO.NameList 对象
SQLDMO.NameList sqlServers=null;
cboServers是下列列表框的ID,下面是将数据库服务器显示在下拉列表框中的代码:









二、获取服务器中的数据库列表





















三、获取数据库中的表
















四、获取数据库中的存储过程

















五、获取数据库中的视图
















这些都是最基本的操作,有了这些大家还可以操作表中的字段、数据类型、字段长度等信息。利用这些信息和.NET框架中的CodeDOM大家就可以轻而易举实现自己的代码生成器了。怎么样,简单吧,哈哈!
分类:
SQL Server 2000
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战