protected void btnSave_Click(object sender, EventArgs e) { string empNo = this.currentUser.empNo; string empNm = this.currentUser.empNm; LForumQuestionData forumQuestionData = new LForumQuestionData(); LForumQuestionBB forumQuestionBB = new LForumQuestionBB(); try { forumQuestionData.questionEmpNo = empNo; forumQuestionData.questionEmpNm = empNm; forumQuestionData.questionDt = DateTime.Now.ToString(); forumQuestionData.questionTitle = this.txtTitle.Text; forumQuestionData.questionContent = this.txtContent.Text; //获取客户端IP地址 string uip = ""; if (Request.ServerVariables["HTTP_VIA"] != null) { uip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); } else { uip = Request.ServerVariables["REMOTE_ADDR"].ToString(); } forumQuestionData.ipAddress = uip; forumQuestionBB.AddRecord(forumQuestionData); } finally { forumQuestionBB.Dispose(); } this.ClientScript.RegisterStartupScript(this.GetType(), "CloseSubmit", "CloseSubmit()", true); }
/// <summary> /// 增加一条数据 /// </summary> /// <param name="model">model</param> public int AddRecord(LForumQuestionData model) { StringBuilder strSql = new StringBuilder(); strSql.Append("set nocount on; "); strSql.Append("insert into LForumQuestion("); strSql.Append(@"questionEmpNo,questionEmpNm,questionDt,questionTitle,questionContent,ipAddress,headPortrait)"); strSql.Append(" values ("); strSql.Append(@"@questionEmpNo,@questionEmpNm,@questionDt,@questionTitle,@questionContent,@ipAddress,@headPortrait)"); strSql.Append("; select @@identity; set nocount off; "); SqlParameter[] parameters = { new SqlParameter("@questionEmpNo", SqlDbType.NVarChar,20), new SqlParameter("@questionEmpNm", SqlDbType.NVarChar,50), new SqlParameter("@questionDt", SqlDbType.DateTime), new SqlParameter("@questionTitle", SqlDbType.NVarChar,50), new SqlParameter("@questionContent", SqlDbType.NVarChar,500), new SqlParameter("@ipAddress", SqlDbType.NVarChar,50), new SqlParameter("@headPortrait", SqlDbType.NVarChar,20) }; parameters[0].Value = model.questionEmpNo; parameters[1].Value = model.questionEmpNm; parameters[2].Value = model.questionDt == string.Empty ? null : model.questionDt; parameters[3].Value = model.questionTitle; parameters[4].Value = model.questionContent; parameters[5].Value = model.ipAddress; parameters[6].Value = model.headPortrait; int id = 0; try { object ret = SqlHelper.ExecuteScalar(this.connection, this.transaction, CommandType.Text, strSql.ToString(), parameters); if (ret != null && ret != DBNull.Value) { id = Convert.ToInt32(ret); } } catch (Exception ex) { throw ex; } return id; }
/// <summary> /// 更新一条数据 /// </summary> /// <param name="model">model</param> public bool ModifyRecord(LForumQuestionData model) { bool ret = false; StringBuilder strSql = new StringBuilder(); strSql.Append("update LForumQuestion set "); strSql.Append("questionEmpNo=@questionEmpNo,"); strSql.Append("questionEmpNm=@questionEmpNm,"); strSql.Append("questionDt=@questionDt,"); strSql.Append("questionTitle=@questionTitle,"); strSql.Append("questionContent=@questionContent,"); strSql.Append("ipAddress=@ipAddress,"); strSql.Append("headPortrait=@headPortrait"); strSql.Append(" where id = @id "); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.Int), new SqlParameter("@questionEmpNo", SqlDbType.NVarChar,20), new SqlParameter("@questionEmpNm", SqlDbType.NVarChar,50), new SqlParameter("@questionDt", SqlDbType.DateTime), new SqlParameter("@questionTitle", SqlDbType.NVarChar,50), new SqlParameter("@questionContent", SqlDbType.NVarChar,500), new SqlParameter("@ipAddress", SqlDbType.NVarChar,50), new SqlParameter("@headPortrait", SqlDbType.NVarChar,20) }; parameters[0].Value = model.id; parameters[1].Value = model.questionEmpNo; parameters[2].Value = model.questionEmpNm; parameters[3].Value = model.questionDt == string.Empty ? null : model.questionDt; parameters[4].Value = model.questionTitle; parameters[5].Value = model.questionContent; parameters[6].Value = model.ipAddress; parameters[7].Value = model.headPortrait; try { SqlHelper.ExecuteNonQuery(this.connection, this.transaction, CommandType.Text, strSql.ToString(), parameters); ret = true; } catch (Exception ex) { throw ex; } return ret; }
/// <summary> /// 得到一个model /// </summary> /// <param name="id">主键值</param> /// <returns>model</returns> public LForumQuestionData GetModel(int id) { StringBuilder strSql = new StringBuilder(); strSql.Append(@"select id,questionEmpNo,questionEmpNm,questionDt,questionTitle,questionContent,ipAddress,headPortrait from LForumQuestion"); strSql.Append(" where id = @id "); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.Int) }; parameters[0].Value = id; LForumQuestionData model = new LForumQuestionData(); DataSet ds = SqlHelper.ExecuteDataset(this.connection, this.transaction, CommandType.Text, strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { DataRow row = ds.Tables[0].Rows[0]; if (row["id"] != DBNull.Value) { model.id = Convert.ToInt32(row["id"]); } if (row["questionEmpNo"] != DBNull.Value) { model.questionEmpNo = Convert.ToString(row["questionEmpNo"]); } if (row["questionEmpNm"] != DBNull.Value) { model.questionEmpNm = Convert.ToString(row["questionEmpNm"]); } if (row["questionDt"] != DBNull.Value) { model.questionDt = Convert.ToString(row["questionDt"]); } if (row["questionTitle"] != DBNull.Value) { model.questionTitle = Convert.ToString(row["questionTitle"]); } if (row["questionContent"] != DBNull.Value) { model.questionContent = Convert.ToString(row["questionContent"]); } if (row["ipAddress"] != DBNull.Value) { model.ipAddress = Convert.ToString(row["ipAddress"]); } if (row["headPortrait"] != DBNull.Value) { model.headPortrait = Convert.ToString(row["headPortrait"]); } return model; } else { return null; } }
/// <summary> /// 更新一条数据 /// </summary> /// <param name="model">model</param> public bool ModifyRecord(LForumQuestionData model) { return this.forumQuestionDB.ModifyRecord(model); }
/// <summary> /// 增加一条数据 /// </summary> /// <param name="model">model</param> public int AddRecord(LForumQuestionData model) { return this.forumQuestionDB.AddRecord(model); }