2016-09-14 2 views
-1

내 요구 사항은 모든 원격 데이터베이스를 나열하는 것이지만 서버 이름 만 알고 있습니다. 로그인 정보없이 다른 서버에서버 이름 만있는 모든 (원격) 데이터베이스 목록을 얻는 방법은 무엇입니까?

List<String> databases = new List<String>(); 
SqlConnectionStringBuilder connection = new SqlConnectionStringBuilder(); 
connection.DataSource = "localhost"; 
connection.IntegratedSecurity = true; 
//connection.UserID = "";// 
//connection.Password = "";// 
String strConn = connection.ToString(); 
SqlConnection sqlConn = new SqlConnection(strConn); 
sqlConn.Open(); 
DataTable tblDatabases = sqlConn.GetSchema("Databases"); 
sqlConn.Close(); 
foreach (System.Data.DataRow row in tblDatabases.Rows) 
{ 
    String strDatabaseName = row["database_name"].ToString(); 
    databases.Add(strDatabaseName); 
} 

하지만 어떻게 내가 얻을 수있는 데이터베이스 :

나는이 작업 및 로컬 호스트에있는 모든 데이터베이스를 반환 코드 아래 사용하고?

+2

stacktrace를 포함하여 오류를 표시하십시오. 귀하의 코드는'connection.DataSource = "localhost"'로 나를 위해 일하고 있습니다. 나는 8 개의 데이터베이스를 얻는다. –

+0

다른 기계 서버를 사용하십시오. 오류가 발생합니다. –

+0

_ 뭐 오류? 너는 아직 그것을 보여주지 않았다. 로그인 할 수 없다는 오류가 표시되면 사용자와 암호를 제공해야합니다. –

답변

0
public List<string> GetDatabaseList() 
{ 
    List<string> list = new List<string>(); 
    string conString = "server=ServerName;uid=sa;pwd=Password;"; 
    using (SqlConnection con = new SqlConnection(conString)) 
    { 
     con.Open(); 
     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; 
} 
+0

다른 컴퓨터의 사용자 이름, 암호를 어떻게 알 수 있습니까? –

+0

사용자 이름, 비밀번호없이 ... 다른 컴퓨터의 SQL 서버에 액세스하려면 Windows 인증이 필요합니다. 있다면,이를 통합 보안으로 변경하십시오. –

+0

도 그 방법을 시도했습니다. windows 인증 시스템 사용자, pwd했다. 그러나 다른 서버는이 방법이 작동하지 않음을 나타냅니다. –

관련 문제