珂珂的个人博客 - 一个程序猿的个人网站

discuz插入帖子和用户数据

由于不懂php...所以网上搜索了下它的数据表结构,只能找到部分说明,也只插入了部分数据,经测试可以正常显示了。

由于这个就几张表,所以没必要再用什么orm框架了吧,直接sql语句。。。有2个属性类就不发出来了。。一看就明白

  #region 发布主题帖子
        public int InsertThread(string mysqlConnectionString, string pre, int fid, string author, int authorid, string subject, string message, string useip)
        {
            DbConnection conn = new MySql.Data.MySqlClient.MySqlConnection(mysqlConnectionString);
            conn.Open();
            DbCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select max(tid) from " + pre + "forum_thread";
            int tid = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
            cmd.CommandText = "select max(pid) from " + pre + "forum_post";
            int pid = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
            conn.Close();
            int dateline = (int)ConvertDateTimeInt(DateTime.Now);
            ThreadModel thread = new ThreadModel();
            thread.author = author;
            thread.authorid = authorid;
            thread.dateline = dateline;
            thread.fid = fid;
            thread.dateline = dateline;
            thread.lastpost = dateline;
            thread.lastposter = author;
            thread.replies = 0;
            thread.subject = subject;
            thread.views = 0;
            thread.tid = tid;

            PostModel post = new PostModel();
            post.author = author;
            post.authorid = authorid;
            post.dateline = thread.dateline;
            post.fid = thread.fid;
            post.first = 1;
            post.message = message;
            post.pid = pid;
            post.position = 1;
            post.subject = subject;
            post.tid = tid;
            post.useip = useip;
            return InsertThread(mysqlConnectionString, pre, thread, post);
        }

        private int InsertThread(string mysqlConnectionString, string pre, ThreadModel thread, PostModel post)
        {
            int result = 0;
            string threadSql = "insert into " + pre + "forum_thread (tid,fid,author,authorid,subject,dateline,lastpost,lastposter,views,replies)";
            threadSql += " values (@tid,@fid,@author,@authorid,@subject,@dateline,@lastpost,@lastposter,@views,@replies)";

            string postSql = "insert into " + pre + "forum_post(pid,fid,tid,first,author,authorid,subject,dateline,message,useip,position) ";
            postSql += "values(@pid,@fid,@tid,@first,@author,@authorid,@subject,@dateline,@message,@useip,@position)";

            DbConnection conn = new MySql.Data.MySqlClient.MySqlConnection(mysqlConnectionString);
            conn.Open();
            DbTransaction tx = conn.BeginTransaction();
            try
            {
                DbCommand cmd = conn.CreateCommand();
                cmd.Transaction = tx;
                cmd.CommandText = threadSql;
                foreach (PropertyInfo pi in thread.GetType().GetProperties())
                {
                    DbParameter parameter = cmd.CreateParameter();
                    parameter.ParameterName = "@" + pi.Name;
                    parameter.Value = pi.GetValue(thread, null);
                    cmd.Parameters.Add(parameter);
                }
                result += cmd.ExecuteNonQuery();

                cmd.Parameters.Clear();
                cmd.CommandText = postSql;
                foreach (PropertyInfo pi in post.GetType().GetProperties())
                {
                    DbParameter parameter = cmd.CreateParameter();
                    parameter.ParameterName = "@" + pi.Name;
                    parameter.Value = pi.GetValue(post, null);
                    cmd.Parameters.Add(parameter);
                }

                result += cmd.ExecuteNonQuery();

                string forumUpdateSql = "update " + pre + "forum_forum set threads=threads+1,posts=posts+1,lastpost='{0}' where fid={1}";
                forumUpdateSql = String.Format(forumUpdateSql, thread.tid + "\t" + thread.subject + "\t" + thread.dateline + "\t" + thread.author, thread.fid);
                cmd.CommandText = forumUpdateSql;
                result += cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                result = 0;
                tx.Rollback();
            }
            finally
            {
                conn.Close();
            }

            return result;
        }
        #endregion

        #region 添加用户
        public int InsertMember(string mysqlConnectionString,string pre, string username,string password)
        {
            int result = 0;
             DbConnection conn = new MySql.Data.MySqlClient.MySqlConnection(mysqlConnectionString);
            conn.Open();
            DbCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select (case WHEN EXISTS(select 1 from "+pre+"ucenter_members where username='"+username+"') THEN 1 ELSE 0 END) a";
            if (Convert.ToInt32(cmd.ExecuteScalar()) > 0)
            {
                throw new Exception("用户"+username+"已存在");
            }
            cmd.CommandText = "select max(uid) from " + pre + "ucenter_members";
            int uid = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
            string salt = Guid.NewGuid().ToString("N").Substring(0,6);
            int dateline=(int)ConvertDateTimeInt(DateTime.Now);
            password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "md5").ToLower() + salt, "md5").ToLower();
            string sql1 =String.Format( "insert into " + pre + "ucenter_members (uid,username,password,regdate,salt) values ({0},'{1}','{2}',{3},'{4}')",uid, username,password,dateline,salt);
            string sql2 = "insert into " + pre + "common_member(uid,username,regdate) ";
            sql2 += "values({0},'{1}',{2})";
            sql2 = String.Format(sql2,uid,username,dateline);
          
            DbTransaction tx = conn.BeginTransaction();
            try
            {
                cmd.Transaction = tx;
                cmd.CommandText = sql1;
                result += cmd.ExecuteNonQuery();
                cmd.CommandText = sql2;
                result += cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                result = 0;
                tx.Rollback();
            }
            finally
            {
                conn.Close();
            }

            return result;
        }
        #endregion

 


上一篇:c#调用迅雷下载引擎

下一篇:个人代码全部开源


0 评论

查看所有评论

给个评论吧