日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

dot net操作sql服务器大全

發布時間:2024/4/11 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 dot net操作sql服务器大全 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
李洪根
SQLDMO(SQL Distributed Management Objects,SQL分布式管理對象)封裝 Microsoft SQL Server 2000 數據庫中的對象。SQL-DMO 允許用支持自動化或 COM 的語言編寫應用程序,以管理 SQL Server 安裝的所有部分。SQL-DMO 是 SQL Server 2000 中的 SQL Server 企業管理器所使用的應用程序接口 (API);因此使用 SQL-DMO 的應用程序可以執行 SQL Server 企業管理器執行的所有功能。 SQL-DMO 用于必須包含 SQL Server 管理的任何自動化或 COM 應用程序,例如: 1.封裝 SQL Server 作為自己的數據存儲并想盡量減少用戶的 SQL Server 管理任務的應用程序。 2.在程序本身并入了專門的管理邏輯的應用程序。 3.想在自己的用戶界面中集成 SQL Server 管理任務的應用程序。 SQLDMO對象來自SQLDMO.dll,SQLDMO.dll是隨SQL Server2000一起發布的。SQLDMO.dll自身是一個COM對象,因此,在你的.NET項目里必須先引用它。 得到網絡中的SQL服務器的列表: cbDatabase為一下拉列表框 '得到SQL服務器的列表'必須安裝SQL SERVER 2000 SP2 及以上版本Dim I As ShortDim sqlApp As New SQLDMO.Application()Dim ServerName As SQLDMO.NameListServerName = sqlApp.ListAvailableSQLServersFor i = 1 To ServerName.CountcbServer.Items.Add(ServerName.Item(i))Next 得到指定SQL服務器所有數據庫的列表: '得到指定SQL服務器所有數據庫的列表Dim sqlApp As New SQLDMO.Application()Dim oServer As New SQLDMO.SQLServer()oServer.Connect("(local)", "sa", "sa")cbDatabase.Items.Clear()Dim db As SQLDMO.DatabaseFor Each db In oServer.DatabasesMe.cbDatabase.Items.Add(db.Name)Next 得到所有的表、視圖、存儲過程: Dim I As ShortDim oServer As New SQLDMO.SQLServer()oServer.Connect("(local)", "sa", "sa")Dim db As New SQLDMO.Database()For I = 1 To oServer.Databases.CountIf oServer.Databases.Item(I, "dbo").Name = "Northwind" Then Exit ForNextIf I > oServer.Databases.Count Then Exit Subdb = oServer.Databases.Item(I, "dbo")ListBox1.Items.Clear()'得到所有的存儲過程For I = 1 To db.StoredProcedures.CountListBox1.Items.Add(db.StoredProcedures.Item(I, "dbo").Name)Next'得到所有的表For I = 1 To db.Tables.CountListBox1.Items.Add(db.Tables.Item(I, "dbo").Name)Next' 得到所有的視圖For I = 1 To db.Views.CountListBox1.Items.Add(db.Views.Item(I, "dbo").Name)Next 利用SQLDMO實現帶進度條的數據庫備份: '?添加進度條ProgressBar1控件 '?引用Microsoft SQLDMO Object Library '聲明 Public WithEvents bkps As SQLDMO.Backup'數據庫備份操作 Private Sub btnBackUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackUp.ClickDim oSQLServer As New SQLDMO.SQLServer()oSQLServer.LoginSecure = FalseoSQLServer.Connect("(local)", "sa", "sa") '連接服務器 Me.Cursor = Windows.Forms.Cursors.WaitCursorbkps = CreateObject("SQLDMO.Backup")bkps.Database = "Northwind" '指定需備份的數據庫 bkps.Action = 0bkps.Files = "f:\Northwind.bak" '指定備份文件 bkps.Initialize = TrueProgressBar1.Value = 0ProgressBar1.Maximum = 100Me.Cursor = Windows.Forms.Cursors.Default()Application.DoEvents()Dim mouseCur As CursorMe.Cursor = Windows.Forms.Cursors.WaitCursorbkps.SQLBackup(oSQLServer)ProgressBar1.Value = 100Application.DoEvents()bkps = NothingMe.Cursor = Windows.Forms.Cursors.Default()MsgBox("數據庫備份完成", MsgBoxStyle.Information, "系統消息")End Sub'顯示進度 Private Sub bkps_PercentComplete(ByVal Message As String, ByVal Percent As Integer) Handles bkps.PercentCompleteProgressBar1.Value = ProgressBar1.Maximum * (Percent / 100)End Sub

SQLDMO For C#
By Kevin Goss

Download SQLDMO.zip

Many times I have had a need to get at SQL Server details in my applications.? Until recently I had to use API calls and bastardized ADO calls to get the information I needed.? Now we have SQLDMO? (SQL Distributed Management Objects) .? Although not widely known or used, SQLDMO provides a very powerful set of functionality to do just about anything with an SQL Server from code.? For the purposes of this example I will show how to retrieve a list of SQL Servers on your local network, how to connect to one, and how to retrieve a list of tables, stored procedures, or views from a server.?

The?SQLDMO object?comes from the SQLDMO.dll that ships with SQL Server 2000.? The dll itself is a COM object and you must reference it from your .net project as such.? The IDE will create the necessary COM wrappers needed to use the library.? NOTE: IF YOU USE THE STATEMENT "using SQLDMO;" IN YOUR APP YOU MAY GET AN ERROR.

(YOU MUST RE-REFERENCE THE COM OBJECT FOR THE SAMPLE APP TO WORK)

After referencing the COM object, you can begin using?it quite easily.?

All of the operations performed in the example use one or more of the following objects:

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

There are a multitude of objects available for actions such as backups and restores, but for the purpose of this article I decided to keep it simple to ease you into the world of SQLDMO.

Listing the available SQL Servers on your network is quite simple.? First you need a references SQLDMO.Application object.? Next you set an instance of SQLDMO.NameList to the return value of the SQLDMO.Application.ListAvailableSQLServers()?method.? The SQLDMO.NameList if a COM collection of the server names.??

Keep in mind, calling COM objects is a little funky until you get used to it, but the conventions are similar with all of them.? Here is example code which fills a combo box name cboServers with a list of all available SQL Servers on the local network:

//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>";

As you can see, this is quite simple.? Just remember that COM collections start at an index of 1, not 0.?

Connecting to a server and getting a list of databases is also fairly simple.? The following code will take the chosen SQL Server in the combo box, connect to it (with a user name and password in 2 text boxes), and then poulates another combo box with a list of databases on the server.

//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);
}

Getting a list of objects by type is also a breeze with this library.? Again, you make a connection to the database, and then you loop through the object collection.?

//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;
????}
}

Well folks, that is it for my SQLDMO beginners' tutorial.? Please download the sample code and app to see it in action.? As you can see, this is a much easier alternative when SQL information or control is needed.? Happy coding!!!

轉載于:https://www.cnblogs.com/xioxu/archive/2008/05/06/1185512.html

總結

以上是生活随笔為你收集整理的dot net操作sql服务器大全的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。