C#获取指定数据库服务器下某用户可访问的数据库列表
代码如下:
SqlConnection conn = new SqlConnection("server=数据库服务器名;uid=sa;pwd=sa");
SqlCommand cmd = new SqlCommand("select name from sys.databases",conn);
try
{
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
cbxDBList.Items.Add(dr[0]);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}