public void select() { db.open(); string sql = "select * from standard"; dataGridView1.DataSource = db.getdatatable(sql); db.close();
} private void button1_Click(object sender, EventArgs e) { try { select(); string a = this.textBox1.Text; string b = this.textBox2.Text; string c = this.textBox3.Text; if (a != "" & b == "" & c == "") { db.open(); string sql = "select * from standard where standardname like '%" + a + "%'"; dataGridView1.DataSource = db.getdatatable(sql); db.close(); } else if (a != "" & b != "" & c == "") { db.open(); string sql = "select* from standard where StandardName like'%" + a + "%' and StandardId='" + b + "'"; dataGridView1.DataSource = db.getdatatable(sql); db.close(); } else if (a != "" & b == "" & c != "") { db.open(); string sql = "select * from standard where StandardName like'%" + a + "%' and Published='" + c + "'"; dataGridView1.DataSource = db.getdatatable(sql); db.close(); } else if (a != "" & b != "" & c != "") { db.open(); string sql = "select* from standard where StandardName like'%" + a + "%' and StandardId='" + b + "' and Published='" + c + "'"; dataGridView1.DataSource = db.getdatatable(sql); db.close(); } else if (a == "" & b != "" & c != "") { db.open(); string sql = "select* from standard where Published='" + c + "' and StandardId='" + b + "'"; dataGridView1.DataSource = db.getdatatable(sql); db.close(); } else if (a == "" & b != "" & c == "") { db.open(); string sql = "select* from standard where StandardId='" + b + "'"; dataGridView1.DataSource = db.getdatatable(sql); db.close(); } else if (a != "" & b != "" & c == "") { db.open(); string sql = "select* from standard where StandardName like'%" + a + "%' and StandardId='" + b + "'"; dataGridView1.DataSource = db.getdatatable(sql); db.close(); } else if (a == "" & b == "" & c != "") { db.open(); string sql = "select* from standard where Published='" + c + "'"; dataGridView1.DataSource = db.getdatatable(sql); db.close(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { this.textBox1.Text = ""; this.textBox2.Text = ""; this.textBox3.Text = ""; } } 我昨天才做的 也是刚学的 你自己可以看看 我这个是通过TEXTBOX输入的信息查询 可能有点长 不过还算好理解!
在dataGridView中清空原记录并重新填充
应该怎么写了```麻烦了~
string strPub = "select * from T_Publish where LifeAct>'" + DateTime.Now + "' or (LifeDie>'" + DateTime.Now + "' and LifeAct<'" + DateTime.Now + "')";
SqlCommand comPub = new SqlCommand(strPub, mycon);
DataSet dsPub = new DataSet();
SqlDataAdapter adPub = new SqlDataAdapter(comPub);
adPub.Fill(dsPub);
scr.Clear();
if (dsPub.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < dsPub.Tables[0].Rows.Count; i++)
{}
}
//..重新填充的代码
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter("select * from HD_locotype where nindex<" + textBox1.Text, conn);
DataSet ds = new DataSet();
adapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0]; //数据源绑定后会自动清空原来的数据的
-- 测试数据
create table table2
(
姓名 nvarchar(15),
编号 nvarchar(15),
岗位工资 decimal(10,2),
金额 decimal(10,2),
加班 decimal(10,2),
扣岗 decimal(10,2),
) insert into table2
select '张三','01',100,100,50,30
union all
select '李四','02',200,100,60,40
union all
select '王五','03',100,200,50,20 select * from table2 页面代码 添加两Button,命名btnLoadData,btnExport;
添加按钮的Click事件代码.using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;public partial class Salary : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } private const string conString = "Server=SZSOFT-SZZB\\SQLEXPRESS;database=office;uid=sa;pwd=abc_!123456;"; protected void btnLoadData_Click(object sender, EventArgs e)
{
GridView1.DataSource = GetSalary();
GridView1.DataBind();
} /// <summary>
/// 导出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnExport_Click(object sender, EventArgs e)
{ this.Response.Clear();
this.Response.Buffer = true;
this.Response.Charset = "GB2312";
this.Response.AppendHeader("Content-Disposition", "attachment;filename=salary.xls"); this.Response.ContentEncoding = System.Text.Encoding.UTF7;
this.Response.ContentType = "application/ms-excel"; DataTable dt = GetSalary();
string excelScript = CreateExcelScript(dt); this.Response.Output.Write(excelScript);
this.Response.Flush();
this.Response.End(); } /// <summary>
/// 获取工资数据
/// </summary>
/// <returns></returns>
private DataTable GetSalary()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = conString;
con.Open();
string sqlSelect = "Select * From table2";
SqlDataAdapter adapter = new SqlDataAdapter(sqlSelect, con);
DataTable dt = new DataTable();
adapter.Fill(dt);
return dt;
}
/// <summary>
/// 格式化导出数据
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
private string CreateExcelScript(DataTable dt)
{
// 表格
StringBuilder sbTable = new StringBuilder();
// 标题
StringBuilder sbTitle = new StringBuilder();
// 行数据
StringBuilder sbRowData = new StringBuilder();
// 总列数
int colCount = dt.Columns.Count;
// 总行数
int rowCount = dt.Rows.Count; // 标题
for (int i = 0; i < colCount; i++)
{
sbTitle.AppendFormat("<td>{0}</td>",dt.Columns[i].ColumnName);
} sbTable.Append("<table border=1>");
// 遍历数据
for(int m=0;m<rowCount;m++)
{
// 添加标题
sbTable.AppendFormat("<tr>{0}</tr>",sbTitle.ToString());
// 添加数据
DataRow dr = dt.Rows[m];
sbRowData = new StringBuilder();
for (int n = 0; n < colCount; n++)
{
sbRowData.AppendFormat("<td>{0}</td>", dr[n].ToString());
}
sbTable.AppendFormat("<tr>{0}</tr>", sbRowData.ToString());
// 添加空行
if (m != rowCount - 1)
{
sbTable.AppendFormat("<tr><td colspan='{0}'></td></tr>", colCount);
}
}
sbTable.Append("</table>"); return sbTable.ToString();
}
}
/// <summary>
/// 获取工资数据
/// </summary>
/// <returns></returns>
private DataTable GetSalary(string name)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = conString;
con.Open();
string sqlSelect = string.Format("Select * From table2 where [姓名]='{0}'", name);
SqlDataAdapter adapter = new SqlDataAdapter(sqlSelect, con);
DataTable dt = new DataTable();
adapter.Fill(dt);
return dt;
}
2000的话是
string sql="user id=sa;password=密码;database=那个数据库;data source=127.0.0.1";
string sqlselect="select * from 你的表名";
datatble db=new db();
sqlconnection conn=new sqlconnection(sql);
conn.open(); 打开连接
然后是输入命令
sqlcommand comm=new sqlcommand(sqlselect,sql); 格式是命令跟连接方式
拉个 Datagridview控件在FORM1上用来显示查询
SqlDataAdapter da=new SqlDataAdapter(comm);
da.Fill(db);
Datagridview1.datasource=db;
public void select()
{
db.open();
string sql = "select * from standard";
dataGridView1.DataSource = db.getdatatable(sql);
db.close();
} private void button1_Click(object sender, EventArgs e)
{
try
{
select();
string a = this.textBox1.Text;
string b = this.textBox2.Text;
string c = this.textBox3.Text;
if (a != "" & b == "" & c == "")
{
db.open();
string sql = "select * from standard where standardname like '%" + a + "%'";
dataGridView1.DataSource = db.getdatatable(sql);
db.close();
}
else if (a != "" & b != "" & c == "")
{
db.open();
string sql = "select* from standard where StandardName like'%" + a + "%' and StandardId='" + b + "'";
dataGridView1.DataSource = db.getdatatable(sql);
db.close();
}
else if (a != "" & b == "" & c != "")
{
db.open();
string sql = "select * from standard where StandardName like'%" + a + "%' and Published='" + c + "'";
dataGridView1.DataSource = db.getdatatable(sql);
db.close();
}
else if (a != "" & b != "" & c != "")
{
db.open();
string sql = "select* from standard where StandardName like'%" + a + "%' and StandardId='" + b + "' and Published='" + c + "'";
dataGridView1.DataSource = db.getdatatable(sql);
db.close();
}
else if (a == "" & b != "" & c != "")
{
db.open();
string sql = "select* from standard where Published='" + c + "' and StandardId='" + b + "'";
dataGridView1.DataSource = db.getdatatable(sql);
db.close(); }
else if (a == "" & b != "" & c == "")
{
db.open();
string sql = "select* from standard where StandardId='" + b + "'";
dataGridView1.DataSource = db.getdatatable(sql);
db.close(); }
else if (a != "" & b != "" & c == "")
{
db.open();
string sql = "select* from standard where StandardName like'%" + a + "%' and StandardId='" + b + "'";
dataGridView1.DataSource = db.getdatatable(sql);
db.close(); }
else if (a == "" & b == "" & c != "")
{
db.open();
string sql = "select* from standard where Published='" + c + "'";
dataGridView1.DataSource = db.getdatatable(sql);
db.close(); }
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
this.textBox1.Text = "";
this.textBox2.Text = "";
this.textBox3.Text = "";
}
}
我昨天才做的 也是刚学的 你自己可以看看 我这个是通过TEXTBOX输入的信息查询 可能有点长 不过还算好理解!
DataTable tb=new DataTable();private void button1_click(...)
{
SqlDataAdapter da=new SqlDataAdapter("select * from table where "+TextBox1.Text,conn);
tb.Clear();
da.Fill(tb);
dataGridView1.DataSource = tb;
}