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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#_获取 SQL服务器列表

發布時間:2025/3/20 C# 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#_获取 SQL服务器列表 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

#region 得到所有本地網絡中可使用的SQL服務器列表
///<summary>
/// 得到所有本地網絡中可使用的SQL服務器列表
///</summary>
///<param name="p_strServerList">服務器列表</param>
///<returns></returns>
public static bool GetServers(ref string [] p_strServerList)
{
try
{
SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass();
SQLDMO.NameList sqlServers = sqlApp.ListAvailableSQLServers();
if(sqlServers.Count > 0)
{
p_strServerList = new string[sqlServers.Count];
for(int i=0;i<sqlServers.Count;i++)
{
string srv = sqlServers.Item(i + 1);
if(srv != null)
{
p_strServerList[i] = srv;
}
}
}
return true;
}
catch(Exception ex)
{
throw ex;
}
}

#endregion

#region 得到指定SQL服務器所有數據庫的列表
///<summary>
/// 得到指定SQL服務器所有數據庫的列表
///</summary>
///<param name="p_strDataBaseList">數據庫列表</param>
///<param name="p_strServer">服務器名</param>
///<param name="p_strUser">用戶名</param>
///<param name="p_strPWD">密碼</param>
///<returns></returns>
public static bool GetDataBases(ref string [] p_strDataBaseList, string p_strServer, string p_strUser, string p_strPWD)
{
try
{
int i = 0;

SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass();
SQLDMO.SQLServer srv = new SQLDMO.SQLServerClass();
srv.Connect(p_strServer,p_strUser,p_strPWD);

if(srv.Databases.Count > 0)
{
p_strDataBaseList = new string[srv.Databases.Count];

foreach(SQLDMO.Database db in srv.Databases)
{
if(db.Name!=null)
{
p_strDataBaseList[i] = db.Name;
}
i = i + 1;
}
}
return true;
}
catch(Exception ex)
{
throw ex;
}
}

#endregion

#region 得到所有的存儲過程
///<summary>
/// 得到所有的存儲過程
///</summary>
///<param name="p_strProcedureList">存儲過程列表</param>
///<param name="p_strServer">服務器名</param>
///<param name="p_strUser">用戶名</param>
///<param name="p_strPWD">密碼</param>
///<param name="p_strDataBase">數據庫名</param>
///<returns></returns>
public static bool GetProcedures(ref string [] p_strProcedureList, string p_strServer, string p_strUser, string p_strPWD, string p_strDataBase)
{
try
{
SQLDMO.SQLServer srv = new SQLDMO.SQLServerClass();
srv.Connect(p_strServer,p_strUser,p_strPWD);

for(int i=0;i<srv.Databases.Count;i++)
{
if(srv.Databases.Item(i+1,"dbo").Name == p_strDataBase)
{
SQLDMO._Database db= srv.Databases.Item(i+1,"dbo");
if (db.StoredProcedures.Count > 0)
{
p_strProcedureList = new string[db.StoredProcedures.Count];

for(int j=0;j<db.StoredProcedures.Count;j++)
{
p_strProcedureList[j] = db.StoredProcedures.Item(j+1,"dbo").Name;
}
break;
}
}
}

return true;
}
catch(Exception ex)
{
throw ex;
}
}
#endregion

#region 得到所有的Tables集合
///<summary>
/// 得到所有的Tables集合
///</summary>
///<param name="p_strProcedureList">Tables集合</param>
///<param name="p_strServer">服務器名</param>
///<param name="p_strUser">用戶名</param>
///<param name="p_strPWD">密碼</param>
///<param name="p_strDataBase">數據庫名</param>
///<returns></returns>
public static bool GetTables(ref string [] p_strTableList, string p_strServer, string p_strUser, string p_strPWD, string p_strDataBase)
{
try
{
SQLDMO.SQLServer srv = new SQLDMO.SQLServerClass();
srv.Connect(p_strServer,p_strUser,p_strPWD);

for(int i=0;i<srv.Databases.Count;i++)
{
if(srv.Databases.Item(i+1,"dbo").Name == p_strDataBase)
{
SQLDMO._Database db= srv.Databases.Item(i+1,"dbo");
if (db.Tables.Count > 0)
{
p_strTableList = new string[db.Tables.Count];

for(int j=0;j<db.Tables.Count;j++)
{
p_strTableList[j] = db.Tables.Item(j+1,"dbo").Name;
}
break;
}
}
}

return true;
}
catch(Exception ex)
{
throw ex;
}
}
#endregion

#region 得到所有的Views集合
///<summary>
/// 得到所有的Views集合
///</summary>
///<param name="p_strProcedureList">Views集合</param>
///<param name="p_strServer">服務器名</param>
///<param name="p_strUser">用戶名</param>
///<param name="p_strPWD">密碼</param>
///<param name="p_strDataBase">數據庫名</param>
///<returns></returns>
public static bool GetViews(ref string [] p_strViewList, string p_strServer, string p_strUser, string p_strPWD, string p_strDataBase)
{
try
{
SQLDMO.SQLServer srv = new SQLDMO.SQLServerClass();
srv.Connect(p_strServer,p_strUser,p_strPWD);

for(int i=0;i<srv.Databases.Count;i++)
{
if(srv.Databases.Item(i+1,"dbo").Name == p_strDataBase)
{
SQLDMO._Database db= srv.Databases.Item(i+1,"dbo");
if (db.Views.Count > 0)
{
p_strViewList = new string[db.Views.Count];

for(int j=0;j<db.Views.Count;j++)
{
p_strViewList[j] = db.Views.Item(j+1,"dbo").Name;
}
break;
}
}
}

return true;
}
catch(Exception ex)
{
throw ex;
}
}
#endregion


轉載于:https://www.cnblogs.com/szytwo/archive/2011/09/20/2263742.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的C#_获取 SQL服务器列表的全部內容,希望文章能夠幫你解決所遇到的問題。

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