Exemplo n.º 1
0
        //
        public static bool AddUsage(UsageItem item)
        {
            string        connString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=""E:\c++练习\MyMoneyAgent - 副本 (2) - 副本\MyMoneyAgent\MyMoney.mdf"";Integrated Security=True";
            SqlConnection conn       = new SqlConnection(connString);//创建链接对象

            try
            {
                conn.Open();
                //执行删除命令
                string cmdTxt = string.Format("INSERT INTO [用途表] VALUES('{0}','{1}')",
                                              item.Usage, item.Description);
                SqlCommand comm = new SqlCommand(cmdTxt, conn);
                comm.ExecuteNonQuery();
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
            finally
            {
                //关闭连接
                conn.Close();
            }
        }
Exemplo n.º 2
0
        private void btnDelUse_Click(object sender, EventArgs e)
        {
            //如果没有选中任何用途,则不删除,直接退出
            if (this.lstUsage.SelectedItem == null)
            {
                return;
            }
            //获取选中的用途
            UsageItem item = (UsageItem)this.lstUsage.SelectedItem;

            //通过BankCardAccessor删除用途
            if (BankCardSQL.DeleteUsage(item.Usage))
            {
                this.btnLoadUse_Click(null, null);
                //删除成功提示成功
                MessageBox.Show(this, "删除用途:" + item.Usage + ",成功!", "提示",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                //删除失败,提示失败
                MessageBox.Show(this, "删除用途:" + item.Usage + ",失败!", "提示",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Exemplo n.º 3
0
        //
        public static List <UsageItem> LoadUseageList()
        {
            string        connString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=""E:\c++练习\MyMoneyAgent - 副本 (2) - 副本\MyMoneyAgent\MyMoney.mdf"";Integrated Security=True";
            SqlConnection conn       = new SqlConnection(connString);//创建链接对象

            try
            {
                conn.Open();
                //查询数据库中
                string        cmdTxt = "SELECT [用途], [说明] FROM [用途表] ";
                SqlCommand    comm   = new SqlCommand(cmdTxt, conn);
                SqlDataReader dr;
                dr = comm.ExecuteReader();
                List <UsageItem> usageLst = new List <UsageItem>();
                while (dr.Read())
                {
                    UsageItem item = new UsageItem();
                    item.Usage       = dr.GetString(0);
                    item.Description = dr.GetString(1);
                    usageLst.Add(item);
                }
                return(usageLst);
            }
            catch (Exception)
            {
                return(null);
            }
            finally
            {
                //关闭连接
                conn.Close();
            }
        }
Exemplo n.º 4
0
 private void lstUsage_SelectedIndexChanged(object sender, EventArgs e)
 {
     //如果有选中的用途,则更新
     if (this.lstUsage.SelectedItem != null)
     {
         //获取选中的用途
         UsageItem item = (UsageItem)this.lstUsage.SelectedItem;
         //将用途的名称和说明显示到界面
         this.tbUsage.Text    = item.Usage;
         this.tbUsageDes.Text = item.Description;
     }
 }
Exemplo n.º 5
0
        private void btnAddUse_Click(object sender, EventArgs e)
        {
            //检查用途名称的合法性
            if (string.IsNullOrEmpty(this.tbUsage.Text))
            {
                MessageBox.Show(this, "请输入正确的用途名称,不能已经存在,也不能为空!", "提示",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            //检查用途说明的合法性
            if (string.IsNullOrEmpty(this.tbUsageDes.Text))
            {
                MessageBox.Show(this, "请输入正确的用途说明,不能为空!", "提示",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            //创建用途对象
            UsageItem item = new UsageItem();

            //获取新用途数据
            item.Usage       = this.tbUsage.Text.Trim();
            item.Description = this.tbUsageDes.Text.Trim();
            //添加到数据库
            if (BankCardSQL.AddUsage(item))
            {
                //重新加载用途列表
                this.btnLoadUse_Click(null, null);
                //删除成功提示成功
                MessageBox.Show(this, "添加用途:" + item.Usage + ",成功!", "提示",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                //删除成功提示成功
                MessageBox.Show(this, "添加用途:" + item.Usage + ",失败!", "提示",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }