// Name field corresponding to textbox1
// The password field corresponding to textbox2
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. Data. sqlclient;
Namespace findrow
{
Public partial class form1: Form
{
String SQL;
Public form1 ()
{
Initializecomponent ();
}
Private void button#click (Object sender, eventargs E)
{
If (textbox1.text. Trim (). Length = 0 & this. textbox1.text. Trim (). Length = 0)
{
MessageBox. Show ("incomplete data ");
}
Else
{
If (this. textbox1.text. Trim ()! = String. Empty)
{
SQL + = "and name = '" + textbox1.text + "'";
}
If (this. textbox2.text. Trim ()! = String. Empty)
{
SQL + = "and Password = '" + textbox2.text + "'";
}
A (SQL );
SQL = "";
}
}
Public void a (string SQL)
{
String strconn = "Data Source = (local); initial catalog = mydatabase; user id = sa; Password = lansoft ";
Sqlconnection conn = new sqlconnection (strconn );
Sqlcommand cmd = conn. createcommand ();
Cmd. commandtext = "select * From newtable where 1 = 1" + SQL;
Datatable dt = new datatable ();
Sqldataadapter ad = new sqldataadapter (CMD );
Ad. Fill (DT );
This. Maid = DT. defaultview;
// SQL = "";
}
}
}
This is a winform. With "where 1 = 1", I feel that the super-powerful function can implement queries with multiple conditions without a lot of code, that is, dynamically generate multiple where and .. and .. and SQL statement