// public static bool ModifyIncomrec(string name, IncomeRecord rec) { 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; cmdTxt = string.Format("UPDATE [收入记录] SET [收入时间]='{0}', [记录时间]='{1}', [收入方式]='{2}', [收入类型]='{3}', 金额={4}, [银行卡号]='{5}', [收入说明]='{6}' WHERE ([收入编号]={7} AND [姓名]='{8}')", rec.IncomeTime, rec.RecordTime, rec.IncomeType, rec.IncomUsage, rec.Amount, rec.BankCard, rec.Description, rec.ID, name); //查询命令 SqlCommand comm = new SqlCommand(cmdTxt, conn); comm.ExecuteNonQuery(); return(true); } catch (Exception) { return(false); } finally { //关闭连接 conn.Close(); } }
// public static bool AddIncomRec(string name, IncomeRecord rec) { 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; cmdTxt = string.Format("INSERT INTO [收入记录] VALUES({0}, '{1}', '{2}', '{3}', '{4}', '{5}', {6}, '{7}', '{8}')", rec.ID, name, rec.IncomeTime, rec.RecordTime, rec.IncomeType, rec.IncomUsage, rec.Amount, rec.BankCard, rec.Description); //查询命令 SqlCommand comm = new SqlCommand(cmdTxt, conn); comm.ExecuteNonQuery(); return(true); } catch (Exception) { return(false); } finally { //关闭连接 conn.Close(); } }
public IncomeReForm(string nm, IncomeRecord rec, bool ro) { InitializeComponent(); this.name = nm; this.record = rec; this.readoly = ro; }
private void btnAddNewIncomRec_Click(object sender, EventArgs e) { //创建用来保存新记录的对象 int newId = IncomeSQL.GetUsableIncomID(this.curUser.Name); if (newId == 0) { MessageBox.Show(this, "无法获取可用的收入记录编号,请确保数据库连接正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } IncomeRecord rec = new IncomeRecord(); rec.ID = newId; rec.IncomeTime = DateTime.Now; rec.RecordTime = DateTime.Now; //创建收入记录编辑对话框 IncomeReForm dlg = new IncomeReForm(this.curUser.Name, rec, false); if (dlg.ShowDialog(this) == DialogResult.OK) { //添加记录成功,则添加到界面 if (IncomeSQL.AddIncomRec(this.curUser.Name, rec)) { //添加一行到界面 int newRowIndex = this.dgvInRec.Rows.Add(); this.dgvInRec.Rows[newRowIndex].Tag = rec; //显示信息到新添加的行 this.dgvInRec.Rows[newRowIndex].Cells["colInID"].Value = rec.ID; this.dgvInRec.Rows[newRowIndex].Cells["colInInTime"].Value = rec.IncomeTime; this.dgvInRec.Rows[newRowIndex].Cells["colInType"].Value = rec.IncomeType; this.dgvInRec.Rows[newRowIndex].Cells["colInUsage"].Value = rec.IncomUsage; this.dgvInRec.Rows[newRowIndex].Cells["colInRecTime"].Value = rec.RecordTime; this.dgvInRec.Rows[newRowIndex].Cells["colInDes"].Value = rec.Description; this.dgvInRec.Rows[newRowIndex].Cells["colInBank"].Value = rec.BankCard; this.dgvInRec.Rows[newRowIndex].Cells["colInAmount"].Value = rec.Amount; } else { MessageBox.Show(this, "添加记录失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }
public static List <IncomeRecord> LoadIncomRcdList(string name) { 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 [收入记录] " + "WHERE [姓名]='" + name + "'"; SqlCommand comm = new SqlCommand(cmdTxt, conn); List <IncomeRecord> recLst = new List <IncomeRecord>(); SqlDataReader dr = comm.ExecuteReader(); while (dr.Read()) { IncomeRecord rec = new IncomeRecord(); rec.ID = dr.GetInt32(0); rec.IncomeTime = dr.GetDateTime(2); rec.RecordTime = dr.GetDateTime(3); rec.IncomeType = dr.GetString(4); rec.IncomUsage = dr.GetString(5); rec.Amount = dr.GetDecimal(6); rec.BankCard = dr.GetString(7); rec.Description = dr.GetString(8); recLst.Add(rec); } return(recLst); } catch (Exception) { return(null); } finally { //关闭连接 conn.Close(); } }