关于C#中使用SQLDMO来获取数据库中的一些操作
首先添加SQLDMO的引用
?
1:通過SQLDMO來獲取可用的網(wǎng)絡(luò)數(shù)據(jù)庫(區(qū)域網(wǎng)當(dāng)中)
public static List<string> get可用SQL服務(wù)器()
????? {
????????? SQLDMO.Application app = new SQLDMO.Application();
????????? SQLDMO.NameList lst可用SQLServer服務(wù)器 = app.ListAvailableSQLServers();
????????? List<string> lst = new List<string>();
????????? for (int i = 1; i <= lst可用SQLServer服務(wù)器.Count; i++)
????????? {
????????????? string str = lst可用SQLServer服務(wù)器.Item(i);
????????????? lst.Add(str);
????????? }
????????? return lst;
????? }
?
2.使用SQLDMO來獲取數(shù)據(jù)庫當(dāng)中的所有表
public static List<string> get數(shù)據(jù)庫所有表(string strServerName, string strDatabase, string strUserID, string strPwd)
???? {
???????? SQLDMO.SQLServer Server = new SQLDMO.SQLServerClass();
???????? List<string> lst = null;
???????? //連接到服務(wù)器
???????? try
???????? {
???????????? Server.Connect(strServerName, strUserID, strPwd);
???????????? //對所有的數(shù)據(jù)庫遍歷,獲得指定數(shù)據(jù)庫
???????????? for (int i = 0; i < Server.Databases.Count; i++)
???????????? {
???????????????? //判斷當(dāng)前數(shù)據(jù)庫是否是指定數(shù)據(jù)庫
???????????????? if (Server.Databases.Item(i + 1).Name == strDatabase)
???????????????? {
???????????????????? lst = new List<string>();
???????????????????? //獲得指定數(shù)據(jù)庫
???????????????????? SQLDMO._Database db = Server.Databases.Item(i + 1);
???????????????????? //獲得指定數(shù)據(jù)庫中的所有表
???????????????????? for (int j = 0; j < db.Tables.Count; j++)
???????????????????? {
???????????????????????? lst.Add(db.Tables.Item(j + 1).Name);
???????????????????? }
???????????????? }
???????????? }
???????? }
???????? catch
???????? {
???????????? MessageBox.Show("獲取失敗");
???????? }
???????? return lst;
???? }
3.使用SQLDMO來獲取數(shù)據(jù)庫當(dāng)中的指定表的所有列
public static List<string> get數(shù)據(jù)庫表中列(string strServerName, string strDatabase, string strUserID, string strPwd,string str表名)
?????? {
?????????? SQLDMO.SQLServer Server = new SQLDMO.SQLServerClass();
?????????? List<string> lst = null;
?????????? //連接到服務(wù)器
?????????? try
?????????? {
?????????????? Server.Connect(strServerName, strUserID, strPwd);
?????????????? //對所有的數(shù)據(jù)庫遍歷,獲得指定數(shù)據(jù)庫
?????????????? for (int i = 0; i < Server.Databases.Count; i++)
?????????????? {
?????????????????? //判斷當(dāng)前數(shù)據(jù)庫是否是指定數(shù)據(jù)庫
?????????????????? if (Server.Databases.Item(i + 1).Name == strDatabase)
?????????????????? {
?????????????????????? lst = new List<string>();
?????????????????????? //獲得指定數(shù)據(jù)庫
?????????????????????? SQLDMO._Database db = Server.Databases.Item(i + 1);
?????????????????????? //獲得指定數(shù)據(jù)庫中的所有表
?????????????????????? for (int j = 0; j < db.Tables.Count; j++)
?????????????????????? {
?????????????????????????? //lst.Add(db.Tables.Item(j + 1).Name);
?????????????????????????? if (db.Tables.Item(j + 1).Name == str表名)
?????????????????????????? {
?????????????????????????????? foreach (SQLDMO._Column item in db.Tables.Item(j + 1).Columns)
?????????????????????????????? {
?????????????????????????????????? lst.Add(item.Name);
?????????????????????????????? }
?????????????????????????? }
?????????????????????? }
?????????????????? }
?????????????? }
?????????? }
?????????? catch
?????????? {
?????????????? MessageBox.Show("獲取失敗");
?????????? }
?????????? return lst;
?????? }
轉(zhuǎn)載于:https://www.cnblogs.com/fx_guo/archive/2010/12/21/1912317.html
總結(jié)
以上是生活随笔為你收集整理的关于C#中使用SQLDMO来获取数据库中的一些操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我们学校也在使用IPV6
- 下一篇: jquery插件 autoComboBo