首页 > 数据库 >C#获得SQLServer服务器名、数据库名、表名、以及字段名

C#获得SQLServer服务器名、数据库名、表名、以及字段名

时间:2023-01-05 11:34:34浏览次数:44  
标签:string C# SQLServer objConnetion connection connect 表名 new List

using System.Data.SqlClient;

/// <summary>
/// 获取局域网内的所有数据库服务器名称
/// </summary>
/// <returns>服务器名称数组</returns>
public List<string> GetSqlServerNames()
{
DataTable dataSources = SqlClientFactory.Instance.CreateDataSourceEnumerator().GetDataSources();

DataColumn column = dataSources.Columns["InstanceName"];
DataColumn column2 = dataSources.Columns["ServerName"];

DataRowCollection rows = dataSources.Rows;
List<string> Serverlist = new List<string>();
string array = string.Empty;
for (int i = 0; i < rows.Count; i++)
{
string str2 = rows[i][column2] as string;
string str = rows[i][column] as string;
if (((str == null) || (str.Length == 0)) || ("MSSQLSERVER" == str))
{
array = str2;
}
else
{
array = str2 + @"/" + str;
}

Serverlist.Add(array);
}

Serverlist.Sort();

return Serverlist;
}

/// <summary>
/// 查询sql中的非系统库
/// </summary>
/// <param name="connection"></param>
/// <returns></returns>
public List<string> databaseList(string connection)
{
List<string> getCataList = new List<string>();
string cmdStirng = "select name from sys.databases where database_id > 4";
SqlConnection connect = new SqlConnection(connection);
SqlCommand cmd = new SqlCommand(cmdStirng, connect);
try
{
if (connect.State == ConnectionState.Closed)
{
connect.Open();
IDataReader dr = cmd.ExecuteReader();
getCataList.Clear();
while (dr.Read())
{
getCataList.Add(dr["name"].ToString());
}
dr.Close();
}

}
catch (SqlException e)
{
//MessageBox.Show(e.Message);
}
finally
{
if (connect != null && connect.State == ConnectionState.Open)
{
connect.Dispose();
}
}
return getCataList;
}

/// <summary>
/// 获取列名
/// </summary>
/// <param name="connection"></param>
/// <returns></returns>
public List<string> GetTables(string connection)
{
List<string> tablelist = new List<string>();
SqlConnection objConnetion = new SqlConnection(connection);
try
{
if (objConnetion.State == ConnectionState.Closed)
{
objConnetion.Open();
DataTable objTable = objConnetion.GetSchema("Tables");
foreach (DataRow row in objTable.Rows)
{
tablelist.Add(row[2].ToString());
}
}
}
catch
{

}
finally
{
if (objConnetion != null && objConnetion.State == ConnectionState.Closed)
{
objConnetion.Dispose();
}

}
return tablelist;
}

/// <summary>
/// 获取字段
/// </summary>
/// <param name="connection"></param>
/// <param name="TableName"></param>
/// <returns></returns>
public List<string> GetColumnField(string connection, string TableName)
{
List<string> Columnlist = new List<string>();
SqlConnection objConnetion = new SqlConnection(connection);
try
{
if (objConnetion.State == ConnectionState.Closed)
{
objConnetion.Open();
}

SqlCommand cmd = new SqlCommand("Select Name FROM SysColumns Where id=Object_Id('" + TableName + "')", objConnetion);
SqlDataReader objReader = cmd.ExecuteReader();

while (objReader.Read())
{
Columnlist.Add(objReader[0].ToString());

}
}
catch
{

}
objConnetion.Close();
return Columnlist;
}

标签:string,C#,SQLServer,objConnetion,connection,connect,表名,new,List
From: https://blog.51cto.com/kenkao/5989757

相关文章

  • VC中自行构建Stack(堆栈)类
    template<classT>classStack{structLink{T*data;Link*next;Link(T*dat,Link*nxt):data(dat),next(nxt){}}*head;public:Stack():head(0){}~Stack......
  • 运维之tmux 与 screen
    1.前提,我们之前都是在服务器nohup这个命令,让他在后端保持运行,但是那个有点不好,我们现在用tmux来玩1.1原理tmux==nohup2.安装tmuxyuminstall-ytmux3.......
  • C#实现treeview节点上下左右自由移动
    以下是节点移动类NodeMove.cs源码:usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Collections;namespaceNo......
  • C#实现文件二进制存储
    privatestaticBinaryFormatterTransfer=newBinaryFormatter();publicintBinaryFileSav(){//-----二进制文件写入并存储......
  • C#实现TreeView向XML的绝对转换类
    从第一次接触XML开始就想写一个能实现tree和XML灵活转换的类了。写这个类大概用去了将近半天的时间,花的时间有些长了。呵呵。。好在收获颇多,熟练了XML的读写类,对C#中的forea......
  • 用hardhat 做ERC20转帐时报了 Nonce too high. Expected nonce to be 6 but got 11. N
    https://github.com/scaffold-eth/scaffold-eth/issues/476InMetamask,ensureyouareonyourdev/testaccountthen:clickontheavatarcircletoprightInthe......
  • mysql 5.7 增加regexp_replace函数
    CREATEDEFINER=`root`@`%`FUNCTION`regexp_replace`(ori_strLONGTEXT,patternVARCHAR(1000),repl_strVARCHAR(1000))RETURNSlongtextCHARSETutf8mb4DETERMI......
  • React的useLayoutEffect和useEffect执行时机有什么不同
    我们先看下React官方文档对这两个hook的介绍,建立个整体认识useEffect(create,deps):该Hook接收一个包含命令式、且可能有副作用代码的函数。在函数组件主体内(这......
  • React循环DOM时为什么需要添加key
    一、React渲染流程和更新流程react渲染流程:jsx->虚拟dom->真实domreact更新流程:props/state改变->render函数重新执行->生成新的虚拟dom树->新旧虚拟dom树进......
  • 校招前端二面常考react面试题(边面边更)
    高阶组件高阶函数:如果一个函数接受一个或多个函数作为参数或者返回一个函数就可称之为高阶函数。高阶组件:如果一个函数接受一个或多个组件作为参数并且返回一个组件就......