namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private DataSet ds = new DataSet();
private DataTable d1 = new DataTable();
private SqlDataAdapter sd = new SqlDataAdapter();
private void Form1_Load(object sender, EventArgs e)
{
ds.Tables.Add(d1);
string sqlConnection = "Data Source=.;Initial Catalog=mySQL;Integrated Security=True";
string sql = "select * from Table_login";
SqlConnection conn = new SqlConnection(sqlConnection);
conn.Open();
SqlCommand com = new SqlCommand(sql, conn);
sd.SelectCommand = com;
sd.Fill(ds.Tables[0]);
sql = "select * from Table_1";
com = new SqlCommand(sql, conn);
sd.SelectCommand = com;
sd.Fill(ds, "d1");
dataGridView1.DataSource = ds.Tables[0];//ds.Tables[索引],此索引并不等同于往dataset里添加datatable的顺序。
//如需按datatable名,则需按下面的格式
//dataGridView1.DataSource = ds.Tables["d1"];
sd.Dispose();
com.Dispose();
conn.Close();
}
}
}
将数据库中的数据读取到数据表中,并通过datagridview显示
最新推荐文章于 2021-08-18 13:46:38 发布