当前位置:数据库 > SQLServer >>

C#中读取sql server的数据,并在datagridview中显示

 private void button1_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = "server=localhost;uid=sa;pwd=123456;database=table1";
                conn.Open();
                using (SqlCommand command = conn.CreateCommand())
                {
                    command.CommandText = "select * from gongneng";
                    SqlDataAdapter da = new SqlDataAdapter(command);
                   
                    try
                    {
                        da.Fill(ds);
                    }
                    catch
                    { }
                }
            }
            DataTable dt=new DataTable ();
            dt = ds.Tables[0].Copy();
            this.dataGridView1.DataSource = dt;  //绑定到datagridview中显示
        }
补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,