哪位好心的大虾给做做这两个最简单的查询例子呗~
1.在textbox里输入条件 点击Button gridview中显示查询结果 失败提示~
2.DropDownList中选择查询条件 点击Button gridview中显示查询结果 失败提示~
1.在textbox里输入条件 点击Button gridview中显示查询结果 失败提示~
2.DropDownList中选择查询条件 点击Button gridview中显示查询结果 失败提示~
sting sqlStr="SELECT * FROM TableName WHERE [ID]={0}";
sqlStr=string.Fromat(sqlStr,Conver.ToInt64(this.TextBox1.text.Trim()));
动态查询请一定要注意安全性!!
SqlCommand.CommandText = strsql
SqlCommand.ExecuteQuery()try catch 捕捉异常输出查询失败dataset接受查询结果gridview.DataSource =dataset
gridview.DataBind()
/// <summary>
/// 执行数据库查询操作
/// </summary>
/// <param name="command">命令对象</param>
/// <param name="commandType">命令类型</param>
/// <param name="commandText">SQL文本或存储过程名</param>
/// <param name="commandParameters">参数</param>
/// <returns>表DataTable数据集</returns>
public static DataTable ExecutCommandReturnTable(OleDbCommand command, CommandType commandType, string commandText, OleDbParameter[] commandParameters)
{
command.CommandType = commandType;
command.CommandText = commandText;
if (commandParameters != null) command.Parameters.AddRange(commandParameters);
DataTable table = new DataTable();
using (command.Connection)
{
using (command)
{
command.Connection.Open();
OleDbDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult);
//填充表
table.Load(reader);
command.Cancel();
}
}
return table;
}
DataSet ds= new DataSet();
using(SqlConnection conn= new SqlConnection(connstring ))
{
string strsql="select * from from Tb where 1=1 ";
SqlDataAdapter thisAdapter = new SqlDataAdapter(strsql, conn);
thisAdapter.Fill(ds);
gridview.DataSource =ds;
gridview.DataBind();
conn.Close();
}
if(ds.Tables[0].Rows.Count<=0)
{
}
或参数
if(DropDownList1.SelectedValue!="")
{
strsql+=" and name='"+DropDownList1.SelectedValue+"'";
}