How to get list of all database from sql server in a combobox using c#.net

前端 未结 6 939
北荒
北荒 2020-12-13 16:14

I am entering the source name userid and password through the textbox and want the database list should be listed on the combo box so that all the four options sourcename, u

6条回答
  •  醉梦人生
    2020-12-13 16:44

       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);
            }
    

提交回复
热议问题