using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 多条件查询
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
bands();
}
private void bands()
{
string sql = "select CId, CName, CDesc from TClass";
DataTable dt = SqlHelPer.Gettable(sql);
dataGridView1.DataSource = dt;
}
private void btncha_Click(object sender, EventArgs e)
{
string sql = "select CId, CName, CDesc from TClass where 1=1";
List<SqlParameter> list = new List<SqlParameter>();
if (!string.IsNullOrEmpty(txtname.Text))
{
sql += " and CName like @CName";
list.Add(new SqlParameter("@CName","%"+txtname.Text+"%"));
}
if (!string.IsNullOrEmpty(txtinfo.Text))
{
sql += " and CDesc like @CDesc";
list.Add(new SqlParameter("@CDesc","%"+ txtinfo.Text+"%"));
}
SqlDataReader dr = SqlHelPer.ExecuteReader(sql,list.ToArray());
List<Class> listn = new List<Class>();
if (dr.HasRows)
{
while (dr.Read())
{
Class c = new Class();
c.CDesc = dr["CDesc"].ToString();
c.CName=dr["CName"].ToString();
c.CId=Convert.ToInt32(dr["CId"]);
listn.Add(c);
}
}
dataGridView1.DataSource = listn;
}
}
}
像这种只能一步步往下查询 在第一次查询时 多列的模糊查询 我使用了 or 但是这个拼接的字符串太大了 (列名有10个)
今天学习的有 dataset sqlhelper封装的四个方法 资源管理器 还有 以上的多条件查询
通过几天的ADO 学习,渐渐开始明白了实体类的作用 ,每一张表对应的就是一个实体类
第一次感觉学习如此充实 生活充满压力,虽然我刚刚迈向这个门槛,但是我能坚持!
菜鸟一枚,正努力学习 !
资源管理器
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 资源管理器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//获得跟元素
List<Category> list= Getid(-1);
//将跟元素添加到跟节点
loadnodes(list, tv.Nodes);
}
private void loadnodes(List<Category> list, TreeNodeCollection tnc)
{
foreach (Category item in list)
{
TreeNode tn= tnc.Add(item.TName);
tn.Tag = item.TId;
//遍历里面所有的信息 递归
loadnodes(Getid(item.TId),tn.Nodes);
}
}
private List<Category> Getid(int p)
{
List<Category> list = new List<Category>();
string sql = "select tId,tName from Category where tParentId="+p;
SqlDataReader dr = SqlHelPer.ExecuteReader(sql);
Category cat = null;
if (dr.HasRows)
{
while (dr.Read())
{
cat = new Category();
cat.TId = Convert.ToInt32(dr["tId"]);
cat.TName = dr["tName"].ToString();
// cat.TParentId = Convert.ToInt32(dr["tParentId"]);
list.Add(cat);
}
}
return list;
}
private void tv_AfterSelect(object sender, TreeViewEventArgs e)
{
//MessageBox.Show(tv.SelectedNode.Tag.ToString());
if (tv.SelectedNode.Tag!=null) //拿到刚刚在上面存的tID
{
int id = Convert.ToInt32(tv.SelectedNode.Tag);
Getbyid(id);
}
}
private void Getbyid(int id)
{
List<ContentInfo> list=new List<ContentInfo>();
string sql = "select dId, dTId, dName, dContent from ContentInfo where dTId=" + id;
SqlDataReader dr = SqlHelPer.ExecuteReader(sql);
if (dr.HasRows)
{
while (dr.Read())
{
ContentInfo cn = new ContentInfo();
// cn.DContent=dr["dContent"].ToString();
cn.DId=Convert.ToInt32(dr["dId"]);
cn.DName=dr["dName"].ToString();
list.Add(cn);
}
}
listBox1.DataSource = list;
listBox1.DisplayMember = "dName";
listBox1.ValueMember = "dId";
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItems.Count>0)
{
// MessageBox.Show(listBox1.SelectedValue.ToString());
string id = listBox1.SelectedValue.ToString();
string sql = "select dContent from ContentInfo where dId= "+id;
SqlDataReader dr = SqlHelPer.ExecuteReader(sql);
ContentInfo cn = new ContentInfo();
if (dr.HasRows)
{
while (dr.Read())
{
cn.DContent = dr["DContent"].ToString();
}
textBox1.Text = cn.DContent;
}
}
}
}
}
这个因为我刚学 还是有点不之其所以然 唉 人有点笨 只能靠勤奋弥补了!