药品信息管理系统前端更新1

namespace 药品信息管理系统
{
    public partial class frm_Client : Form
    {
        public frm_Client()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;                                //本窗体启动位置设为屏幕中央; 
            this.Client = new Client(); 
        }
        private Client Client
        {
            get;
            set;
        }

        private void frm_Client_Load(object sender, EventArgs e)
        {
            
        }

       

        private void btn_Go_Click(object sender, EventArgs e)
        {
            frm_ClientTable frm_clientTable = new frm_ClientTable();
            
            frm_clientTable.ShowDialog(this);
            this.Hide();
        }


        private void btn_seek_Click_1(object sender, EventArgs e)
        {
            if (cb_LoadName.Checked)
            {
                this.txt_CName.Text = this.cob_Name.Text;
                using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
                {
                    SqlCommand sqlCommand1 = sqlConnection.CreateCommand();                             //调用SQL连接的方法CreateCommand来创建SQL命令;该命令将绑定SQL连接;
                    sqlCommand1.CommandText = "select * from tb_Client where CName in (select CName from tb_Client where CName like '%" + this.cob_Name.Text.ToString() + "%')";                                 //指定SQL命令的命令文本;命令文本为存储过程名称;
                    //sqlCommand1.CommandType = CommandType.StoredProcedure;                              //SQL命令的类型设为存储过程;
                    //sqlCommand1.Parameters.AddWithValue("@CNo", client.CNo);                                //向SQL命令的参数集合添加参数的名称、值;
                    //sqlCommand1.Parameters.AddWithValue("@CName", client.CName);
                    //sqlCommand1.Parameters.AddWithValue("@CTel", client.CTel);
                    //sqlCommand1.Parameters.AddWithValue("@CAddress", client.CAddress);
                    sqlConnection.Open();                                                               //打开SQL连接;
                    SqlDataReader sqlDataReader = sqlCommand1.ExecuteReader();
                    while (sqlDataReader.Read())
                    {

                        this.txt_CNo.Text = sqlDataReader["CNO"].ToString();
                        //this.txt_CName.Text = sqlDataReader["CName"].ToString();
                        this.txt_CTel.Text = sqlDataReader["CTel"].ToString();
                        this.txt_CAddr.Text = sqlDataReader["CAddress"].ToString();

                    }
                    sqlDataReader.Close();
                    sqlConnection.Close();
                }
 
            }
            else if (cb_LoadNo.Checked)
            {
                this.txt_CNo.Text = this.cob_ClientNo.Text;
                using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
                {                  //配置管理器从App.config读取连接字符串;
                    SqlCommand sqlCommand1 = sqlConnection.CreateCommand();
                    sqlCommand1.CommandText = "select * from tb_Client where CNo=@CNo";
                    sqlCommand1.Parameters.AddWithValue("@CNo", this.cob_ClientNo.Text.ToString());//this.txt_supplyname.Text.ToString()       //调用SQL连接的方法CreateCommand来创建SQL命令;该命令将绑定SQL连接;
                    //sqlCommand1.CommandText = "select * from tb_Client " + "where CNO='" + this.txt_Name.Text.ToString() + "'";                                    //指定SQL命令的命令文本;命令文本为存储过程名称;
                    //sqlCommand1.CommandType = CommandType.StoredProcedure;                              //SQL命令的类型设为存储过程;
                    //sqlCommand1.Parameters.AddWithValue("@CNo", client.CNo);                                //向SQL命令的参数集合添加参数的名称、值;
                    //sqlCommand1.Parameters.AddWithValue("@CName", client.CName);
                    //sqlCommand1.Parameters.AddWithValue("@CTel", client.CTel);
                    //sqlCommand1.Parameters.AddWithValue("@CAddress", client.CAddress);
                    sqlConnection.Open();                                                               //打开SQL连接;
                    SqlDataReader sqlDataReader = sqlCommand1.ExecuteReader();
                    while (sqlDataReader.Read())
                    {

                        //this.txt_CNo.Text = sqlDataReader["CNO"].ToString();
                        this.txt_CName.Text = sqlDataReader["CName"].ToString();
                        this.txt_CTel.Text = sqlDataReader["CTel"].ToString();
                        this.txt_CAddr.Text = sqlDataReader["CAddress"].ToString();

                    }
                    sqlDataReader.Close();
                    sqlConnection.Close();
                }
 
            }
            
        }

        
        private void btn_Add_Click_1(object sender, EventArgs e)
        {
            this.Client.CNo = this.txt_CNo.Text.Trim();                                         //将文本框的文本清除首尾的空格后,赋予用户的相应属性;
            this.Client.CName = this.txt_CName.Text.Trim();
            this.Client.CTel = this.txt_CTel.Text.Trim();
            this.Client.CAddress = this.txt_CAddr.Text.Trim();
            ClientBll.Add(this.Client);                                                          //调用业务逻辑层的静态方法,对用户执行注册操作;       
            MessageBox.Show(this.Client.Message); 
        }

        private void btn_alter_Click_1(object sender, EventArgs e)
        {
            using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
            {
                sqlConnection.Open(); //配置管理器从App.config读取连接字符串;
                // SqlCommand sqlCommand1 = sqlConnection.CreateCommand();                             //调用SQL连接的方法CreateCommand来创建SQL命令;该命令将绑定SQL连接;
                //sqlCommand1.CommandText = "update tb_Client set CName ='"+this.txt_CName .Text .ToString ()+"',CTel =' "+this .txt_CTel .Text .ToString ()+"' ,CAddress =' "+this .txt_CAddr .Text .ToString ()+"'where CNO =' "+this.txt_CNo .Text .ToString ()+"';";                                 //指定SQL命令的命令文本;命令文本为存储过程名称;
                //sqlCommand1.CommandType = CommandType.StoredProcedure;                              //SQL命令的类型设为存储过程;
                //sqlCommand1.Parameters.AddWithValue("@CNo", client.CNo);                                //向SQL命令的参数集合添加参数的名称、值;
                //sqlCommand1.Parameters.AddWithValue("@CName", client.CName);
                //sqlCommand1.Parameters.AddWithValue("@CTel", client.CTel);
                //sqlCommand1.Parameters.AddWithValue("@CAddress", client.CAddress);
                string str = "update tb_Client set CName='" + this.txt_CName.Text + "',CTel='" + this.txt_CTel.Text + "',CAddress='" + this.txt_CAddr.Text + "' where CNO='" + this.txt_CNo .Text + "'";
                SqlCommand sqlCommand1 = new SqlCommand(str, sqlConnection);
                //sqlConnection.Open();                                                               //打开SQL连接;
                int i = sqlCommand1.ExecuteNonQuery();
                if (i == 1)
                {
                    MessageBox.Show("OK");
                }
                else
                    MessageBox.Show("not OK");
                sqlConnection.Close();
            }
        }

        private void btn_Delete_Click_1(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认删除编号为"+txt_CNo .Text +"的数据?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
                {
                    sqlConnection.Open(); //配置管理器从App.config读取连接字符串;
                    // SqlCommand sqlCommand1 = sqlConnection.CreateCommand();                             //调用SQL连接的方法CreateCommand来创建SQL命令;该命令将绑定SQL连接;
                    //sqlCommand1.CommandText = "update tb_Client set CName ='"+this.txt_CName .Text .ToString ()+"',CTel =' "+this .txt_CTel .Text .ToString ()+"' ,CAddress =' "+this .txt_CAddr .Text .ToString ()+"'where CNO =' "+this.txt_CNo .Text .ToString ()+"';";                                 //指定SQL命令的命令文本;命令文本为存储过程名称;
                    //sqlCommand1.CommandType = CommandType.StoredProcedure;                              //SQL命令的类型设为存储过程;
                    //sqlCommand1.Parameters.AddWithValue("@CNo", client.CNo);                                //向SQL命令的参数集合添加参数的名称、值;
                    //sqlCommand1.Parameters.AddWithValue("@CName", client.CName);
                    //sqlCommand1.Parameters.AddWithValue("@CTel", client.CTel);
                    //sqlCommand1.Parameters.AddWithValue("@CAddress", client.CAddress);
                    string str = "delete from tb_Client where CNO='" + this.txt_CNo .Text + "'";
                    SqlCommand sqlCommand1 = new SqlCommand(str, sqlConnection);
                    //sqlConnection.Open();                                                               //打开SQL连接;
                    int i = sqlCommand1.ExecuteNonQuery();
                    if (i == 1)
                    {
                        MessageBox.Show("OK");
                    }
                    else
                        MessageBox.Show("not OK");
                    sqlConnection.Close();
                    sqlConnection.Close();
                }
                cob_ClientNo.Text = "";
                cob_Name.Text = "";
                txt_CAddr.Text = "";
                txt_CName.Text = "";
                txt_CNo.Text = "";
                txt_CTel.Text = "";
            }
            
            
            
            
        }

        

     

        private void cb_LoadNo_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_LoadNo.Checked == false)
            {
                if (cb_LoadName.Checked == false)
                {
                    cob_Name.Text = "";
                    cob_Name.Text = "";
                }

            }
            if (cb_LoadNo.Checked)
            {
                cb_LoadName.Checked = false;
                txt_CName.ReadOnly = false;
                cob_Name.Text = "";
                using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
                {
                    SqlCommand sqlCommand = sqlConnection.CreateCommand();                             //调用SQL连接的方法CreateCommand来创建SQL命令;该命令将绑定SQL连接;
                    sqlCommand.CommandText = "select CNo  from tb_Client";
                    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                           //声明并实例化SQL数据适配器,同时借助构造函数,将其SelectCommand属性设为先前创建的SQL命令;
                    sqlDataAdapter.SelectCommand = sqlCommand;                                                      //将SQL数据适配器的查询命令属性指向SQL命令;
                    DataTable ClientTable = new DataTable();
                    sqlConnection.Open();
                    sqlDataAdapter.Fill(ClientTable);                                                                //SQL数据适配器读取数据,并填充班级数据表;
                    this.cob_ClientNo.DataSource = ClientTable;                                                         //将班级下拉框的数据源设为班级数据表;
                    this.cob_ClientNo.DisplayMember = "CNo";                                                          //将班级下拉框的显示成员设为班级数据表的名称列;
                    this.cob_ClientNo.ValueMember = "CNo";
                    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                    while (sqlDataReader.Read())
                    {

                        this.cob_ClientNo.Text = sqlDataReader["CNo"].ToString();

                    }
                    sqlDataReader.Close();
                    sqlConnection.Close();

                }

            }
        }

        private void cb_LoadName_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_LoadNo.Checked==false   )
            {
                if (cb_LoadName.Checked ==false )
                {
                    cob_Name.Text = "";
                    cob_Name.Text = "";
                }
 
            }
            if (cb_LoadName.Checked)
            {
                cb_LoadNo.Checked = false;
                cob_ClientNo.Text = "";
                //txt_CName.ReadOnly = true;

                using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
                {
                    SqlCommand sqlCommand = sqlConnection.CreateCommand();                             //调用SQL连接的方法CreateCommand来创建SQL命令;该命令将绑定SQL连接;
                    sqlCommand.CommandText = "select CName  from tb_Client";
                    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                           //声明并实例化SQL数据适配器,同时借助构造函数,将其SelectCommand属性设为先前创建的SQL命令;
                    sqlDataAdapter.SelectCommand = sqlCommand;                                                      //将SQL数据适配器的查询命令属性指向SQL命令;
                    DataTable ClientTable = new DataTable();
                    sqlConnection.Open();
                    sqlDataAdapter.Fill(ClientTable);                                                                //SQL数据适配器读取数据,并填充班级数据表;
                    this.cob_Name.DataSource = ClientTable;                                                         //将班级下拉框的数据源设为班级数据表;
                    this.cob_Name.DisplayMember = "CName";                                                          //将班级下拉框的显示成员设为班级数据表的名称列;
                    this.cob_Name.ValueMember = "CName";
                    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                    while (sqlDataReader.Read())
                    {

                        this.cob_Name.Text = sqlDataReader["CName"].ToString();

                    }
                    sqlDataReader.Close();
                    sqlConnection.Close();

                }

            }
        }

        private void btn_Load_Click(object sender, EventArgs e)
        {
            cob_ClientNo.Text = "";
            cob_Name.Text = "";
            txt_CAddr.Text = "";
            txt_CName.Text = "";
            txt_CNo.Text = "";
            txt_CTel.Text = "";
        }
    }
}

namespace 药品信息管理系统
{
    public partial class frm_ClientTable : Form
    {
        public frm_ClientTable()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;  
        }

        private void frm_ClientTable_Load(object sender, EventArgs e)
        {

        }

        private void btn_Load_Click(object sender, EventArgs e)
        {
            using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
            {
                SqlCommand sqlCommand = new SqlCommand();
                                                                      //声明并实例化SQL命令;
                sqlCommand.Connection = sqlConnection;
                                                                       //将SQL命令的连接属性指向SQL连接;
                sqlCommand.CommandText = "SELECT * FROM tb_Client;";
                                                         //指定SQL命令的命令文本;该命令查询所有学生;
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                           //声明并实例化SQL数据适配器;
                sqlDataAdapter.SelectCommand = sqlCommand;                                                      //将SQL数据适配器的查询命令属性指向SQL命令;
                DataTable ClientTable = new DataTable();
                                                                     //声明并实例化数据表,用于保存所有学生,以用作数据网格视图的数据源;
                sqlConnection.Open();                                                                           //打开SQL连接;
                sqlDataAdapter.Fill(ClientTable);
                                                                           //SQL数据适配器读取数据,并填充学生数据表;
                sqlConnection.Close();                                                                          //关闭SQL连接;
                this.dgv_Client.Columns.Clear();                                                                 //数据网格视图的列集合清空;
                this.dgv_Client.DataSource = ClientTable;                                                       //将数据网格视图的数据源设为学生数据表;
                this.dgv_Client.Columns["CNo"].HeaderText = "客户编号";                                               //将数据网格视图的指定列的表头文本设为中文;
                this.dgv_Client.Columns["CName"].HeaderText = "客户名称";
                this.dgv_Client.Columns["CAddress"].HeaderText = "地址";
                this.dgv_Client.Columns["CTel"].HeaderText = "联系电话";
                this.dgv_Client.Columns["CName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                this.dgv_Client.Columns[this.dgv_Client.Columns.Count - 1].AutoSizeMode =                         //数据网格视图的最后一列的自动调整列宽模式设为填充(至数据网格视图右侧边缘);
                    DataGridViewAutoSizeColumnMode.Fill;

            }
        }

        private void btn_Update_Click(object sender, EventArgs e)
        {
            using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
            {
                SqlCommand sqlCommand = new SqlCommand();                                                       //声明并实例化SQL命令;
                sqlCommand.Connection = sqlConnection;                                                          //将SQL命令的连接属性指向SQL连接;
                sqlCommand.CommandText =                                                                        //指定SQL命令的命令文本;
                    "UPDATE tb_Client"
                    + " SET CName=@CName,CAddress=@CAddress,CTel=@CTel"
                    + " WHERE CNo=@CNo;";





                sqlCommand.Parameters.Add("@CName", SqlDbType.VarChar, 0, "CName");                                  //向SQL命令的参数集合添加参数的名称、SQL Server数据类型、长度(仅用于定长类型)、所绑定的数据表中的列名;
                sqlCommand.Parameters.Add("@CAddress", SqlDbType.VarChar, 0, "CAddress");
                sqlCommand.Parameters.Add("@CTel", SqlDbType.VarChar, 0, "CTel");
                sqlCommand.Parameters.Add("@CNo", SqlDbType.Char, 10, "CNo");





                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                           //声明并实例化SQL数据适配器,同时借助构造函数,将其SelectCommand属性设为先前创建的SQL命令;
                sqlDataAdapter.UpdateCommand = sqlCommand;                                                      //将SQL数据适配器的更新命令属性指向SQL命令;
                DataTable ClientTable = (DataTable)this.dgv_Client.DataSource;





                //声明数据表,并指向数据网格视图的数据源;数据源默认类型为object,还需强制转换类型;
                sqlConnection.Open();

                //打开SQL连接;
                int rowAffected = sqlDataAdapter.Update(ClientTable);
                sqlConnection.Close();
                if (rowAffected > 0)
                {
                    MessageBox.Show("更新" + rowAffected.ToString() + "行。");
                }
                else
                {
                    MessageBox.Show("not ok");
                }
                
                //SQL数据适配器根据学生数据表提交更新,并返回受影响行数;

            }
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
           
            //未完成
            using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
            {
                SqlCommand sqlCommand = new SqlCommand();                                                       //声明并实例化SQL命令;
                sqlCommand.Connection = sqlConnection;                                                          //将SQL命令的连接属性指向SQL连接;
                sqlCommand.CommandText =                                                                        //指定SQL命令的命令文本;
                   "insert tb_Client (CNo,CName,CTel,CAddress)values"
                    + "(@CNo,@CName,@CTel,@CAddress)";





                sqlCommand.Parameters.Add("@CName", SqlDbType.VarChar, 0, "CName");                                  //向SQL命令的参数集合添加参数的名称、SQL Server数据类型、长度(仅用于定长类型)、所绑定的数据表中的列名;
                sqlCommand.Parameters.Add("@CAddress", SqlDbType.VarChar, 0, "CAddress");
                sqlCommand.Parameters.Add("@CTel", SqlDbType.VarChar, 0, "CTel");
                sqlCommand.Parameters.Add("@CNo", SqlDbType.Char, 10, "CNo");





                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                           //声明并实例化SQL数据适配器,同时借助构造函数,将其SelectCommand属性设为先前创建的SQL命令;
                sqlDataAdapter.UpdateCommand = sqlCommand;                                                      //将SQL数据适配器的更新命令属性指向SQL命令;
                DataTable ClientTable = (DataTable)this.dgv_Client.DataSource;





                //声明数据表,并指向数据网格视图的数据源;数据源默认类型为object,还需强制转换类型;
                sqlConnection.Open();


                //打开SQL连接;
                int i = sqlCommand.ExecuteNonQuery();
                if (i == 1)
                {
                    MessageBox.Show("OK");
                }
                else
                    MessageBox.Show("not OK");
                //int rowAffected = sqlDataAdapter.Update(ClientTable);
                sqlConnection.Close();
                MessageBox.Show("添加" + i.ToString() + "行。");
                //SQL数据适配器根据学生数据表提交更新,并返回受影响行数;

            }
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {
           
            
             
             
       

            //未完成
            using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
            {
                SqlCommand sqlCommand = new SqlCommand();                                                       //声明并实例化SQL命令;
                sqlCommand.Connection = sqlConnection;                                                          //将SQL命令的连接属性指向SQL连接;
                sqlCommand.CommandText =                                                                        //指定SQL命令的命令文本;
                    "delete from tb_Client"
                    + " WHERE CName='"+this.dgv_Client .CurrentRow .Cells [1].ToString ()+"'";;
                
                //sqlCommand.Parameters.Add("@CName", SqlDbType.VarChar, 0, "CName");                                  //向SQL命令的参数集合添加参数的名称、SQL Server数据类型、长度(仅用于定长类型)、所绑定的数据表中的列名;
                //sqlCommand.Parameters.Add("@CAddress", SqlDbType.VarChar, 0, "CAddress");
                //sqlCommand.Parameters.Add("@CTel", SqlDbType.VarChar, 0, "CTel");
                //sqlCommand.Parameters.Add("@CNo", SqlDbType.Char, 10, "CNo");





                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                           //声明并实例化SQL数据适配器,同时借助构造函数,将其SelectCommand属性设为先前创建的SQL命令;
                sqlDataAdapter.UpdateCommand = sqlCommand;                                                      //将SQL数据适配器的更新命令属性指向SQL命令;
                DataTable ClientTable = (DataTable)this.dgv_Client.DataSource;





                //声明数据表,并指向数据网格视图的数据源;数据源默认类型为object,还需强制转换类型;
                sqlConnection.Open();

                //打开SQL连接;
                int i = sqlCommand.ExecuteNonQuery();
                if (i >0)
                {
                    MessageBox.Show("OK");
                }
                else
                    MessageBox.Show("not OK");
                //int rowAffected = sqlDataAdapter.Update(ClientTable);
                sqlConnection.Close();
                MessageBox.Show("删除" + i.ToString() + "行。");
                //SQL数据适配器根据学生数据表提交更新,并返回受影响行数;

            }
        }
    }
}

namespace 药品信息管理系统
{
    public partial class medicine : Form
    {
        private string PhotoFileName;
        public medicine()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;                                            //本窗体启动位置设为屏幕中央;
           
        }


        private void medicine_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“medicineDataSet1.tb_medicine”中。您可以根据需要移动或移除它。
            //this.tb_medicineTableAdapter.Fill(this.medicineDataSet1.tb_medicine);
      
            using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
                {
                    SqlCommand sqlCommand2 = new SqlCommand();
                    SqlCommand sqlCommand = new SqlCommand();
                    sqlCommand2.Connection = sqlConnection;
                    sqlCommand.Connection = sqlConnection;
                    sqlCommand2.CommandText = "select * from tb_Supply";
                    sqlCommand.CommandText = "select * from Tb_Stock";
                    SqlDataAdapter sqlDataAdapter2 = new SqlDataAdapter();
                    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();//声明并实例化SQL数据适配器,同时借助构造函数,将其SelectCommand属性设为先前创建的SQL命令;
                    sqlDataAdapter2.SelectCommand = sqlCommand2;
                    sqlDataAdapter.SelectCommand = sqlCommand;//将SQL数据适配器的查询命令属性指向SQL命令;
                    DataTable SupplyTable = new DataTable();
                    DataTable StockTable = new DataTable();
                    sqlConnection.Open();
                    sqlDataAdapter2.Fill(SupplyTable);                                                                //SQL数据适配器读取数据,并填充班级数据表;
                    this.cob_Supply.DataSource = SupplyTable;                                                         //将班级下拉框的数据源设为班级数据表;
                    this.cob_Supply.DisplayMember = "SupplyName";                                                          //将班级下拉框的显示成员设为班级数据表的名称列;
                    this.cob_Supply.ValueMember = "SupplyNo";
                    SqlDataReader sqlDataReader2 = sqlCommand2.ExecuteReader();
                    while (sqlDataReader2.Read())
                    {
                        this.cob_Supply.SelectedValue = sqlDataReader2["SupplyNo"];
 
                    }
                    sqlDataReader2.Close();
                    sqlDataAdapter.Fill(StockTable);                                                                //SQL数据适配器读取数据,并填充班级数据表;
                    this.cob_Stock.DataSource = StockTable;                                                         //将班级下拉框的数据源设为班级数据表;
                    this.cob_Stock.DisplayMember = "StockName";                                                          //将班级下拉框的显示成员设为班级数据表的名称列;
                    this.cob_Stock.ValueMember = "StockNo";
                    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                    while (sqlDataReader.Read())
                    {
                        this.cob_Stock.SelectedValue = sqlDataReader["StockNo"];

                    }
                    sqlDataReader.Close();
                    sqlConnection.Close();
            }

        }

   

        private void btn_update1_Click(object sender, EventArgs e)
        {
            MemoryStream memoryStream = new MemoryStream();                                                 //声明并实例化内存流,用于读取照片的字节数据;
            this.ptb_Photo.Image.Save(memoryStream, ImageFormat.Bmp);                                       //调用图像框的图像的静态方法Save,将图像保存至内存流;
            byte[] photoBytes = new byte[memoryStream.Length];                                              //声明并实例化字节数组,用于保存照片数据;数组长度对应内存流长度;
            memoryStream.Seek(0, SeekOrigin.Begin);                                                         //保存后的内存流的偏移位置在末尾,需通过查找来将偏移位置设为起始;
            memoryStream.Read(photoBytes, 0, photoBytes.Length);  
            using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
            {
                SqlCommand sqlCommand = new SqlCommand();                                                       //声明并实例化SQL命令;
                sqlCommand.Connection = sqlConnection;                                                          //将SQL命令的连接属性指向SQL连接;
                sqlCommand.CommandText =                                                                        //指定SQL命令的命令文本;
                    "UPDATE tb_medicine"
                    + " SET MName=@MName,IsOTC=@IsOTC,MNum=@MNum,Norm=@Norm,MPrice=@MPrice,ValidDate=@ValidDate,SupplyNo=@SupplyNo,StockNo=@StockNo,Instuction=@Instuction,Photo=@Photo"
                    + " WHERE MNo=@MNo;";
                sqlCommand.Parameters.AddWithValue("@MName", this.txt_MName .Text.Trim());
                sqlCommand.Parameters.AddWithValue("@IsOTC", this.rdb_OTC.Checked);//向SQL命令的参数集合添加参数的名称、值;
                sqlCommand.Parameters.AddWithValue("@MNum", this.txt_MNum .Text .Trim ());
                sqlCommand.Parameters.AddWithValue("@Norm", this.txt_norm.Text.Trim());
                sqlCommand.Parameters.AddWithValue("@MPrice", this.txt_MPrice.Text.Trim());
                sqlCommand.Parameters.AddWithValue("@ValidDate", this.dtp_ValidDate.Value);
                sqlCommand.Parameters.AddWithValue("@SupplyNo", this.cob_Supply.SelectedValue);
                sqlCommand.Parameters.AddWithValue("@StockNo", this.cob_Stock.SelectedValue);
                sqlCommand.Parameters.AddWithValue("@Instuction", this.txt_Instruction.Text.Trim());
                sqlCommand.Parameters.AddWithValue("@Photo", photoBytes);
                sqlCommand.Parameters.AddWithValue("@MNo", this.txt_MNo .Text.Trim () );
                sqlConnection.Open();                                                                           //打开SQL连接;
                int rowAffected = sqlCommand.ExecuteNonQuery();                                                 //调用SQL命令的方法ExecuteNonQuery来执行命令,向数据库写入数据,并返回受影响行数;
                sqlConnection.Close();                                                                          //关闭SQL连接;
                MessageBox.Show("更新" + rowAffected.ToString() + "行。"); 
            }

        }

        private void btn_Go_Click(object sender, EventArgs e)
        {
            frm_MedicineTable frm_medicineTable = new frm_MedicineTable();
            frm_medicineTable.ShowDialog(this);
            this.Hide();
        }


        private void btn_OpenPhoto_Click(object sender, EventArgs e)
        {
            OpenFileDialog openPhotoDialog = new OpenFileDialog()                                           //声明并实例化打开文件对话框;
            {                                                                                           //在初始化器中,设置打开文件对话框的各属性;
                Title = "打开照片文件(位图格式)"                                                      //对话框标题;
                ,
                Filter = "BMP Files (*.bmp)|*.bmp"                                                    //文件格式过滤器;
                ,
                InitialDirectory = @"C:\"                                                             //初始目录;
            };
            if (openPhotoDialog.ShowDialog() == DialogResult.OK)                                            //显示打开文件对话框,若打开文件对话框的对话结果为点击OK键;
            {
                this.PhotoFileName = openPhotoDialog.FileName;                                              //将对话框获得的文件名,存入本窗体的私有字段中;
                this.ptb_Photo.Image = Image.FromFile(this.PhotoFileName);                                  //调用图像的静态方法FromFile从指定文件中读取图像,并赋予图像框;
            }
        }


        private void btn_Add_Click(object sender, EventArgs e)
        {
            MemoryStream memoryStream = new MemoryStream();                                                 //声明并实例化内存流,用于读取照片的字节数据;
            this.ptb_Photo.Image.Save(memoryStream, ImageFormat.Bmp);                                       //调用图像框的图像的静态方法Save,将图像保存至内存流;
            byte[] photoBytes = new byte[memoryStream.Length];                                              //声明并实例化字节数组,用于保存照片数据;数组长度对应内存流长度;
            memoryStream.Seek(0, SeekOrigin.Begin);                                                         //保存后的内存流的偏移位置在末尾,需通过查找来将偏移位置设为起始;
            memoryStream.Read(photoBytes, 0, photoBytes.Length);
            using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
            {
                SqlCommand sqlCommand = new SqlCommand();                                                       //声明并实例化SQL命令;
                sqlCommand.Connection = sqlConnection;                                                          //将SQL命令的连接属性指向SQL连接;
                sqlCommand.CommandText =                                                                        //指定SQL命令的命令文本;
                    "insert tb_medicine "
                    + " (MNo,MName,IsOTC,MNum,Norm,MPrice,ValidDate,SupplyNo,StockNo,Instuction ,Photo) "
                    + " values (@MNo,@MName,@IsOTC,@MNum,@Norm,@MPrice,@ValidDate,@SupplyNo,@StockNo,@Instuction ,@Photo);";
                sqlCommand.Parameters.AddWithValue("@MName", this.txt_MName.Text.Trim());                         //向SQL命令的参数集合添加参数的名称、值;
                sqlCommand.Parameters.AddWithValue("@IsOTC", this.rdb_OTC.Checked);
                sqlCommand.Parameters.AddWithValue("@MNum", this.txt_MNum.Text.Trim());
                sqlCommand.Parameters.AddWithValue("@Norm", this.txt_norm.Text.Trim());
                sqlCommand.Parameters.AddWithValue("@MPrice", this.txt_MPrice.Text.Trim());
                sqlCommand.Parameters.AddWithValue("@ValidDate", this.dtp_ValidDate.Value);
                sqlCommand.Parameters.AddWithValue("@SupplyNo", this.cob_Supply.SelectedValue);
                sqlCommand.Parameters.AddWithValue("@StockNo", this.cob_Stock.SelectedValue);
                sqlCommand.Parameters.AddWithValue("@Instuction", this.txt_Instruction.Text.Trim());
                sqlCommand.Parameters.AddWithValue("@Photo", photoBytes);
                sqlCommand.Parameters.AddWithValue("@MNo", this.txt_MNo  .Text.Trim());
                sqlConnection.Open();
                int i = 0;                                                                //声明整型变量,用于保存受影响行数
                try                                                                                 //尝试;
                {
                    i = sqlCommand.ExecuteNonQuery();                                     //调用SQL命令的方法ExecuteNonQuery来执行命令,向数据库写入数据,并返回受影响行数;
                }
                catch (SqlException sqlEx)                                                          //捕捉SQL异常;
                {
                    if (sqlEx.Number == 2627)                                                       //若异常的编号为2627,则违反实体完整性,即插入了主键重复的记录;
                    {
                        i = 0;
                        MessageBox.Show("药品已存在");//存在雷同用户;
                    }
                    else
                    {
                        throw sqlEx;
                    }
                }//打开SQL连接;
                                                               //调用SQL命令的方法ExecuteNonQuery来执行命令,向数据库写入数据,并返回受影响行数;
                sqlConnection.Close();                                                                          //关闭SQL连接;
                MessageBox.Show("添加" + i.ToString() + "行。");
            }
           
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {
            //MemoryStream memoryStream = new MemoryStream();                                                 //声明并实例化内存流,用于读取照片的字节数据;
            //this.ptb_Photo.Image.Save(memoryStream, ImageFormat.Bmp);                                       //调用图像框的图像的静态方法Save,将图像保存至内存流;
            //byte[] photoBytes = new byte[memoryStream.Length];                                              //声明并实例化字节数组,用于保存照片数据;数组长度对应内存流长度;
            //memoryStream.Seek(0, SeekOrigin.Begin);                                                         //保存后的内存流的偏移位置在末尾,需通过查找来将偏移位置设为起始;
            //memoryStream.Read(photoBytes, 0, photoBytes.Length);
            if (MessageBox.Show("确认删除编号为" + txt_MNo .Text  + "的数据?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
                {
                    SqlCommand sqlCommand = new SqlCommand();                                                       //声明并实例化SQL命令;
                    sqlCommand.Connection = sqlConnection;                                                          //将SQL命令的连接属性指向SQL连接;
                    sqlCommand.CommandText =                                                                        //指定SQL命令的命令文本;
                        "Delete tb_medicine"
                        + " WHERE MNo=@MNo;";

                    sqlCommand.Parameters.AddWithValue("@MNo", this.txt_MNo .Text .Trim());
                    sqlConnection.Open();                                                                           //打开SQL连接;
                    int rowAffected = sqlCommand.ExecuteNonQuery();                                                 //调用SQL命令的方法ExecuteNonQuery来执行命令,向数据库写入数据,并返回受影响行数;
                    sqlConnection.Close();                                                                          //关闭SQL连接;
                    MessageBox.Show("删除" + rowAffected.ToString() + "行。");
                }
                txt_Instruction.Clear();
                txt_MName.Clear();
                txt_MNo.Clear();
                txt_MNum.Clear();
                txt_MPrice.Clear();
                txt_norm.Clear();
                rdb_OTC.Checked = false;
                rdb_Rx.Checked = false;
                ptb_Photo.Image = null;
 
            }
           

        }

        private void cb_LoadNo_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_LoadName.Checked == false && cb_LoadNo.Checked == false)
            {
                cob_medicine.Text = "";
                cob_name.Text = "";
            }
            if (cb_LoadNo.Checked)
            {
                cb_LoadName.Checked = false;
                txt_MName.ReadOnly = false;
                cob_name.Text = "";
                using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
                {
                   
                    SqlCommand sqlCommand4 = sqlConnection.CreateCommand();                             //调用SQL连接的方法CreateCommand来创建SQL命令;该命令将绑定SQL连接;
                    sqlCommand4.CommandText = "select MNo from tb_medicine";
                    SqlDataAdapter sqlDataAdapter4 = new SqlDataAdapter();                                           //声明并实例化SQL数据适配器,同时借助构造函数,将其SelectCommand属性设为先前创建的SQL命令;
                    sqlDataAdapter4.SelectCommand = sqlCommand4;                                                      //将SQL数据适配器的查询命令属性指向SQL命令;
                    DataTable medicineTable = new DataTable();
                    sqlConnection.Open();
                    sqlDataAdapter4.Fill(medicineTable);                                                                //SQL数据适配器读取数据,并填充班级数据表;
                    this.cob_medicine.DataSource = medicineTable;                                                         //将班级下拉框的数据源设为班级数据表;
                    this.cob_medicine.DisplayMember = "MNo";                                                          //将班级下拉框的显示成员设为班级数据表的名称列;
                    this.cob_medicine.ValueMember = "MNo";
                    SqlDataReader sqlDataReader4 = sqlCommand4.ExecuteReader();
                    while (sqlDataReader4.Read())
                    {

                        this.cob_medicine.Text = sqlDataReader4["MNo"].ToString();

                    }
                    sqlDataReader4.Close();
                    sqlConnection.Close();

                }
            }
        }

        private void cb_LoadName_CheckedChanged(object sender, EventArgs e)
        {

            if (cb_LoadName.Checked == false && cb_LoadNo.Checked == false)
            {
                cob_medicine.Text = "";
                cob_name.Text = "";
            }
            if (cb_LoadName.Checked)
            {
                cb_LoadNo.Checked = false;
                //txt_MName.ReadOnly = true;
                cob_medicine.Text = "";
                using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
                {
                    SqlCommand sqlCommand = sqlConnection.CreateCommand();
                    SqlCommand sqlCommand4 = sqlConnection.CreateCommand();
                    sqlCommand.CommandText = "select ";//调用SQL连接的方法CreateCommand来创建SQL命令;该命令将绑定SQL连接;
                    sqlCommand4.CommandText = "select MName from tb_medicine";
                    SqlDataAdapter sqlDataAdapter4 = new SqlDataAdapter();                                           //声明并实例化SQL数据适配器,同时借助构造函数,将其SelectCommand属性设为先前创建的SQL命令;
                    sqlDataAdapter4.SelectCommand = sqlCommand4;                                                      //将SQL数据适配器的查询命令属性指向SQL命令;
                    DataTable medicineTable = new DataTable();
                    sqlConnection.Open();
                    sqlDataAdapter4.Fill(medicineTable);                                                                //SQL数据适配器读取数据,并填充班级数据表;
                    this.cob_name.DataSource = medicineTable;                                                         //将班级下拉框的数据源设为班级数据表;
                    this.cob_name.DisplayMember = "MName";                                                          //将班级下拉框的显示成员设为班级数据表的名称列;
                    this.cob_name.ValueMember = "MName";
                    SqlDataReader sqlDataReader4 = sqlCommand4.ExecuteReader();
                    while (sqlDataReader4.Read())
                    {

                        this.cob_name.Text = sqlDataReader4["MName"].ToString();

                    }
                    sqlDataReader4.Close();
                    sqlConnection.Close();

                }

            }
        }

        private void btn_SEEK_Click(object sender, EventArgs e)
        {
            if(cb_LoadNo .Checked )
            {
                this.txt_MNo.Text = this.cob_medicine.Text;
                using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
                {
                    SqlCommand sqlCommand1 = sqlConnection.CreateCommand();                             //调用SQL连接的方法CreateCommand来创建SQL命令;该命令将绑定SQL连接;
                    sqlCommand1.CommandText = "select * from tb_medicine where MNo=@MNo";
                    sqlCommand1.Parameters.AddWithValue("@MNo", this.cob_medicine.Text.ToString());//                             //指定SQL命令的命令文本;命令文本为存储过程名称;
                    //sqlCommand1.CommandType = CommandType.StoredProcedure;                              //SQL命令的类型设为存储过程;
                    SqlCommand sqlCommand2 = new SqlCommand();
                    sqlCommand2.Connection = sqlConnection;
                    sqlCommand2.CommandText = "select * from tb_Supply";
                    SqlCommand sqlCommand3 = new SqlCommand();
                    sqlCommand3.Connection = sqlConnection;
                    sqlCommand3.CommandText = "select * from Tb_Stock";
                    //sqlCommand1.Parameters.AddWithValue("@MNo", this .txt_MNo .Text .ToString ());
                    //SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter();
                    //sqlDataAdapter1.SelectCommand = sqlCommand1;
                    //DataTable medicinetable = new DataTable();
                    SqlDataAdapter sqlDataAdapter2 = new SqlDataAdapter();                                           //声明并实例化SQL数据适配器,同时借助构造函数,将其SelectCommand属性设为先前创建的SQL命令;
                    sqlDataAdapter2.SelectCommand = sqlCommand2;                                                      //将SQL数据适配器的查询命令属性指向SQL命令;
                    DataTable SupplyTable = new DataTable();
                    SqlDataAdapter sqlDataAdapter3 = new SqlDataAdapter();                                           //声明并实例化SQL数据适配器,同时借助构造函数,将其SelectCommand属性设为先前创建的SQL命令;
                    sqlDataAdapter3.SelectCommand = sqlCommand3;                                                      //将SQL数据适配器的查询命令属性指向SQL命令;
                    DataTable StockTable = new DataTable();//向SQL命令的参数集合添加参数的名称、值;
                    sqlConnection.Open();
                    //sqlDataAdapter1 .Fill (medicinetable );
                    //this.cob_medicine.DataSource = medicinetable;                                                         //将班级下拉框的数据源设为班级数据表;
                    //this.cob_medicine.DisplayMember = "MName";                                                          //将班级下拉框的显示成员设为班级数据表的名称列;
                    //this.cob_Supply.ValueMember = "MNo";
                    sqlDataAdapter2.Fill(SupplyTable);                                                                //SQL数据适配器读取数据,并填充班级数据表;
                    this.cob_Supply.DataSource = SupplyTable;                                                         //将班级下拉框的数据源设为班级数据表;
                    this.cob_Supply.DisplayMember = "SupplyName";                                                          //将班级下拉框的显示成员设为班级数据表的名称列;
                    this.cob_Supply.ValueMember = "SupplyNo";
                    sqlDataAdapter3.Fill(StockTable);                                                                //SQL数据适配器读取数据,并填充班级数据表;
                    this.cob_Stock.DataSource = StockTable;                                                         //将班级下拉框的数据源设为班级数据表;
                    this.cob_Stock.DisplayMember = "StockName";                                                          //将班级下拉框的显示成员设为班级数据表的名称列;
                    this.cob_Stock.ValueMember = "StockNo";

                    SqlDataReader sqlDataReader = sqlCommand1.ExecuteReader();
                    byte[] photoBytes = null;
                    while (sqlDataReader.Read())
                    {

                        this.txt_MName.Text = sqlDataReader["MName"].ToString();
                        //this.rdb_OTC.Checked = (bool)sqlDataReader["IsOTC"];
                        this.txt_MPrice.Text = sqlDataReader["MPrice"].ToString();
                        this.txt_MNum.Text = sqlDataReader["MNum"].ToString();
                        this.txt_norm.Text = sqlDataReader["Norm"].ToString();
                        this.dtp_ValidDate.Value = (DateTime)sqlDataReader["ValidDate"];
                        this.cob_Supply.SelectedValue = sqlDataReader["SupplyNo"];
                        this.cob_Stock.SelectedValue = sqlDataReader["StockNo"];
                        
                        this.txt_Instruction.Text = sqlDataReader["Instuction"].ToString();
                        photoBytes =
                        (sqlDataReader["Photo"] == DBNull.Value ? null : (byte[])sqlDataReader["Photo"]);

                    }
                    sqlDataReader.Close();
                    sqlConnection.Close();
                    if (photoBytes != null)                                                                         //若学生的照片非空;
                    {
                        MemoryStream memoryStream = new MemoryStream(photoBytes);                                   //声明并实例化内存流,用于读取照片的字节数据;
                        this.ptb_Photo.Image = Image.FromStream(memoryStream);                                      //调用图像的静态方法FromStream从内存流中读取图像,并赋予图像框;
                    }
                }
            }
            else if (cb_LoadName.Checked)
            {
                this.txt_MName.Text = this.cob_name.Text;
                using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
                {
                    SqlCommand sqlCommand1 = sqlConnection.CreateCommand();                             //调用SQL连接的方法CreateCommand来创建SQL命令;该命令将绑定SQL连接;
                    sqlCommand1.CommandText = "select * from tb_medicine where MName=@MName";
                    sqlCommand1.Parameters.AddWithValue("@MName", this.cob_name.Text.ToString());//                             //指定SQL命令的命令文本;命令文本为存储过程名称;
                    //sqlCommand1.CommandType = CommandType.StoredProcedure;                              //SQL命令的类型设为存储过程;
                    SqlCommand sqlCommand2 = new SqlCommand();
                    sqlCommand2.Connection = sqlConnection;
                    sqlCommand2.CommandText = "select * from tb_Supply";
                    SqlCommand sqlCommand3 = new SqlCommand();
                    sqlCommand3.Connection = sqlConnection;
                    sqlCommand3.CommandText = "select * from Tb_Stock";
                    //sqlCommand1.Parameters.AddWithValue("@MNo", this .txt_MNo .Text .ToString ());
                    //SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter();
                    //sqlDataAdapter1.SelectCommand = sqlCommand1;
                    //DataTable medicinetable = new DataTable();
                    SqlDataAdapter sqlDataAdapter2 = new SqlDataAdapter();                                           //声明并实例化SQL数据适配器,同时借助构造函数,将其SelectCommand属性设为先前创建的SQL命令;
                    sqlDataAdapter2.SelectCommand = sqlCommand2;                                                      //将SQL数据适配器的查询命令属性指向SQL命令;
                    DataTable SupplyTable = new DataTable();
                    SqlDataAdapter sqlDataAdapter3 = new SqlDataAdapter();                                           //声明并实例化SQL数据适配器,同时借助构造函数,将其SelectCommand属性设为先前创建的SQL命令;
                    sqlDataAdapter3.SelectCommand = sqlCommand3;                                                      //将SQL数据适配器的查询命令属性指向SQL命令;
                    DataTable StockTable = new DataTable();//向SQL命令的参数集合添加参数的名称、值;
                    sqlConnection.Open();
                    //sqlDataAdapter1 .Fill (medicinetable );
                    //this.cob_medicine.DataSource = medicinetable;                                                         //将班级下拉框的数据源设为班级数据表;
                    //this.cob_medicine.DisplayMember = "MName";                                                          //将班级下拉框的显示成员设为班级数据表的名称列;
                    //this.cob_Supply.ValueMember = "MNo";
                    sqlDataAdapter2.Fill(SupplyTable);                                                                //SQL数据适配器读取数据,并填充班级数据表;
                    this.cob_Supply.DataSource = SupplyTable;                                                         //将班级下拉框的数据源设为班级数据表;
                    this.cob_Supply.DisplayMember = "SupplyName";                                                          //将班级下拉框的显示成员设为班级数据表的名称列;
                    this.cob_Supply.ValueMember = "SupplyNo";
                    sqlDataAdapter3.Fill(StockTable);                                                                //SQL数据适配器读取数据,并填充班级数据表;
                    this.cob_Stock.DataSource = StockTable;                                                         //将班级下拉框的数据源设为班级数据表;
                    this.cob_Stock.DisplayMember = "StockName";                                                          //将班级下拉框的显示成员设为班级数据表的名称列;
                    this.cob_Stock.ValueMember = "StockNo";

                    SqlDataReader sqlDataReader = sqlCommand1.ExecuteReader();
                    byte[] photoBytes = null;
                    while (sqlDataReader.Read())
                    {

                        this.txt_MNo.Text = sqlDataReader["MNo"].ToString();
                        this.rdb_OTC.Checked = (bool)sqlDataReader["IsOTC"];
                        this.txt_MPrice.Text = sqlDataReader["MPrice"].ToString();
                        this.txt_MNum.Text = sqlDataReader["MNum"].ToString();
                        this.txt_norm.Text = sqlDataReader["Norm"].ToString();
                        this.dtp_ValidDate.Value = (DateTime)sqlDataReader["ValidDate"];
                        this.cob_Supply.SelectedValue = sqlDataReader["SupplyNo"];
                        this.cob_Stock.SelectedValue = sqlDataReader["StockNo"];
                        
                        this.txt_Instruction.Text = sqlDataReader["Instuction"].ToString();
                        photoBytes =
                        (sqlDataReader["Photo"] == DBNull.Value ? null : (byte[])sqlDataReader["Photo"]);

                    }
                    sqlDataReader.Close();
                    sqlConnection.Close();
                    if (photoBytes != null)                                                                         //若学生的照片非空;
                    {
                        MemoryStream memoryStream = new MemoryStream(photoBytes);                                   //声明并实例化内存流,用于读取照片的字节数据;
                        this.ptb_Photo.Image = Image.FromStream(memoryStream);                                      //调用图像的静态方法FromStream从内存流中读取图像,并赋予图像框;
                    }
                }
 
            }
        }

        private void btn_Load_Click(object sender, EventArgs e)
        {
            txt_Instruction.Clear();
            txt_MName.Clear();
            txt_MNo.Clear();
            txt_MNum.Clear();
            txt_MPrice.Clear();
            txt_norm.Clear();
            rdb_OTC.Checked = false;
            rdb_Rx.Checked = false;
            ptb_Photo.Image = null;
        }
    }
}

namespace 药品信息管理系统
{
    public partial class frm_MedicineTable : Form
    {
        public frm_MedicineTable()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;                                            //本窗体启动位置设为屏幕中央; 
            this.dgv_Medicine.AllowUserToAddRows = false;                                                      //数据网格视图不允许用户添加行;
            this.dgv_Medicine.RowHeadersVisible = false;                                                       //数据网格视图的行标题不可见;
            this.dgv_Medicine.BackgroundColor = Color.White;                                                   //数据网格视图的背景色设为白色;
            this.dgv_Medicine.AutoSizeColumnsMode =
                DataGridViewAutoSizeColumnsMode.AllCells;  
        }
       
     
           
                                                            //数据网格视图的自动调整列宽模式设为显示所有单元格;
        

        private void frm_MedicineTable_Load(object sender, EventArgs e)
        {

        }

        private void btn_Load_Click(object sender, EventArgs e)
        {
            using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
            {
                SqlCommand sqlCommand = new SqlCommand();
                SqlCommand sqlcommand1 = new SqlCommand();//声明并实例化SQL命令;
                SqlCommand sqlCommand2 = new SqlCommand();                                                      //声明并实例化SQL命令;
                sqlCommand.Connection = sqlConnection;
                sqlcommand1.Connection = sqlConnection;//将SQL命令的连接属性指向SQL连接;
                sqlCommand2.Connection = sqlConnection;                                                         //将SQL命令的连接属性指向SQL连接;
                sqlCommand.CommandText = "SELECT * FROM tb_Supply;";
                sqlcommand1.CommandText = "SELECT * FROM Tb_Stock";//指定SQL命令的命令文本;该命令查询所有班级,以用作下拉框数据源;
                sqlCommand2.CommandText = "SELECT * FROM tb_medicine;";                                          //指定SQL命令的命令文本;该命令查询所有学生;
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                           //声明并实例化SQL数据适配器;
                sqlDataAdapter.SelectCommand = sqlCommand;                                                      //将SQL数据适配器的查询命令属性指向SQL命令;
                DataTable SupplyTable = new DataTable();
                SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter();                                           //声明并实例化SQL数据适配器;
                sqlDataAdapter1.SelectCommand = sqlcommand1;                                                      //将SQL数据适配器的查询命令属性指向SQL命令;
                DataTable StockTable= new DataTable();   //声明并实例化数据表,用于保存所有班级,以用作下拉框数据源;
                SqlDataAdapter sqlDataAdapter2 = new SqlDataAdapter();                                          //声明并实例化SQL数据适配器;
                sqlDataAdapter2.SelectCommand = sqlCommand2;                                                    //将SQL数据适配器的查询命令属性指向SQL命令;
                DataTable medicineTable = new DataTable();                                                       //声明并实例化数据表,用于保存所有学生,以用作数据网格视图的数据源;
                sqlConnection.Open();                                                                           //打开SQL连接;
                sqlDataAdapter.Fill(SupplyTable);
                sqlDataAdapter1.Fill(StockTable );//SQL数据适配器读取数据,并填充班级数据表;
                sqlDataAdapter2.Fill(medicineTable);                                                             //SQL数据适配器读取数据,并填充学生数据表;
                sqlConnection.Close();                                                                          //关闭SQL连接;
                this.dgv_Medicine.Columns.Clear();                                                                 //数据网格视图的列集合清空;
                this.dgv_Medicine.DataSource = medicineTable;                                                       //将数据网格视图的数据源设为学生数据表;
                this.dgv_Medicine.Columns["MNo"].HeaderText = "药品编号";                                               //将数据网格视图的指定列的表头文本设为中文;
                this.dgv_Medicine.Columns["MName"].HeaderText = "药品名称";
                this.dgv_Medicine.Columns["MNum"].HeaderText = "库存";
                this.dgv_Medicine.Columns["Norm"].HeaderText = "规格";
                this.dgv_Medicine.Columns["MPrice"].HeaderText = "单价";
                this.dgv_Medicine.Columns["ValidDate"].HeaderText = "有效期至";
                this.dgv_Medicine.Columns["IsOTC"].HeaderText = "是否为非处方药";
                this.dgv_Medicine.Columns["Instuction"].Visible = false;
                this.dgv_Medicine.Columns["SupplyNo"].Visible = false;
                this.dgv_Medicine.Columns["StockNo"].Visible = false;
                this.dgv_Medicine.Columns["Photo"].Visible = false;
                //将数据网格视图的指定列设为不可见;
                this.dgv_Medicine.Columns[this.dgv_Medicine.Columns.Count - 1].AutoSizeMode =                         //数据网格视图的最后一列的自动调整列宽模式设为填充(至数据网格视图右侧边缘);
                    DataGridViewAutoSizeColumnMode.Fill;
                DataGridViewComboBoxColumn SupplyColumn = new DataGridViewComboBoxColumn();                      //声明并实例化数据网格视图下拉框列,用于设置学生的班级;
                SupplyColumn.Name = "SupplyName";                                                                     //设置下拉框列的名称;
                SupplyColumn.HeaderText = "供应商";                                                                //设置下拉框列的表头文本;
                SupplyColumn.DataSource = SupplyTable;                                                            //设置下拉框列的数据源为班级数据表;
                SupplyColumn.DisplayMember = "SupplyName";                                                             //设置下拉框列的显示成员为(班级数据表的)名称(列);
                SupplyColumn.ValueMember = "SupplyNo";                                                                 //设置下拉框列的值成员为(班级数据表的)编号(列);
                SupplyColumn.DataPropertyName = "SupplyNo";                                                       //设置下拉框列的数据属性名称为(学生数据表的)班级编号(列);
                SupplyColumn.DisplayIndex = 10;                                                                   //设置下拉框列的显示顺序;
                SupplyColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;                                 //设置下拉框列的自动调整列宽模式为填充;
                this.dgv_Medicine.Columns.Add(SupplyColumn);
                DataGridViewComboBoxColumn StockColumn = new DataGridViewComboBoxColumn();                      //声明并实例化数据网格视图下拉框列,用于设置学生的班级;
                StockColumn.Name = "StockName";                                                                     //设置下拉框列的名称;
                StockColumn.HeaderText = "药库";                                                                //设置下拉框列的表头文本;
                StockColumn.DataSource = StockTable;                                                            //设置下拉框列的数据源为班级数据表;
                StockColumn.DisplayMember = "StockName";                                                             //设置下拉框列的显示成员为(班级数据表的)名称(列);
                StockColumn.ValueMember = "StockNo";                                                                 //设置下拉框列的值成员为(班级数据表的)编号(列);
                StockColumn.DataPropertyName = "StockNo";                                                       //设置下拉框列的数据属性名称为(学生数据表的)班级编号(列);
                StockColumn.DisplayIndex = 10;                                                                   //设置下拉框列的显示顺序;
                //StockColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;                                 //设置下拉框列的自动调整列宽模式为填充;
                this.dgv_Medicine.Columns.Add(StockColumn);
                this.dgv_Medicine.Columns[this.dgv_Medicine.Columns.Count - 1].AutoSizeMode =                         //数据网格视图的最后一列的自动调整列宽模式设为填充(至数据网格视图右侧边缘);
                    DataGridViewAutoSizeColumnMode.Fill;
            }//在字符串变量中,描述连接字符串所需的服务器地址、数据库名称、集成安全性(即是否使用Windows验证);
            
        }

        private void btn_Update_Click(object sender, EventArgs e)
        {
            using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
            {
                SqlCommand sqlCommand = new SqlCommand();                                                       //声明并实例化SQL命令;
                sqlCommand.Connection = sqlConnection;                                                          //将SQL命令的连接属性指向SQL连接;
                sqlCommand.CommandText =                                                                        //指定SQL命令的命令文本;
                    "UPDATE tb_medicine"
                    + " SET MName=@MName,MPrice=@MPrice,Norm=@Norm,MNum=@MNum,ValidDate=@ValidDate,SupplyNo=@SupplyNo,StockNo=@StockNo"
                    + " WHERE MNo=@MNo;";
                sqlCommand.Parameters.Add("@MName", SqlDbType.VarChar, 0, "MName");                                  //向SQL命令的参数集合添加参数的名称、SQL Server数据类型、长度(仅用于定长类型)、所绑定的数据表中的列名;
                sqlCommand.Parameters.Add("@MPrice", SqlDbType.Money, 0, "MPrice");
                sqlCommand.Parameters.Add("@ValidDate", SqlDbType.Date, 0, "ValidDate");
                sqlCommand.Parameters.Add("@Norm", SqlDbType.VarChar, 0, "Norm");
                sqlCommand.Parameters.Add("@MNum", SqlDbType.Int, 0, "MNum");
                sqlCommand.Parameters.Add("@SupplyNo", SqlDbType.Char, 0, "SupplyNo");
                sqlCommand.Parameters.Add("@StockNo", SqlDbType.Char, 0, "StockNo");
                sqlCommand.Parameters.Add("@MNo", SqlDbType.Char, 10, "MNo");
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                           //声明并实例化SQL数据适配器,同时借助构造函数,将其SelectCommand属性设为先前创建的SQL命令;
                sqlDataAdapter.UpdateCommand = sqlCommand;                                                      //将SQL数据适配器的更新命令属性指向SQL命令;
                DataTable medicineTable = (DataTable)this.dgv_Medicine.DataSource;                                  //声明数据表,并指向数据网格视图的数据源;数据源默认类型为object,还需强制转换类型;
                sqlConnection.Open();                                                                           //打开SQL连接;
                int rowAffected = sqlDataAdapter.Update(medicineTable);                                          //SQL数据适配器根据学生数据表提交更新,并返回受影响行数;
                sqlConnection.Close();                                                                          //关闭SQL连接;
                MessageBox.Show("更新" + rowAffected.ToString() + "行。"); 
            }

        }

      
        }
    }

namespace 药品信息管理系统
{
    public partial class frm_Supply : Form
    {
        public frm_Supply()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;  
        }


      
    

             private void btn_Go_Click(object sender, EventArgs e)
             {
                 frm_SupplyTable frm_supplyTable = new frm_SupplyTable();
                 frm_supplyTable.ShowDialog(this);
                 this.Hide();
             }

            

             private void frm_Supply_Load(object sender, EventArgs e)
             {

             }


             private void btn_SSeek_Click_1(object sender, EventArgs e)
             {
                 if (cb_LoadNo.Checked)
                 {
                     this.txt_SNO.Text = this.cob_SupplyNo.Text;
                     using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
                     {
                         SqlCommand sqlCommand1 = sqlConnection.CreateCommand();                             //调用SQL连接的方法CreateCommand来创建SQL命令;该命令将绑定SQL连接;
                         sqlCommand1.CommandText = "select * from tb_Supply where SupplyNo=@SupplyNo";
                         sqlCommand1.Parameters.AddWithValue("@SupplyNo", this.cob_SupplyNo.Text.ToString());//this.txt_supplyname.Text.ToString()                                //指定SQL命令的命令文本;命令文本为存储过程名称;
                         //sqlCommand1.CommandType = CommandType.StoredProcedure;                              //SQL命令的类型设为存储过程;
                         //sqlCommand1.Parameters.AddWithValue("@CNo", client.CNo);                                //向SQL命令的参数集合添加参数的名称、值;
                         //sqlCommand1.Parameters.AddWithValue("@CName", client.CName);
                         //sqlCommand1.Parameters.AddWithValue("@CTel", client.CTel);
                         //sqlCommand1.Parameters.AddWithValue("@CAddress", client.CAddress);
                         sqlConnection.Open();                                                               //打开SQL连接;
                         SqlDataReader sqlDataReader = sqlCommand1.ExecuteReader();
                         while (sqlDataReader.Read())
                         {

                             //this.txt_SNO.Text = sqlDataReader["SupplyNO"].ToString();
                             this.txt_SName.Text = sqlDataReader["SupplyName"].ToString();
                             this.txt_STel.Text = sqlDataReader["SupplyTel"].ToString();
                             this.txt_SAddress.Text = sqlDataReader["Address"].ToString();

                         }
                         sqlDataReader.Close();
                         sqlConnection.Close();
                     }
                    
 
                 }
                 else if (cb_LoadName.Checked)
                 {
                     this.txt_SName.Text = this.cob_Name.Text;
                     using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
                     {
                         SqlCommand sqlCommand1 = sqlConnection.CreateCommand();                             //调用SQL连接的方法CreateCommand来创建SQL命令;该命令将绑定SQL连接;
                         sqlCommand1.CommandText = "select * from tb_Supply where SupplyName in (select SupplyName from tb_Supply where SupplyName like '%" + this.cob_Name.Text.ToString() + "%')";                                 //指定SQL命令的命令文本;命令文本为存储过程名称;
                         //sqlCommand1.CommandType = CommandType.StoredProcedure;                              //SQL命令的类型设为存储过程;
                         //sqlCommand1.Parameters.AddWithValue("@CNo", client.CNo);                                //向SQL命令的参数集合添加参数的名称、值;
                         //sqlCommand1.Parameters.AddWithValue("@CName", client.CName);
                         //sqlCommand1.Parameters.AddWithValue("@CTel", client.CTel);
                         //sqlCommand1.Parameters.AddWithValue("@CAddress", client.CAddress);
                         sqlConnection.Open();                                                               //打开SQL连接;
                         SqlDataReader sqlDataReader = sqlCommand1.ExecuteReader();
                         while (sqlDataReader.Read())
                         {

                             this.txt_SNO.Text = sqlDataReader["SupplyNO"].ToString();
                             //this.txt_CName.Text = sqlDataReader["CName"].ToString();
                             this.txt_STel.Text = sqlDataReader["SupplyTel"].ToString();
                             this.txt_SAddress.Text = sqlDataReader["Address"].ToString();

                         }
                         sqlDataReader.Close();
                         sqlConnection.Close();
                     }
                 }
                 
             }
            

             private void btn_Sadd_Click(object sender, EventArgs e)
             {
                 using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
                 {
                     sqlConnection.Open();
                     string str = " insert tb_Supply (SupplyNO ,SupplyName ,Address ,SupplyTel )values('" + this.txt_SNO.Text + "','" + this.txt_SName.Text + "','" + this.txt_SAddress.Text + "','" + this.txt_STel.Text + "')";
                     SqlCommand sqlCommand1 = new SqlCommand(str, sqlConnection);
                     //sqlConnection.Open(); 
                     int i= 0;                                                                //声明整型变量,用于保存受影响行数
                     try                                                                                 //尝试;
                     {
                         i  = sqlCommand1.ExecuteNonQuery();                                     //调用SQL命令的方法ExecuteNonQuery来执行命令,向数据库写入数据,并返回受影响行数;
                     }
                     catch (SqlException sqlEx)                                                          //捕捉SQL异常;
                     {
                         if (sqlEx.Number == 2627)                                                       //若异常的编号为2627,则违反实体完整性,即插入了主键重复的记录;
                         {
                             i = 0;
                             MessageBox.Show("客户已存在");//存在雷同用户;
                         }
                         else
                         {
                             throw sqlEx;
                         }
                     }//打开SQL连接;
                      
                     if (i == 1)
                     {
                         MessageBox.Show("OK");
                     }
                     else
                         MessageBox.Show("not OK");
                     sqlConnection.Close();
                 }
             }

             private void btn_Salter_Click(object sender, EventArgs e)
             {
                 using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
                 {
                     sqlConnection.Open();
                     string str = "update tb_Supply set SupplyName='" + this.txt_SName.Text + "',SupplyTel='" + this.txt_STel.Text + "',Address='" + this.txt_SAddress.Text + "' where SupplyNO='" + this.txt_SNO .Text + "'";
                     SqlCommand sqlCommand1 = new SqlCommand(str, sqlConnection);
                     //sqlConnection.Open();                                                               //打开SQL连接;
                     int i = sqlCommand1.ExecuteNonQuery();
                     if (i == 1)
                     {
                         MessageBox.Show("OK");
                     }
                     else
                         MessageBox.Show("not OK");
                     sqlConnection.Close();
                 }
             }

             private void btn_SDelete_Click(object sender, EventArgs e)
             {
                 if (MessageBox.Show("确认删除编号为" + txt_SNO.Text + "的数据?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                 {
                     using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
                     {
                         sqlConnection.Open();
                         string str = "delete from tb_Supply where SupplyNO='" + this.txt_SNO .Text + "'";
                         SqlCommand sqlCommand1 = new SqlCommand(str, sqlConnection);
                         //sqlConnection.Open();                                                               //打开SQL连接;
                         int i = sqlCommand1.ExecuteNonQuery();
                         if (i == 1)
                         {
                             MessageBox.Show("OK");
                         }
                         else
                             MessageBox.Show("not OK");
                         sqlConnection.Close();
                     }
                     cob_SupplyNo.Text = "";
                     cob_Name.Text = "";
                     txt_SAddress.Text = "";
                     txt_SName.Text = "";
                     txt_SNO.Text = "";
                     txt_STel.Text = "";
 
                 }
            
                 
             }

             private void cb_LoadNo_CheckedChanged(object sender, EventArgs e)
             {
                 if(cb_LoadName .Checked ==false )
                 {
                     if(cb_LoadNo .Checked ==false )
                     {
                         cob_Name .Text ="";
                         cob_SupplyNo .Text ="";
                     }
                 }
                 if (cb_LoadNo.Checked)
                 {
                     cb_LoadName.Checked = false;
                     txt_SName.ReadOnly = false;
                     cob_Name.Text = "";
                     using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
                     {
                         SqlCommand sqlCommand = sqlConnection.CreateCommand();                             //调用SQL连接的方法CreateCommand来创建SQL命令;该命令将绑定SQL连接;
                         sqlCommand.CommandText = "select SupplyNo  from tb_Supply";
                         SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                           //声明并实例化SQL数据适配器,同时借助构造函数,将其SelectCommand属性设为先前创建的SQL命令;
                         sqlDataAdapter.SelectCommand = sqlCommand;                                                      //将SQL数据适配器的查询命令属性指向SQL命令;
                         DataTable SupplyTable = new DataTable();
                         sqlConnection.Open();
                         sqlDataAdapter.Fill(SupplyTable);                                                                //SQL数据适配器读取数据,并填充班级数据表;
                         this.cob_SupplyNo.DataSource = SupplyTable;                                                         //将班级下拉框的数据源设为班级数据表;
                         this.cob_SupplyNo.DisplayMember = "SupplyNo";                                                          //将班级下拉框的显示成员设为班级数据表的名称列;
                         this.cob_SupplyNo.ValueMember = "SupplyNo";
                         SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                         while (sqlDataReader.Read())
                         {

                             this.cob_SupplyNo.Text = sqlDataReader["SupplyNo"].ToString();

                         }
                         sqlDataReader.Close();
                         sqlConnection.Close();

                     }

                 }
             }

             private void cb_LoadName_CheckedChanged(object sender, EventArgs e)
             {
                 if (cb_LoadName.Checked == false)
                 {
                     if (cb_LoadNo.Checked == false)
                     {
                         cob_Name.Text = "";
                         cob_SupplyNo.Text = "";
                     }
                 }
                 if (cb_LoadName.Checked)
                 {
                     cb_LoadNo.Checked = false;
                     //txt_SName.ReadOnly = true;
                     cob_SupplyNo.Text = "";
                     using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString))
                     {
                         SqlCommand sqlCommand = sqlConnection.CreateCommand();                             //调用SQL连接的方法CreateCommand来创建SQL命令;该命令将绑定SQL连接;
                         sqlCommand.CommandText = "select SupplyName  from tb_Supply";
                         SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                           //声明并实例化SQL数据适配器,同时借助构造函数,将其SelectCommand属性设为先前创建的SQL命令;
                         sqlDataAdapter.SelectCommand = sqlCommand;                                                      //将SQL数据适配器的查询命令属性指向SQL命令;
                         DataTable SupplyTable = new DataTable();
                         sqlConnection.Open();
                         sqlDataAdapter.Fill(SupplyTable);                                                                //SQL数据适配器读取数据,并填充班级数据表;
                         this.cob_Name.DataSource = SupplyTable;                                                         //将班级下拉框的数据源设为班级数据表;
                         this.cob_Name.DisplayMember = "SupplyName";                                                          //将班级下拉框的显示成员设为班级数据表的名称列;
                         this.cob_Name.ValueMember = "SupplyName";
                         SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                         while (sqlDataReader.Read())
                         {

                             this.cob_Name.Text = sqlDataReader["SupplyName"].ToString();

                         }
                         sqlDataReader.Close();
                         sqlConnection.Close();

                     }
                 }
             }

             private void btn_Load_Click(object sender, EventArgs e)
             {
                 cob_SupplyNo.Text = "";
                 cob_Name.Text = "";
                 txt_SAddress.Text = "";
                 txt_SName.Text = "";
                 txt_SNO.Text = "";
                 txt_STel.Text = "";
             }
    }
}

猜你喜欢

转载自www.cnblogs.com/xxnzmy/p/12398109.html