写个类可以省去很多代码,而且可以重用代码,不写类也可以参考下面的代码:
例如:
SqlConnection connection = new SqlConnection("server=192.168.0.1;UID=sa;PWD=1234;database=master");
connection.Open();
DataSet tables = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter("select * from tb_UserInfo", connection);
adapter.Fill(tables,"UserInfo");
dataGridView1.DataSource = tables.Tables["UserInfo"];