20140911 multi-condition Query

Source: Internet
Author: User

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 multi-condition Query
{
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;

}
}
}

 

 

For example, I can only perform step-by-step query on Fuzzy queries with multiple columns during the first query. I used or, but the concatenated string is too large (there are 10 column names)

The four method resource managers encapsulated by dataset sqlhelper and the multi-condition query above are learned today.

After several days of ADO learning, I gradually began to understand the role of the entity class. Each table corresponds to an entity class.

For the first time, I felt that my life was full of pressure. Although I was just moving towards this threshold, I can stick to it!

A cainiao. I am studying hard!

 

Resource Manager

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 Resource Manager
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}

Private void form1_load (Object sender, eventargs E)
{
// Obtain the heel Element
List <Category> List = GETID (-1 );
// Add the element to the node
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;
// Recursively traverse all the information in it
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) // get the TID that was just stored on it
{
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;
}
}
}
}
}

This is because I just learned a little bit difficult to do. Alas, people are a little stupid and can only make up for it by diligence!

20140911 multi-condition Query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.