public code_topics Update(code_topics ct, Connection con) { code_topics old = GetByid(con, ct.topic_id); //Connection con = new Connection(); Command cmd = new Command(); string query1 = ""; if (!ct.topic_subject.Equals(old.topic_details)) { query1 += "topic_subject=@topic_subject,"; cmd.Parameters.Add("@topic_subject", DBType.VarChar, ct.topic_subject); } if (!ct.search_tags.Equals(old.search_tags)) { query1 += "search_tags=@search_tags,"; cmd.Parameters.Add("@search_tags", DBType.VarChar, ct.search_tags); } if (!ct.code_topicscol.Equals(old.code_topicscol)) { query1 += "code_topicscol=@code_topicscol,"; cmd.Parameters.Add("@code_topicscol", DBType.VarChar, ct.code_topicscol); } if (!ct.topic_details.Equals(old.topic_details)) { query1 += "topic_details=@topic_details,"; cmd.Parameters.Add("@topic_details", DBType.VarChar, ct.topic_details); } if (!ct.fileName.Equals(old.fileName)) { query1 += "fileName=@fileName,"; cmd.Parameters.Add("@fileName", DBType.VarChar, ct.fileName); } if (!ct.Language.Equals(old.Language)) { query1 += "Language=@Language,"; cmd.Parameters.Add("@Language", DBType.VarChar, ct.Language); } if (!ct.URL.Equals(old.URL)) { query1 += "URL=@URL,"; cmd.Parameters.Add("@URL", DBType.VarChar, ct.URL); } if (query1.Length > 2) { string Query = "UPDATE code_topics SET " + query1.TrimEnd(',') + " WHERE topic_id=" + ct.topic_id; cmd.CommandText = Query; cmd.CommandType = DBConnection.CommandType.Text; con.cmd = cmd; DataTable DT = con.getDataTable(); ct.topic_id = Convert.ToInt32(DT.Rows[0][0]); } return(ct); }
public code_topics Create(code_topics ct, Connection con) { //Connection con = new Connection(); Command cmd = new Command(); string query1 = ""; string query2 = ""; if (ct.userId == 0) { throw new Exception("userId is required"); } else { query1 += "userId,"; query2 += "@userId,"; cmd.Parameters.Add("@userId", DBType.Int, ct.userId); } if (!string.IsNullOrEmpty(ct.topic_subject)) { query1 += "topic_subject,"; query2 += "@topic_subject,"; cmd.Parameters.Add("@topic_subject", DBType.VarChar, ct.topic_subject); } if (!string.IsNullOrEmpty(ct.search_tags)) { query1 += "search_tags,"; query2 += "@search_tags,"; cmd.Parameters.Add("@search_tags", DBType.VarChar, ct.search_tags); } if (!string.IsNullOrEmpty(ct.code_topicscol)) { query1 += "code_topicscol,"; query2 += "@code_topicscol,"; cmd.Parameters.Add("@code_topicscol", DBType.VarChar, ct.code_topicscol); } query1 += "Active,"; query2 += "@Active,"; cmd.Parameters.Add("@Active", DBType.Int, ct.Active); query1 += "Allow_Comment,"; query2 += "@Allow_Comment,"; cmd.Parameters.Add("@Allow_Comment", DBType.Int, ct.Allow_Comment); // Write Here for missing column created_date query1 += "created_date,"; query2 += "@created_date,"; cmd.Parameters.Add("@created_date", DBType.DateTime, ct.created_date); query1 += "visible,"; query2 += "@visible,"; cmd.Parameters.Add("@visible", DBType.Int, ct.visible); if (!string.IsNullOrEmpty(ct.topic_details)) { query1 += "topic_details,"; query2 += "@topic_details,"; cmd.Parameters.Add("@topic_details", DBType.VarChar, ct.topic_details); } if (!string.IsNullOrEmpty(ct.fileName)) { query1 += "fileName,"; query2 += "@fileName,"; cmd.Parameters.Add("@fileName", DBType.VarChar, ct.fileName); } if (!string.IsNullOrEmpty(ct.Language)) { query1 += "Language,"; query2 += "@Language,"; cmd.Parameters.Add("@Language", DBType.VarChar, ct.Language); } if (!string.IsNullOrEmpty(ct.URL)) { query1 += "URL,"; query2 += "@URL,"; cmd.Parameters.Add("@URL", DBType.VarChar, ct.URL); } string Query = "INSERT INTO code_topics(" + query1.TrimEnd(',') + ") VALUES (" + query2.TrimEnd(',') + ") ;" + (con.ServerType == DBServerType.MYSQL ? " SELECT last_insert_id(); " : "SELECT @@IDENTITY"); cmd.CommandText = Query; cmd.CommandType = DBConnection.CommandType.Text; con.cmd = cmd; DataTable DT = con.getDataTable(); ct.topic_id = Convert.ToInt32(DT.Rows[0][0]); return(ct); }