C# dataGridView展示資料庫查詢結果

seven_apple發表於2020-10-11

C# dataGridView展示資料庫查詢結果

        private void browsetc()
        {
            //SqlConnection cn = new SqlConnection(sqlText.sql);
            login frmlg = new login();
            string urlIP = "Data Source =" + frmlg.textIP.Text.Trim() + sqlText.sql;
            SqlConnection cn = new SqlConnection(urlIP);
            cn.Open();

            string sql = @"SELECT * FROM [tc_2020]  where Status = 0";//
            SqlCommand cmd = new SqlCommand(sql, cn);

            SqlDataAdapter da = new SqlDataAdapter();

            da.SelectCommand = cmd;

            DataSet ds = new DataSet();

            da.Fill(ds);

            cn.Close();
            //dataGridView2.DataSource = ds.Tables[0];
            dataGridView2.DataSource = ds.Tables[0];
            //dataGridView行數統計
            label20.Text = dataGridView2.Rows.Count.ToString();

            
        }

相關文章