using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Systems
{
public partial class Form6 : Form
{
public Form6()
{
InitializeComponent();
}
private void Form6_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=.;database=student;trusted_connection=sspi");
SqlCommand cm = new SqlCommand();
cm.Connection = con;
cm.CommandText = "select * from studentInfo";
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cm;
DataSet ds = new DataSet();
da.Fill(ds, "student");
dataGridView1.DataSource = ds.Tables["student"];
}
}
}
最简单的
写一个数据查询类.然后调用xx.chaxun方法
窗体代码写以下这些就可以了
sql = "select * from xxxx";
Datatable dt = xx.chaxun(sql);
datagridview1.DataSource = dt;
记得将datagridview里面的字段与表中的字段相关联
查询结果用 dataset保存起来 然后dataGridView.SqlDataSource=mydataset;
就显示出来了 当然还有更简单的用数据绑定SqlDataSource来做都不用写代码
用数据集嘛~`