Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. LINQ;
Using system. text;
Using system. Windows. forms;
Using system. Data. sqlclient;
Namespace meifengsystem
{
Public partial class shouhuoform: Form
{
Public form pform = NULL;
Public int mode = 0;
Sqlconnection conn = NULL;
String driver = "Server = 127.0.0.1; uid = sa; Pwd = admin; database = Factory ";
Sqldataadapter da;
Dataset DS = new dataset ();
// Select_all: unconditional search + where1 = 1
String SQL _select_all = "select S. ID, kehu_name, doctor_name, yamo_no, ya_type, Yawi, color, remark_id, remark_text, price, amount, yewuyuan, product_no, shouhuo_date, huanzhe, fangong_yuanyin from Shouhuo s left join remark R on S. remark_id = R. ID where
1 = 1 ";
Public shouhuoform ()
{
Initializecomponent ();
}
Private void shouhuoform_load (Object sender, eventargs E)
{
Initform ();
Dv1load ();
}
Private void bn_add_click (Object sender, eventargs E)
{
Addrowclick ();
}
Private void shouhuoform_formclosed (Object sender, formclosedeventargs E)
{
Formclose ();
}
Private void tb_key_textchanged (Object sender, eventargs E)
{
Mohusearch ();
}
Private void bn_search_all_click (Object sender, eventargs E)
{
Mohusearch ();
}
Private void bn_search_click (Object sender, eventargs E)
{
Search ();
}
Private void dt_start_valuechanged (Object sender, eventargs E)
{
Search ();
}
Private void bn_add_date_click (Object sender, eventargs E)
{
Showdate ();
Search ();
}
Private void bn_del_date_click (Object sender, eventargs E)
{
Hidedate ();
Search ();
}
Private void bn_delete_click (Object sender, eventargs E)
{
Dialogresult result = MessageBox. Show ("are you sure you want to delete the currently selected row? "," Delete ", messageboxbuttons. yesno, messageboxicon. Question );
If (result = dialogresult. Yes)
{
Deleterows ();
}
}
Private void bn_modify_click (Object sender, eventargs E)
{
Modifyclick ();
}
Private void tb_yamo_no_textchanged (Object sender, eventargs E)
{
Search ();
}
Private void dt_end_valuechanged (Object sender, eventargs E)
{
Search ();
}
Private void tb_kehu_name_textchanged (Object sender, eventargs E)
{
Search ();
}
Private void tb_huanzhe_textchanged (Object sender, eventargs E)
{
Search ();
}
Private void tb_yewuyuan_textchanged (Object sender, eventargs E)
{
Search ();
}
Private void bn_print_click (Object sender, eventargs E)
{
}
// ----------------- Common -----------------------------------
Public void formclose ()
{
This. pform. Show ();
This. Dispose ();
}
Public void mohusearch ()
{
Try
{
Conn = new sqlconnection (driver );
String sqlstr = "";
If (string. isnullorempty (tb_key.text.trim ()))
{
Sqlstr = "select S. ID, kehu_name, doctor_name, yamo_no, ya_type, Yawi, color, remark_id, remark_text, price, amount, yewuyuan, product_no, shouhuo_date, huanzhe, fangong_yuanyin from Shouhuo s left join remark R on S. remark_id = R. ID ";
}
Else
{// Like with no price or amount
Sqlstr = "select S. ID, kehu_name, doctor_name, yamo_no, ya_type, Yawi, color, remark_id, remark_text, price, amount, yewuyuan, product_no, shouhuo_date, huanzhe, fangong_yuanyin from Shouhuo s left join remark R on S. remark_id = R. ID"
+ "Where"
+ "Remark_text like '" + "%" + tb_key.text.trim () + "%" + "'"
+ "Or S. Id like '" + "%" + tb_key.text.trim () + "%" + "'"
+ "Or kehu_name like '" + "%" + tb_key.text.trim () + "%" + "'"
+ "Or doctor_name like '" + "%" + tb_key.text.trim () + "%" + "'"
+ "Or yamo_no like '" + "%" + tb_key.text.trim () + "%" + "'"
+ "Or ya_type like '" + "%" + tb_key.text.trim () + "%" + "'"
+ "Or Yawi like '" + "%" + tb_key.text.trim () + "%" + "'"
+ "Or color like '" + "%" + tb_key.text.trim () + "%" + "'"
+ "Or remark_id like '" + "%" + tb_key.text.trim () + "%" + "'"
+ "Or remark_text like '" + "%" + tb_key.text.trim () + "%" + "'"
+ "Or yewuyuan like '" + "%" + tb_key.text.trim () + "%" + "'"
+ "Or product_no like '" + "%" + tb_key.text.trim () + "%" + "'"
+ "Or shouhuo_date like '" + "%" + tb_key.text.trim () + "%" + "'"
+ "Or huanzhe like '" + "%" + tb_key.text.trim () + "%" + "'"
+ "Or fangong_yuanyin like '" + "%" + tb_key.text.trim () + "%" + "'";
}
DA = new sqldataadapter (sqlstr, Conn );
DS. Clear ();
Da. Fill (DS );
This. dv1.datasource = Ds. Tables [0]. defaultview;
}
Catch (exception ERR)
{
Console. writeline ("database read error" + err. Message );
}
Finally
{
Conn. Close ();
}
}
Public bool checkform ()
{
Bool flag = true;
Return flag;
}
// Search by options
Public void search ()
{
Try
{
Conn = new sqlconnection (driver );
String resultstr = addsearchoptions (SQL _select_all );
DA = new sqldataadapter (resultstr, Conn );
DS. Clear ();
Da. Fill (DS );
This. dv1.datasource = Ds. Tables [0]. defaultview;
}
Catch (exception ERR)
{
MessageBox. Show ("database error" + err. Message );
}
Finally
{
Conn. Close ();
}
}
Public void addrowclick ()
{
Shouhuoaddform = new shouhuoaddform ();
Shouhuoaddform. pform = this;
Shouhuoaddform. Show ();
}
Public void dv1load ()
{
Try
{
Conn = new sqlconnection (driver );
DA = new sqldataadapter (SQL _select_all, Conn );
DS. Clear ();
Da. Fill (DS );
This. dv1.datasource = Ds. Tables [0]. defaultview;
}
Catch (exception ERR)
{
MessageBox. Show ("database error" + err. Message );
}
Finally
{
Conn. Close ();
}
}
Public String addsearchoptions (string sqlstr)
{
// Add a condition for SQL based on whether it is empty
If (tb_yamo_no.text.trim ()! = "")
{
Sqlstr = sqlstr + "and yamo_no like '" + "%" + tb_yamo_no.text.trim () + "%" + "'";
}
If (tb_huanzhe.text.trim ()! = "")
{
Sqlstr = sqlstr + "and huanzhe like '" + "%" + tb_huanzhe.text.trim () + "%" + "'";
}
If (tb_kehu_name.text.trim ()! = "")
{
Sqlstr = sqlstr + "and kehu_name like '" + "%" + tb_kehu_name.text.trim () + "%" + "'";
}
If (tb_yewuyuan.text.trim ()! = "")
{
Sqlstr = sqlstr + "and yewuyuan like '" + "%" + tb_yewuyuan.text.trim () + "%" + "'";
}
// Time condition
If (dt_start.visible = true) & (dt_end.visible = true ))
{
Sqlstr = sqlstr + "and shouhuo_date between '" + dt_start.value.date + "' and '" + dt_end.value.date + "'";
}
Return sqlstr;
}
Public void showdate ()
{
Dt_start.visible = true;
Dt_end.visible = true;
Lb_fro.visible = true;
Lb_to.visible = true;
}
Public void hidedate ()
{
Dt_start.visible = false;
Dt_end.visible = false;
Lb_fro.visible = false;
Lb_to.visible = false;
}
Public void deleterows ()
{
For (INT I = This. dv1.selectedrows. Count; I> 0; I --)
{
String id = This. dv1.selectedrows [I-1]. cells [0]. value. tostring ();
This. dv1.rows. removeat (this. dv1.selectedrows [I-1]. Index );
String SQL _delete = "delete from Shouhuo where id = '" + ID + "'";
Int S = convert. toint32 (execute (SQL _delete); // Cl is an object of the operation class, and execute () is a method in the class
If (s! = 0)
{
MessageBox. Show ("selected row data is deleted successfully! ");
}
}
}
Public int execute (string SQL)
{
Conn = new sqlconnection (driver );
Sqlcommand cmd = new sqlcommand (SQL, Conn );
Int I = 0;
Try
{
Conn. open ();
I = convert. toint32 (CMD. executenonquery ());
}
Catch (exception ERR)
{
Console. writeline ("database read error" + err. Message );
}
Finally
{
Conn. Close ();
}
Return I;
}
Public void modifyclick ()
{
Shouhuomform mform = new shouhuomform ();
Mform. pform = this;
Try
{
Mform. PID = This. dv1.selectedrows [0]. cells [0]. value. tostring ();
}
Catch (exception ERR)
{
Console. writeline (ERR. Message );
}
Mform. Show ();
}
Public void initform ()
{
If (this. mode = 0)
{
Dt_start.visible = false;
Dt_end.visible = false;
Lb_fro.visible = false;
Lb_to.visible = false;
Bn_make_chuhuo.visible = false;
}
If (this. mode = 1)
{
Dt_start.visible = false;
Dt_end.visible = false;
Lb_fro.visible = false;
Lb_to.visible = false;
Bn_make_chuhuo.visible = true;
Bn_add.visible = false;
Bn_delete.visible = false;
Bn_modify.visible = false;
}
}
}
}