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中顯示
}