public Form1() { InitializeComponent(); lb_pri_db.DrawMode = DrawMode.OwnerDrawFixed; lb_sec_db.DrawMode = DrawMode.OwnerDrawFixed; //synchronizationContext = SynchronizationContext.Current; ColoredItem itp = new ColoredItem { Color = Color.Gray, Text = "---primary servers----" }; ColoredItem its = new ColoredItem { Color = Color.Gray, Text = "---secondary servers----" }; lb_pri_db.Items.Add(itp); lb_sec_db.Items.Add(its); selected_databases = new List <LSDB>(); //Thread workingThread = new Thread(new ParameterizedThreadStart(update_ui)) //{ IsBackground = true }; m_oWorker = new BackgroundWorker(); m_oWorker.DoWork += new DoWorkEventHandler(m_oWorker_DoWork); m_oWorker.ProgressChanged += new ProgressChangedEventHandler (m_oWorker_ProgressChanged); m_oWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler (m_oWorker_RunWorkerCompleted); m_oWorker.WorkerReportsProgress = true; m_oWorker.WorkerSupportsCancellation = true; m_oWorker_restore = new BackgroundWorker(); m_oWorker_restore.DoWork += new DoWorkEventHandler(m_oWorker_restore_DoWork); m_oWorker_restore.ProgressChanged += new ProgressChangedEventHandler (m_oWorker_restore_ProgressChanged); m_oWorker_restore.RunWorkerCompleted += new RunWorkerCompletedEventHandler (m_oWorker_restore_RunWorkerCompleted); m_oWorker_restore.WorkerReportsProgress = true; m_oWorker_restore.WorkerSupportsCancellation = true; //new Thread(() => update_ui("")) { IsBackground = true }.Start(); }
//public void Subscriber(SqlCommandWithProgress pub) //{ // //id = ID; // // Subscribe to the event using C# 2.0 syntax // pub.RaiseCustomEvent += HandleCustomEvent; //} //void HandleCustomEvent(object sender, CustomEventArgs e) //{ // lbl_output.Text = e.Message; // //Console.WriteLine(id + " received this message: {0}", e.Message); //} //public void update_ui(string msg) //{ // //while(true) // //{ // // Thread.Sleep(4000); // try // { // SqlCommandWithProgress pub = new SqlCommandWithProgress(); // this.Subscriber(pub); // // Subscriber sub1 = new Subscriber("sub1", pub); // synchronizationContext.Post(new SendOrPostCallback(o => // { // lbl_output.Text = (string)o; // }), msg); // //lbl_output.Text = msg_ls_primary_server.ToString(); // } // catch(Exception ex) // { // MessageBox.Show(ex.Message); // } // //} //} public void test_connection(String srv_name) { // Build connection string builder = new SqlConnectionStringBuilder(); builder.DataSource = srv_name.Trim(); // tb_primary.Text.Trim(); // update me builder.IntegratedSecurity = true; //builder.UserID = "sa"; // update me //builder.Password = "******"; // update me builder.InitialCatalog = "master"; // Connect to SQL Console.Write("Connecting to SQL Server ... "); //Get sql command text string commandText; Assembly thisAssembly = Assembly.GetExecutingAssembly(); using (Stream s = thisAssembly.GetManifestResourceStream( "LogShippingSetup.SCRIPTS.TestConnection.sql")) { using (StreamReader sr = new StreamReader(s)) { commandText = sr.ReadToEnd(); } } try { using (SqlConnection connection = new SqlConnection(builder.ConnectionString)) { connection.Open(); Console.WriteLine("open connection."); // Create a sample database Console.Write("Testing connection ... "); String sql = commandText; // "DROP DATABASE IF EXISTS [SampleDB]; CREATE DATABASE [SampleDB]"; using (SqlCommand command = new SqlCommand(sql, connection)) { using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { if (srv_name == tb_primary.Text) { //DataRow dr = lb_pri_db.Rows[i]; //ListViewItem listitem = new ListViewItem(dr["desc"].ToString()); //listitem.SubItems.Add(dr["desc"].ToString()); //listitem.SubItems.Add(dr["enchimento"].ToString()); //lb_pri_db.Items.AddRange(new object[] { //reader.GetString(0)+","+reader.GetString(1)}); ColoredItem coloredItem; if (reader.GetString(1) == "LSP" || reader.GetString(1) == "LSS") { coloredItem = new ColoredItem { Color = Color.Green, Text = reader.GetString(0) }; } else { coloredItem = new ColoredItem { Color = Color.Black, Text = reader.GetString(0) }; } lb_pri_db.Items.Add(coloredItem); } if (srv_name == tb_secondary.Text) { ColoredItem coloredItem; if (reader.GetString(1) == "LSP" || reader.GetString(1) == "LSS") { coloredItem = new ColoredItem { Color = Color.Green, Text = reader.GetString(0) }; } else { coloredItem = new ColoredItem { Color = Color.Black, Text = reader.GetString(0) }; } lb_sec_db.Items.Add(coloredItem); //lb_sec_db.Items.Add(reader.GetString(0) + " " + reader.GetString(1)); } //Console.WriteLine("{0} {1} {2}", reader.GetInt32(0), reader.GetString(1), reader.GetString(2)); } } //MessageBox.Show("Connection successful"); //command.ExecuteNonQuery(); Console.WriteLine("Done."); } } } catch (SqlException e) { MessageBox.Show(e.Message); Console.WriteLine(e.ToString()); } }