/// <summary> /// 获得网络中可用的 SQL Server 实例集合。 /// </summary> /// <returns></returns> public static IEnumerable <string> GetSqlServerInstances() { List <string> lsNames = new List <string>(); try { System.Data.Sql.SqlDataSourceEnumerator instance = System.Data.Sql.SqlDataSourceEnumerator.Instance; System.Data.DataTable dt = instance.GetDataSources(); foreach (System.Data.DataRow dr in dt.Rows) { if (dr["InstanceName"].ToString().Length > 0) { lsNames.Add(dr["ServerName"].ToString() + "\\" + dr["InstanceName"].ToString()); } else { lsNames.Add(dr["ServerName"].ToString()); } } } catch { } return(lsNames); }
private void txtServer_DropDown(object sender, EventArgs e) { if (txtServer.Items.Count > 0) { return; } Cursor = System.Windows.Forms.Cursors.WaitCursor; try { // Retrieve the enumerator instance and then the data. System.Data.Sql.SqlDataSourceEnumerator instance = System.Data.Sql.SqlDataSourceEnumerator.Instance; System.Data.DataTable table = instance.GetDataSources(); foreach (System.Data.DataRow row in table.Rows) { txtServer.Items.Add(row["ServerName"] + (row["InstanceName"].ToString().Length > 0 ? "\\" + row["InstanceName"].ToString() : "")); } } finally { Cursor = System.Windows.Forms.Cursors.Default; } }
private void BrowseForServers_Load(object sender, EventArgs e) { Application.UseWaitCursor = true; Application.DoEvents(); //BrowseForServers.SendMessage(Form.ActiveForm.Handle, 0x20, Form.ActiveForm.Handle, (IntPtr)1); System.Data.Sql.SqlDataSourceEnumerator servers = System.Data.Sql.SqlDataSourceEnumerator.Instance; DataTable serversTable = servers.GetDataSources(); foreach (DataRow row in serversTable.Rows) { if (row[1].Equals(DBNull.Value)) { string serverName = string.Format("{0}", row[0]); listBox.Items.Add(serverName); } else { string serverName = string.Format("{0}\\{1}", row[0], row[1]); //appending instance name to the server name listBox.Items.Add(serverName); //adding } } //GetNetworkServerInstances(); Application.UseWaitCursor = false; }
/// <summary> /// Gets the array of SQL servers that are available on the LAN. /// </summary> /// <returns>string[] of SQL server names</returns> public static string[] GetSQLServers() { string[] retval = null; DataTable dt = null; System.Data.Sql.SqlDataSourceEnumerator sds = System.Data.Sql.SqlDataSourceEnumerator.Instance; dt = sds.GetDataSources(); ArrayList al = new ArrayList(); if (retval != null) { for (int zz = 0; zz < retval.Length; zz++) { if (!al.Contains(retval[zz])) { al.Add(retval[zz]); } } } if (dt != null) { foreach (DataRow row in dt.Rows) { string name = row["ServerName"].ToString() + (row["InstanceName"].ToString().Trim().Length != 0 ? "\\" + row["InstanceName"].ToString() : ""); if (!al.Contains(name)) { al.Add(name); } } dt.Dispose(); } al.Sort(); return((string[])al.ToArray(typeof(string))); }
private List <string> getInstancesSQLSERVER() { List <string> sqlInstances = new List <string>(); System.Data.Sql.SqlDataSourceEnumerator instance = System.Data.Sql.SqlDataSourceEnumerator.Instance; System.Data.DataTable dataTable = instance.GetDataSources(); foreach (DataRow row in dataTable.Rows) { string instanceName = row["ServerName"].ToString(); if (!sqlInstances.Contains(instanceName) && !instanceName.Contains(Environment.MachineName)) { sqlInstances.Add(instanceName); } } List <string> lclInstances = GetLocalSqlServerInstanceNames(); foreach (var lclInstance in lclInstances) { string instanceName = Environment.MachineName; if (!sqlInstances.Contains(instanceName)) { sqlInstances.Add(instanceName); } } sqlInstances.Sort(); return(sqlInstances); }
/// <summary> /// Lists all available SQL servers. /// </summary> public void ListAvailableServers() { // Get the data source enumerator object. System.Data.Sql.SqlDataSourceEnumerator serversEnum = System.Data.Sql.SqlDataSourceEnumerator.Instance; // Get the data sources and fill the list view. try { // Get data sources. using (DataTable servers = serversEnum.GetDataSources()) { // Process each row and populate the list view. foreach (DataRow row in servers.Rows) { // Check that the server is not null. if (!row.IsNull("ServerName")) { // Construct the instance name. string serverInstance = (string)row["ServerName"]; if (!row.IsNull("InstanceName")) { serverInstance = serverInstance + @"\" + (string)row["InstanceName"]; } // Update the list view. _viewAdapter.AddItemToSQLInstanceList(serverInstance.ToUpper()); } } } } catch (Exception ex) { _viewAdapter.DisplayMessageBox("Error browsing for servers.", "Error!"); } }
private void comboBox1_DropDown(object sender, EventArgs e) { this.Cursor = System.Windows.Forms.Cursors.WaitCursor; /*Отображаем список всех доступных серверов*/ System.Data.Sql.SqlDataSourceEnumerator NI = System.Data.Sql.SqlDataSourceEnumerator.Instance; comboBox1.DataSource = NI.GetDataSources(); this.Cursor = System.Windows.Forms.Cursors.Default; }
public void FillComboServers(ComboBox Combo) { System.Data.Sql.SqlDataSourceEnumerator SqlEnumerator = System.Data.Sql.SqlDataSourceEnumerator.Instance; System.Data.DataTable dTable = SqlEnumerator.GetDataSources(); foreach (DataRow Dr in dTable.Rows) { Combo.Items.Add(Dr[0]); } }
// pegar o nome do servidor sql server public static string getServer() { string servidor = string.Empty; System.Data.Sql.SqlDataSourceEnumerator instancia = System.Data.Sql.SqlDataSourceEnumerator.Instance; System.Data.DataTable dados = instancia.GetDataSources(); foreach (DataRow item in dados.Rows) { servidor = item["ServerName"].ToString(); } return(servidor); }
/// <summary> /// 获取局域网中服务器名 /// </summary> /// <returns></returns> public static string[] GetServers() { System.Data.Sql.SqlDataSourceEnumerator instance = System.Data.Sql.SqlDataSourceEnumerator.Instance; System.Data.DataTable table = instance.GetDataSources(); string[] servers = new string[table.Rows.Count]; for (int i = 0; i < table.Rows.Count; i++) { System.Data.DataRow row = table.Rows[i]; servers[i] = row["ServerName"].ToString(); } return(servers); }
// pegar o nome do instancia sql server public static string getInstance() { string instancia = string.Empty; System.Data.Sql.SqlDataSourceEnumerator instance = System.Data.Sql.SqlDataSourceEnumerator.Instance; System.Data.DataTable dados = instance.GetDataSources(); foreach (DataRow item in dados.Rows) { instancia = item["InstanceName"].ToString(); } return(instancia); }
public bool LoadServers(bool registeredOnly) { // Get the data source enumerator object. bool isOk = true; System.Data.Sql.SqlDataSourceEnumerator serversEnum = System.Data.Sql.SqlDataSourceEnumerator.Instance; // Get the data sources and fill the list view. try { if (!registeredOnly) { // Get data sources. using (DataTable servers = serversEnum.GetDataSources()) { // Process each row and populate the list view. foreach (DataRow row in servers.Rows) { // Check that the server is not null. if (!row.IsNull("ServerName")) { // Construct the instance name. string serverInstance = (string)row["ServerName"]; if (!row.IsNull("InstanceName")) { serverInstance = serverInstance + @"\" + (string)row["InstanceName"]; } // Update the list view. _listView_Servers.Items.Add(serverInstance); } } } } else { var servers = RegisteredServer.LoadRegisteredServers(Program.gController.Repository.ConnectionString); foreach (RegisteredServer item in servers) { _listView_Servers.Items.Add(item.FullName); } } } catch (Exception ex) { logX.loggerX.Error("ERROR - exception raised when enumerating servers, ", ex); isOk = false; } return(isOk); }
private void _GetAvailableSQLSvrs() { Cursor.Current = Cursors.WaitCursor; System.Data.Sql.SqlDataSourceEnumerator instance = System.Data.Sql.SqlDataSourceEnumerator.Instance; System.Data.DataTable dataTable = instance.GetDataSources(); foreach (System.Data.DataRow row in dataTable.Rows) { cbxSqlServers.Items.Add((string)row[0].ToString()); } Cursor.Current = Cursors.Default; // if the local machine is in the list, lets select it by default cbxSqlServers.SelectedIndex = cbxSqlServers.FindString(Dns.GetHostName()); }
private async void EnlistServers() { System.Data.Sql.SqlDataSourceEnumerator instance = System.Data.Sql.SqlDataSourceEnumerator.Instance; System.Data.DataTable dataTable = await Task <System.Data.DataTable> .Run(() => { return(instance.GetDataSources()); }).ConfigureAwait(true); //DataServers.Clear(); //foreach (var r in dataTable.Rows) // DataServers.Add( (DataRow)r); dataTable.Rows.Add(dataTable.NewRow()); DataServers = dataTable.DefaultView; SelectedServer = DataServers.Table.DefaultView[1]; //SelectedServerIndex = DataServers.Rows.Count > 0 ? 0 : -1; }
public void Refresh() { SqlServerInstances.Clear(); System.Data.Sql.SqlDataSourceEnumerator instance = System.Data.Sql.SqlDataSourceEnumerator.Instance; System.Data.DataTable dataTable = instance.GetDataSources(); foreach (System.Data.DataRow row in dataTable.Rows) { foreach (System.Data.DataColumn col in dataTable.Columns) { Console.WriteLine("{0} = {1}", col.ColumnName, row[col]); } SqlServerInstances.Add(row[0] + "\\" + row[1]); } SelectedSqlServerInstance = 0; }
// Function: Get Local SQL Server Instances // ==================================================================================================================== public List <string> GetSqlInstances() { System.Data.Sql.SqlDataSourceEnumerator instance = System.Data.Sql.SqlDataSourceEnumerator.Instance; DataTable dataTable = instance.GetDataSources(); List <string> list = new List <string>(); foreach (DataRow row in dataTable.Rows) { string temp = row.ItemArray[0].ToString(); if (row.ItemArray[1].ToString() != "") { temp += $@"\{row.ItemArray[1]}"; } list.Add(temp); } return(list); }
private List <string> NetworkServersList() { Cursor.Current = Cursors.WaitCursor; List <string> networkServers = new List <string>(); try { System.Data.Sql.SqlDataSourceEnumerator instance = System.Data.Sql.SqlDataSourceEnumerator.Instance; DataTable tableServers = instance.GetDataSources(); foreach (DataRow row in tableServers.Rows) { networkServers.Add(row["ServerName"].ToString() + ((row.IsNull("InstanceName") || (row["InstanceName"].ToString() == "MSSQLSERVER")) ? "" : (@"\" + row["InstanceName"].ToString()))); } } catch (Exception ex) { } Cursor.Current = Cursors.Default; return(networkServers); }
private void dbProviderName_SelectedIndexChanged(object sender, EventArgs e) { if (dbProviderCbx.SelectedIndex == 0) { //获取本地网络的所有服务器 System.Data.Sql.SqlDataSourceEnumerator instance = System.Data.Sql.SqlDataSourceEnumerator.Instance; DataTable Dt = instance.GetDataSources(); Dt.Columns["ServerName"].ReadOnly = false; for (int i = 0; i < Dt.Rows.Count; i++) { if (Dt.Rows[i]["InstanceName"] != DBNull.Value && Dt.Rows[i]["InstanceName"].ToString() != "") { Dt.Rows[i]["ServerName"] = Dt.Rows[i]["ServerName"].ToString() + "\\" + Dt.Rows[i]["InstanceName"].ToString(); } } ServerCbx.DataSource = Dt; ServerCbx.DisplayMember = "ServerName"; ServerCbx.ValueMember = "ServerName"; } else { ServerCbx.DataSource = null; } }
/// <summary> /// Thread proc to load the servers in background /// </summary> private void LoadServersProc() { System.Data.Sql.SqlDataSourceEnumerator instance = System.Data.Sql.SqlDataSourceEnumerator.Instance; dataTable = instance.GetDataSources(); }
private void GetSSI() { System.Data.Sql.SqlDataSourceEnumerator xoDbEnumerator = System.Data.Sql.SqlDataSourceEnumerator.Instance; System.Data.DataTable xoDataTable = xoDbEnumerator.GetDataSources(); //DataTable xoDataTable = SmoApplication.EnumAvailableSqlServers(); if (xoDataTable.Rows.Count > 0) { for (int xiRow = 0; xiRow < xoDataTable.Rows.Count; xiRow++) { string xsInfo = ""; for (int xiFld = 0; xiFld < xoDataTable.Columns.Count; xiFld++) { if (xiFld > 0) { xsInfo += "\r\n"; } xsInfo += xoDataTable.Columns[xiFld].ColumnName + ": "; xsInfo += xoDataTable.Rows[xiRow][xiFld].ToString(); } MessageBox.Show(xsInfo); } SqlConnection xoConn = new SqlConnection(); //"Server={DS};Database={DB};User Id={UID};Password={PSWD};"; xoConn.ConnectionString = "Server=LEE-PC\\ENTERPRISE2012;Trusted_Connection=True;"; xoConn.Open(); SqlCommand xoCmd = new SqlCommand(); xoCmd.CommandText = "SELECT [name] FROM master.sys.databases"; xoCmd.CommandType = CommandType.Text; xoCmd.Connection = xoConn; SqlDataReader xoRdr = xoCmd.ExecuteReader(); if (xoRdr.HasRows) { string xsDBs = ""; xoRdr.Read(); while (true) { object xo = xoRdr.GetValue(0); xsDBs = xsDBs + "\r\n" + xo.ToString(); if (!xoRdr.Read()) { break; } } MessageBox.Show(xsDBs); } xoRdr.Dispose(); xoCmd.Dispose(); } xoDbEnumerator = null; xoDataTable.Dispose(); xoDataTable = null; }
private void button2_Click(object sender, EventArgs e) { Server_CB.Top = 30; Server_CB.Left = 50; Server_CB.Width = 330; Server_CB.Height = 40; Server_CB.Items.Clear(); Server_CB.Font = new Font("Times New Roman", 10, FontStyle.Bold); Server_CB.Text = "Выберите доступные сервера"; System.Data.Sql.SqlDataSourceEnumerator Server_List = System.Data.Sql.SqlDataSourceEnumerator.Instance; DataTable Server_Table = Server_List.GetDataSources(); foreach (DataRow row in Server_Table.Rows) { Server_CB.Items.Add(row[0] + "\\" + row[1]); } con2pan.Controls.Add(Server_CB); Registration_Window.Show(); this.BackColor = Color.Gray; button1.BackColor = Color.Gray; button1.ForeColor = Color.DimGray; button2.BackColor = Color.Gray; button2.ForeColor = Color.DimGray; button3.BackColor = Color.Gray; button3.ForeColor = Color.DimGray; this.Enabled = false; conpan.Width = 487; conpan.Height = 298; SetRoundedShape(conpan, 70); conpan.Top = 14; conpan.Left = 14; conpan.BackColor = Color.White; Registration_Window.Controls.Add(conpan); con2pan.Width = conpan.Width - 55; con2pan.Height = conpan.Height - 55; //SetRoundedShape(regis, 70); con2pan.Top = 27; con2pan.Left = 27; con2pan.BackColor = Color.Indigo; conpan.Controls.Add(con2pan); //----------------------------------------------------- //Создвание надписи "Логин от источника данных" Spisok_Label.Top = Server_CB.Top - 30; Spisok_Label.Left = Server_CB.Left; Spisok_Label.Height = 20; Spisok_Label.Width = Server_CB.Width; Spisok_Label.Text = "Список доступных серверов"; Spisok_Label.TextAlign = ContentAlignment.BottomCenter; Spisok_Label.ForeColor = Color.White; Spisok_Label.Font = new Font("Times New Roman", 10, FontStyle.Bold); con2pan.Controls.Add(Spisok_Label); Base_Login_Label.Top = Server_CB.Top + Server_CB.Height; Base_Login_Label.Left = Server_CB.Left; Base_Login_Label.Height = 20; Base_Login_Label.Width = Server_CB.Width; Base_Login_Label.Text = "Логин от источника данных"; Base_Login_Label.TextAlign = ContentAlignment.BottomCenter; Base_Login_Label.ForeColor = Color.White; Base_Login_Label.Font = new Font("Times New Roman", 10, FontStyle.Bold); con2pan.Controls.Add(Base_Login_Label); //----------------------------------------------------- //Создание поля ввода логина от базы данных Base_Login_Text.Top = Base_Login_Label.Top + Base_Login_Label.Height + 2; Base_Login_Text.Left = Server_CB.Left; Base_Login_Text.Width = Server_CB.Width; Base_Login_Text.Clear(); Base_Login_Text.Text = "Введите логин от источника данных"; Base_Login_Text.Font = new Font("Times New Roman", 10, FontStyle.Bold); Base_Login_Text.BackColor = Color.White; Base_Login_Text.Leave += Base_Login_Text_Leave; Base_Login_Text.Enter += Base_Login_Text_Enter; con2pan.Controls.Add(Base_Login_Text); //----------------------------------------------------- //Создвание надписи "Пароль от источника данных" Base_Password_Label.Top = Base_Login_Text.Top + Base_Login_Text.Height; Base_Password_Label.Left = Server_CB.Left; Base_Password_Label.Height = 20; Base_Password_Label.Width = Server_CB.Width; Base_Password_Label.ForeColor = Color.White; Base_Password_Label.Text = "Пароль от источника данных"; Base_Password_Label.Font = new Font("Times New Roman", 10, FontStyle.Bold); Base_Password_Label.TextAlign = ContentAlignment.BottomCenter; con2pan.Controls.Add(Base_Password_Label); //----------------------------------------------------- //Создание поля ввода пароль от базы данных Base_Password_Text.Top = Base_Password_Label.Top + Base_Password_Label.Height + 5; Base_Password_Text.Left = Server_CB.Left; Base_Password_Text.Width = Server_CB.Width; Base_Password_Text.Clear(); Base_Password_Text.Text = "Введите пароль от источника данных"; Base_Password_Text.Font = new Font("Times New Roman", 10, FontStyle.Bold); Base_Password_Text.BackColor = Color.White; Base_Password_Text.Leave += Base_Password_Text_Leave; Base_Password_Text.Enter += Base_Password_Text_Enter; con2pan.Controls.Add(Base_Password_Text); Baza_Label.Top = Base_Password_Text.Top + Base_Password_Text.Height; Baza_Label.Left = Server_CB.Left; Baza_Label.Height = 20; Baza_Label.Width = Server_CB.Width; Baza_Label.ForeColor = Color.White; Baza_Label.Text = "Список доступных баз данных"; Baza_Label.Font = new Font("Times New Roman", 10, FontStyle.Bold); Baza_Label.TextAlign = ContentAlignment.BottomCenter; con2pan.Controls.Add(Baza_Label); //----------------------------------------------------- //Создание выпадающего списка всех баз данных DataBase_CB.Top = Baza_Label.Top + Baza_Label.Height + 5; DataBase_CB.Left = Server_CB.Left; DataBase_CB.Width = Server_CB.Width; DataBase_CB.DataSource = null; DataBase_CB.DisplayMember = ""; DataBase_CB.Items.Clear(); DataBase_CB.Text = "Выберите нужную Базу данных"; DataBase_CB.Font = new Font("Times New Roman", 10, FontStyle.Bold); DataBase_CB.Click += DataBase_CB_Click; con2pan.Controls.Add(DataBase_CB); //----------------------------------------------------- // Создание кнопки "Закрыть" Cancel_Button.Top = DataBase_CB.Top + DataBase_CB.Height + 15; Cancel_Button.Left = 5; Cancel_Button.Width = 208; Cancel_Button.Height = 30; Cancel_Button.Text = "Выйти из подключения"; Cancel_Button.Click += Cancel_Button_Click; Cancel_Button.BackColor = Color.White; Cancel_Button.ForeColor = Color.Indigo; Cancel_Button.FlatStyle = FlatStyle.Popup; Cancel_Button.Font = new Font("Times New Roman", 12, FontStyle.Bold); con2pan.Controls.Add(Cancel_Button); // Создание кнопки "Подключиться" Connect_Data_Source_Button.Top = DataBase_CB.Top + DataBase_CB.Height + 15; Connect_Data_Source_Button.Left = Cancel_Button.Left + Cancel_Button.Width + 5; Connect_Data_Source_Button.Width = 208; Connect_Data_Source_Button.Height = 30; Connect_Data_Source_Button.Text = "Подключиться"; //Connect_Data_Source_Button.Enabled = false; Connect_Data_Source_Button.BackColor = Color.White; Connect_Data_Source_Button.ForeColor = Color.Indigo; Connect_Data_Source_Button.FlatStyle = FlatStyle.Popup; Connect_Data_Source_Button.FlatAppearance.BorderSize = 10; Connect_Data_Source_Button.Font = new Font("Times New Roman", 12, FontStyle.Bold); //Connect_Data_Source_Button.Click += this.Connect_Data_Source_Button_Click; //Connect_Data_Source_Button.MouseEnter += this.Connect_Data_Source_Button_MouseEnter; //Connect_Data_Source_Button.MouseLeave += this.Connect_Data_Source_Button_MouseLeave; //Connect_Data_Source_Button.MouseDown += this.Connect_Data_Source_Button_MouseDown; //Connect_Data_Source_Button.MouseUp += this.Connect_Data_Source_Button_MouseUp; con2pan.Controls.Add(Connect_Data_Source_Button); //this.Height = this.Height + 200; }