博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 获取SQL Server所有的数据库名称
阅读量:7281 次
发布时间:2019-06-30

本文共 1162 字,大约阅读时间需要 3 分钟。

参考文章:

http://blog.csdn.net/friendan/article/details/52182923

 

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

[csharp]
    1. // ...  
    2.   
    3.   
    4.   
    5.   
    6.   
    7.                 /// <summary>  
    8.         /// 获取数据库连接对象  
    9.         /// </summary>  
    10.         /// <param name="dbName"></param>  
    11.         /// <returns></returns>  
    12.         public SqlConnection getSqlConnection(string dbName)  
    13.         {  
    14.             SqlConnection conn = new SqlConnection();  
    15.             conn.ConnectionString = string.Format("server=127.0.0.1,9527;database={0};uid=sa;pwd=123456;Enlist=true", dbName);  
    16.             conn.Open();  
    17.             //showLog(string.Format("连接数据库成功:{0}", conn.Database));  
    18.             return conn;  
    19.         }  
    20.           
    21.           
    22.          /// <summary>  
    23.          /// 取所有数据库名称  
    24.          /// </summary>  
    25.          /// <returns></returns>  
    26.          public ArrayList  getAllDbName()  
    27.          {  
    28.              ArrayList dbNameList = new ArrayList();  
    29.              DataTable dbNameTable = new DataTable();  
    30.              SqlConnection conn = getSqlConnection("master");  
    31.              SqlDataAdapter adapter = new SqlDataAdapter("select name from master..sysdatabases",  conn);  
    32.              lock (adapter)  
    33.              {  
    34.                  adapter.Fill(dbNameTable);  
    35.              }  
    36.              foreach (DataRow row in dbNameTable.Rows)  
    37.              {  
    38.                  dbNameList.Add(row["name"]);  
    39.              }  
    40.              conn.Close();   
    41.              return dbNameList;  
    42.          }  
    43.           

转载于:https://www.cnblogs.com/LuoEast/p/8493172.html

你可能感兴趣的文章
信息安全的重要性
查看>>
Python datetime模块
查看>>
event 事件div块的拖拽
查看>>
hibernate中evict()和clear()的区别
查看>>
学习web components
查看>>
PHP 将秒数转换成时分秒
查看>>
node.js 模块加载原理
查看>>
一个自定义线程池的小Demo
查看>>
Report_SRW工具的基本用法(概念)
查看>>
dedecms设置文章分页后,标题会带有序号的解决方法
查看>>
MySQL复制
查看>>
模式识别之相似度计量---余弦计算相似度度量关于两句话的相似度
查看>>
Django之ORM相关操作
查看>>
SVN 安装后报不是内部或外部命令
查看>>
win10 + cuda(v9.0) 安装TensorFlow-gpu版
查看>>
上课笔记
查看>>
工具类(为控件设置圆角) - iOS
查看>>
自定义android的tab样式
查看>>
查询 哪张表 有某字段
查看>>
leetcode700
查看>>