winform窗体简单查询功能

1.【一个button txetbox listview】

private void button_Click(object sender, EventArgs e)
{
SqlConnection scn = new SqlConnection(); //创建对象并设置属性
scn.ConnectionString = "server=XZ-20130306TSYW\\SQL;trusted_connection=yes;database=查询;"; //连接字符串
SqlCommand scmd = new SqlCommand(); // 创建并使用sqlCommand对象
scmd.Connection = scn; // 命令对象名.Connection= 连接对象名
DataSet ds = new DataSet(); //创建DataSet对象
scn.Open(); //打开
string strsql = string.Empty; //查询赋初值
strsql = "select * from 表名 where 列名 like '%" + "列名" + "%'"; //查询
scmd.CommandText = strsql;
SqlDataAdapter sda = new SqlDataAdapter(scmd);
sda.Fill(ds, "表名");
DataTable dt = new DataTable();
dt = ds.Tables[0];
foreach (DataRow row in dt.Rows)
{
ListViewItem lsvItem = listView1.Items.Add(row["列名"].ToString());
lsvItem.Tag = row;
}

2.【用一个textbox 和一个button,DataGirdview实现查询】
代码如下:
private void button_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
SqlConnection scn = new SqlConnection();
scn.ConnectionString = "server=XZ-20130306TSYW\\SQL;trusted_connection=yes;database=查询;";
scn.Open();
SqlCommand scmd = new SqlCommand();
scmd.Connection = scn;
string strsql = string.Empty;
if (textBox1.Text.Trim() != "")
{
strsql = "select * from 新芒果大厦 where 网展公司 like '%" + textBox1.Text.Trim() + "%'";
}
else
{

}
scmd.CommandText = strsql;

SqlDataAdapter sda = new SqlDataAdapter(scmd);
sda.Fill(ds, "新芒果大厦");
dt = ds.Tables[0];
dataGridView1.DataSource = dt;
}

posted @ 2013-03-22 13:43  码农的崛起  阅读(1010)  评论(0编辑  收藏  举报