17

我正在通过文本框输入源名称用户 ID 和密码,并希望数据库列表应列在组合框中,以便用户可以选择所有四个选项源名称、用户 ID、密码和数据库名称来执行连接

数据库将根据用户从其他系统中检索。用户将输入 IP、用户 ID 和密码,他们应该在组合框中获得数据库列表,以便他们可以选择所需的数据库并执行连接

private void frmConfig_Load(object sender, EventArgs e)
{
    try
    {
        string Conn = "server=servername;User Id=userid;" + "pwd=******;";
        con = new SqlConnection(Conn);
        con.Open();

        da = new SqlDataAdapter("SELECT * FROM sys.database", con);
        cbSrc.Items.Add(da);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

我正在尝试这样做,但它没有生成任何数据

4

7 回答 7

38

系统数据库

SELECT name
FROM sys.databases;

编辑:

我建议使用 IDataReader,返回一个列表并缓存结果。您可以简单地将下拉列表绑定到结果,并在需要时从缓存中检索相同的列表。

public List<string> GetDatabaseList()
{
    List<string> list = new List<string>();

    // Open connection to the database
    string conString = "server=xeon;uid=sa;pwd=manager; database=northwind";

    using (SqlConnection con = new SqlConnection(conString))
    {
        con.Open();

        // Set up a command with the given query and associate
        // this with the current connection.
        using (SqlCommand cmd = new SqlCommand("SELECT name from sys.databases", con))
        {
            using (IDataReader dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    list.Add(dr[0].ToString());
                }
            }
        }
    }
    return list;

}
于 2012-12-04T12:50:41.460 回答
11

首先添加以下程序集:

  • Microsoft.SqlServer.ConnectionInfo.dll
  • Microsoft.SqlServer.Management.Sdk.Sfc.dll
  • Microsoft.SqlServer.Smo.dll

C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies\

然后使用下面的代码:

var server = new Microsoft.SqlServer.Management.Smo.Server("Server name");

foreach (Database db in server.Databases) {
    cboDBs.Items.Add(db.Name);
}
于 2012-12-04T13:01:56.060 回答
3

您可以使用以下查询之一:

  • EXEC sp_databases
  • SELECT * FROM sys.databases

哔叽

于 2012-12-04T12:52:24.880 回答
1

简单的使用GetSchema方法:

using (SqlConnection connection = GetConnection())
{
    connection.Open();
    DataTable dtDatabases = connection.GetSchema("databases");

    //Get database name using dtDatabases["database_name"]
}
于 2018-04-16T08:40:42.290 回答
0
    using (var connection = new System.Data.SqlClient.SqlConnection("ConnectionString"))
    {
        connection.Open();
        var command = new System.Data.SqlClient.SqlCommand();
        command.Connection = connection;
        command.CommandType = CommandType.Text;
        command.CommandText = "SELECT name FROM master.sys.databases";

        var adapter = new System.Data.SqlClient.SqlDataAdapter(command);
        var dataset = new DataSet();
        adapter.Fill(dataset);
        DataTable dtDatabases = dataset.Tables[0];
    }
于 2014-05-07T13:59:57.513 回答
0

尝试这个:

SqlConnection con = new SqlConnection(YourConnectionString);
SqlCommand cmd = new SqlCommand("SELECT name from sys.databases", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
    cbSrc.Items.Add(dr[0].ToString());
}
con.Close();

或这个:

DataSet ds = new DataSet();
SqlDataAdapter sqlda = new SqlDataAdapter("SELECT name from sys.databases", YourConnectionString);
sqlda.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
    cbSrc.Items.Add(ds.Tables[0].Rows[i][0].ToString());
}
于 2021-09-19T11:07:30.550 回答
0
   How to get list of all database from sql server in a combobox using c# asp.net windows application  
     try
        {
            string Conn = "server=.;User Id=sa;" + "pwd=passs;";
            SqlConnection con = new SqlConnection(Conn);
            con.Open();

            SqlCommand cmd = new SqlCommand();
         //   da = new SqlDataAdapter("SELECT * FROM sys.database", con);
            cmd = new SqlCommand("SELECT name FROM sys.databases", con);
           // comboBox1.Items.Add(cmd);
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    //comboBox2.Items.Add(dr[0]);
                    comboBox1.Items.Add(dr[0]);
                }
            }

           // .Items.Add(da);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
于 2020-01-24T12:54:44.687 回答