首页 > 编程语言 >一个练习项目,好玩的bbs-c#

一个练习项目,好玩的bbs-c#

时间:2024-06-21 15:09:35浏览次数:22  
标签:string c# bbs retinfo1 Add reader new 好玩 id

c#代码:

using MySql.Data.MySqlClient;
using System.Data;
using Newtonsoft.Json;
using System.Security.Cryptography;
using System.Text;

int pagesize = 20;
string secretKey = "saacac3423@21212";

var builder = WebApplication.CreateSlimBuilder(args);
var app = builder.Build();
app.UseHttpsRedirection();
app.Use((context, next) =>
{
    context.Response.Headers.Add("Server", "c#-webframework");
    return next(context);
});

app.MapGet("/", () => {
    string retstr = "此站接口使用c#实现,<a href='api.html' target='_blank'>接口列表</a>";

    return Results.Text(retstr);
});

app.MapGet("/user/register", (string username, string password, string nickname) => {
    string mysqlcon = "server=127.0.0.1;database=my_bbs;user=root;password=123456";
    MySqlConnection conn = new MySqlConnection(mysqlcon);
    conn.Open();
    MySqlCommand cmd;
    MySqlDataReader reader;

    string sql1 = "select id,username,nickname,addTime,sessionId from user where username='" + username + "'";
    cmd = new MySqlCommand(sql1, conn);
    reader = cmd.ExecuteReader();

    Dictionary<string, string> userinfo1 = new Dictionary<string, string>();
    if (reader.Read())
    {
        userinfo1.Add("id", reader.GetInt32("id").ToString());
        userinfo1.Add("username", reader.GetString("username"));
        userinfo1.Add("nickname", reader.GetString("nickname"));
        userinfo1.Add("addTime", reader.GetDateTime("addTime").ToString("yyyy-MM-dd HH:mm:ss"));
        userinfo1.Add("sessionId", reader.GetString("sessionId"));
    }
    reader.Close();

    RetinfoSp retinfo1 = new RetinfoSp();

    if (userinfo1.ContainsKey("id") && String.Compare(userinfo1["id"], "") != 0)
    {
        retinfo1.code = 1;
        retinfo1.msg = "用户名已经存在";
        retinfo1.data = "";
    }
    else
    {
        string passwordMd5 = string.Join("", MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(password)).Select(m => m.ToString("X2"))); 
        string sql2 = "insert into user(username, password, nickname) value('"+username+"', '"+ passwordMd5 + "', '"+nickname+"')";
        cmd = new MySqlCommand(sql2, conn);
        cmd.ExecuteNonQuery();

        retinfo1.code = 0;
        retinfo1.msg = "";
        retinfo1.data = cmd.LastInsertedId.ToString();
    }

    conn.Clone();

    string json = JsonConvert.SerializeObject(retinfo1);

    return Results.Text(json);
});

app.MapGet("/user/login", (string username, string password) => {
    string passwordMd5 = string.Join("", MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(password)).Select(m => m.ToString("X2")));
    string mysqlcon = "server=127.0.0.1;database=my_bbs;user=root;password=123456";
    MySqlConnection conn = new MySqlConnection(mysqlcon);
    conn.Open();
    MySqlCommand cmd;
    MySqlDataReader reader;

    string sql1 = "select id,username,nickname,addTime,sessionId from user where username='" + username + "' and password='"+ passwordMd5 + "'";
    cmd = new MySqlCommand(sql1, conn);
    reader = cmd.ExecuteReader();

    Dictionary<string, string> userinfo1 = new Dictionary<string, string>();
    if (reader.Read())
    {
        userinfo1.Add("id", reader.GetInt32("id").ToString());
        userinfo1.Add("username", reader.GetString("username"));
        userinfo1.Add("nickname", reader.GetString("nickname"));
        userinfo1.Add("addTime", reader.GetDateTime("addTime").ToString("yyyy-MM-dd HH:mm:ss"));
        userinfo1.Add("sessionId", reader.GetString("sessionId"));
    }
    reader.Close();

    Retinfo retinfo1 = new Retinfo();

    if (!userinfo1.ContainsKey("id") || String.Compare(userinfo1["id"], "") == 0)
    {
        retinfo1.code = 1;
        retinfo1.msg = "用户名或者密码错误";
        retinfo1.data = userinfo1;
    }
    else
    {
        string tmpSessionId = string.Join("", MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(secretKey + userinfo1["id"] + userinfo1["addTime"])).Select(m => m.ToString("X2")));
        string sql2 = "update user set sessionId='" + tmpSessionId + "' where id='" + userinfo1["id"] + "'";
        cmd = new MySqlCommand(sql2, conn);
        cmd.ExecuteNonQuery();
        userinfo1["sessionId"] = tmpSessionId;

        retinfo1.code = 0;
        retinfo1.msg = "";
        retinfo1.data = userinfo1;
    }

    conn.Clone();

    string json = JsonConvert.SerializeObject(retinfo1);

    return Results.Text(json);
});

app.MapGet("/user/logout", (string sessionId) => {
    string mysqlcon = "server=127.0.0.1;database=my_bbs;user=root;password=123456";
    MySqlConnection conn = new MySqlConnection(mysqlcon);
    conn.Open();
    MySqlCommand cmd;
    MySqlDataReader reader;

    string sql1 = "select id,username,nickname,addTime,sessionId from user where sessionId='" + sessionId + "'";
    cmd = new MySqlCommand(sql1, conn);
    reader = cmd.ExecuteReader();

    Dictionary<string, string> userinfo1 = new Dictionary<string, string>();
    if (reader.Read())
    {
        userinfo1.Add("id", reader.GetInt32("id").ToString());
        userinfo1.Add("username", reader.GetString("username"));
        userinfo1.Add("nickname", reader.GetString("nickname"));
        userinfo1.Add("addTime", reader.GetDateTime("addTime").ToString("yyyy-MM-dd HH:mm:ss"));
        userinfo1.Add("sessionId", reader.GetString("sessionId"));
    }
    reader.Close();

    string sql2 = "update user set sessionId='' where sessionId='" + sessionId + "'";
    cmd = new MySqlCommand(sql2, conn);
    cmd.ExecuteNonQuery();

    Retinfo retinfo1 = new Retinfo();
    retinfo1.code = 0;
    retinfo1.msg = "";
    retinfo1.data = userinfo1;

    conn.Clone();

    string json = JsonConvert.SerializeObject(retinfo1);

    return Results.Text(json);
});

app.MapGet("/user/getuserinfo", (string sessionId) => {
    string mysqlcon = "server=127.0.0.1;database=my_bbs;user=root;password=123456";
    MySqlConnection conn = new MySqlConnection(mysqlcon);
    conn.Open();
    MySqlCommand cmd;
    MySqlDataReader reader;

    string sql1 = "select id,username,nickname,addTime,sessionId from user where sessionId='" + sessionId + "'";
    cmd = new MySqlCommand(sql1, conn);
    reader = cmd.ExecuteReader();

    Dictionary<string, string> userinfo1 = new Dictionary<string, string>();
    if (reader.Read()){
        userinfo1.Add("id", reader.GetInt32("id").ToString());
        userinfo1.Add("username", reader.GetString("username"));
        userinfo1.Add("nickname", reader.GetString("nickname"));
        userinfo1.Add("addTime", reader.GetDateTime("addTime").ToString("yyyy-MM-dd HH:mm:ss"));
        userinfo1.Add("sessionId", reader.GetString("sessionId"));
    }
    reader.Close();

    Retinfo retinfo1 = new Retinfo();
    retinfo1.code = 0;
    retinfo1.msg = "";
    retinfo1.data = userinfo1;

    conn.Clone();

    string json = JsonConvert.SerializeObject(retinfo1);

    return Results.Text(json);
});

app.MapGet("/post/list", (string keyword = "", int page = 1) => {
    if (page <= 0)
    {
        page = 1;
    }

    int start = (page - 1) * pagesize;

    string addsql = " isDel=0 ";
    if (String.Compare(keyword, "") != 0)
    {
        addsql = " isDel=0 and title like '%" + keyword + "%' ";
    }

    string mysqlcon = "server=127.0.0.1;database=my_bbs;user=root;password=123456";
    MySqlConnection conn = new MySqlConnection(mysqlcon);
    conn.Open();
    MySqlCommand cmd;
    MySqlDataReader reader;

    string sql1 = "select count(1) as count from content where " + addsql;
    cmd = new MySqlCommand(sql1, conn);
    reader = cmd.ExecuteReader();

    int count = 0;
    if (reader.Read())
    {
        count = reader.GetInt32("count");
    }
    reader.Close();

    int totalpage = Convert.ToInt32(Math.Ceiling((double)count / (double)pagesize));

    string sql2 = "select id,title,userId,userNickename,replyNum,updateTime from content where " + addsql + " order by updateTime desc limit " + start + "," + pagesize;
    cmd = new MySqlCommand(sql2, conn);
    reader = cmd.ExecuteReader();

    List<Dictionary<string, string>> postlist = new List<Dictionary<string, string>>();
    while (reader.Read())
    {
        Dictionary<string, string> postitem = new Dictionary<string, string>();
        postitem.Add("id", reader.GetInt32("id").ToString());
        postitem.Add("title", reader.GetString("title"));
        postitem.Add("userId", reader.GetInt32("userId").ToString());
        postitem.Add("userNickename", reader.GetString("userNickename"));
        postitem.Add("replyNum", reader.GetInt32("replyNum").ToString());
        postitem.Add("updateTime", reader.GetDateTime("updateTime").ToString("yyyy-MM-dd HH:mm:ss"));
        postlist.Add(postitem);
    }
    reader.Close();

    Listdata listdata1 = new Listdata();
    listdata1.totalpage = totalpage;
    listdata1.data = postlist;

    Retlist retlist1 = new Retlist();
    retlist1.code = 0;
    retlist1.msg = "";
    retlist1.data = listdata1;

    conn.Clone();

    string json = JsonConvert.SerializeObject(retlist1);

    return Results.Text(json);
});

app.MapGet("/post/detail", (int id) => {
    string mysqlcon = "server=127.0.0.1;database=my_bbs;user=root;password=123456";
    MySqlConnection conn = new MySqlConnection(mysqlcon);
    conn.Open();
    MySqlCommand cmd;
    MySqlDataReader reader;

    string sql1 = "select id,title,content,userId,userNickename,replyNum,updateTime from content where isDel=0 and id="+ id.ToString();
    cmd = new MySqlCommand(sql1, conn);
    reader = cmd.ExecuteReader();

    Dictionary<string, string> postinfo1 = new Dictionary<string, string>();
    if (reader.Read())
    {
        postinfo1.Add("id", reader.GetInt32("id").ToString());
        postinfo1.Add("title", reader.GetString("title"));
        postinfo1.Add("content", reader.GetString("content"));
        postinfo1.Add("userId", reader.GetInt32("userId").ToString());
        postinfo1.Add("userNickename", reader.GetString("userNickename"));
        postinfo1.Add("replyNum", reader.GetInt32("replyNum").ToString());
        postinfo1.Add("updateTime", reader.GetDateTime("updateTime").ToString("yyyy-MM-dd HH:mm:ss"));
    }
    reader.Close();

    Retinfo retinfo1 = new Retinfo();
    retinfo1.code = 0;
    retinfo1.msg = "";
    retinfo1.data = postinfo1;

    conn.Clone();

    string json = JsonConvert.SerializeObject(retinfo1);

    return Results.Text(json);
});

app.MapGet("/post/add", (string title, string content, string sessionId) => {
    string mysqlcon = "server=127.0.0.1;database=my_bbs;user=root;password=123456";
    MySqlConnection conn = new MySqlConnection(mysqlcon);
    conn.Open();
    MySqlCommand cmd;
    MySqlDataReader reader;

    Userinfo userinfo1 = new Userinfo();
    Dictionary<string, string> userinfo2 = userinfo1.getloginuserinfo(sessionId);
    RetinfoSp retinfo1 = new RetinfoSp();
    string json = "";

    if (!userinfo2.ContainsKey("id") || String.Compare(userinfo2["id"], "") == 0)
    {
        retinfo1.code = 0;
        retinfo1.msg = "";
        retinfo1.data = "请先登录";
        json = JsonConvert.SerializeObject(retinfo1);
    }
    else
    {
        string sql1 = "insert into content(title, content, userId, userNickename) value('" + title + "', '" + content + "', " + userinfo2["id"] + ", '" + userinfo2["nickname"] + "')";
        cmd = new MySqlCommand(sql1, conn);
        cmd.ExecuteNonQuery();

        retinfo1.code = 0;
        retinfo1.msg = "";
        retinfo1.data = cmd.LastInsertedId.ToString();
        json = JsonConvert.SerializeObject(retinfo1);
    }

    conn.Clone();

    return Results.Text(json);
});

app.MapGet("/post/edit", (int id, string title, string content, string sessionId) => {
    string mysqlcon = "server=127.0.0.1;database=my_bbs;user=root;password=123456";
    MySqlConnection conn = new MySqlConnection(mysqlcon);
    conn.Open();
    MySqlCommand cmd;
    MySqlDataReader reader;

    Userinfo userinfo1 = new Userinfo();
    Dictionary<string, string> userinfo2 = userinfo1.getloginuserinfo(sessionId);
    RetinfoSp retinfo1 = new RetinfoSp();
    string json = "";

    if (!userinfo2.ContainsKey("id") || String.Compare(userinfo2["id"], "") == 0)
    {
        retinfo1.code = 0;
        retinfo1.msg = "";
        retinfo1.data = "请先登录";
        json = JsonConvert.SerializeObject(retinfo1);
    }
    else
    {
        string sql1 = "update content set title='"+title+"',content='"+content+"',userId="+ userinfo2["id"].ToString() + ",userNickename='"+ userinfo2["nickname"] + "' where id="+ id.ToString()+ " and userId="+ userinfo2["id"].ToString();
        cmd = new MySqlCommand(sql1, conn);
        cmd.ExecuteNonQuery();

        retinfo1.code = 0;
        retinfo1.msg = "";
        retinfo1.data = "";
        json = JsonConvert.SerializeObject(retinfo1);
    }

    conn.Clone();

    return Results.Text(json);
});

app.MapGet("/post/delete", (int id, string sessionId) => {
    string mysqlcon = "server=127.0.0.1;database=my_bbs;user=root;password=123456";
    MySqlConnection conn = new MySqlConnection(mysqlcon);
    conn.Open();
    MySqlCommand cmd;
    MySqlDataReader reader;

    Userinfo userinfo1 = new Userinfo();
    Dictionary<string, string> userinfo2 = userinfo1.getloginuserinfo(sessionId);
    RetinfoSp retinfo1 = new RetinfoSp();
    string json = "";

    if (!userinfo2.ContainsKey("id") || String.Compare(userinfo2["id"], "") == 0)
    {
        retinfo1.code = 0;
        retinfo1.msg = "";
        retinfo1.data = "请先登录";
        json = JsonConvert.SerializeObject(retinfo1);
    }
    else
    {
        string sql1 = "update content set isDel=1 where id=" + id.ToString() + " and userId=" + userinfo2["id"].ToString();
        cmd = new MySqlCommand(sql1, conn);
        cmd.ExecuteNonQuery();

        retinfo1.code = 0;
        retinfo1.msg = "";
        retinfo1.data = "";
        json = JsonConvert.SerializeObject(retinfo1);
    }

    conn.Clone();

    return Results.Text(json);
});

app.MapGet("/reply/list", (int contentId = 0, int page = 1) => {
    if (page <= 0)
    {
        page = 1;
    }

    int start = (page - 1) * pagesize;

    string addsql = " isDel=0 and contentId="+ contentId.ToString() + " ";

    string mysqlcon = "server=127.0.0.1;database=my_bbs;user=root;password=123456";
    MySqlConnection conn = new MySqlConnection(mysqlcon);
    conn.Open();
    MySqlCommand cmd;
    MySqlDataReader reader;

    string sql1 = "select count(1) as count from reply where " + addsql;
    cmd = new MySqlCommand(sql1, conn);
    reader = cmd.ExecuteReader();

    int count = 0;
    if (reader.Read())
    {
        count = reader.GetInt32("count");
    }
    reader.Close();

    int totalpage = Convert.ToInt32(Math.Ceiling((double)count / (double)pagesize));

    string sql2 = "select id,content,replyUserId,replyUserNickename,addTime from reply where " + addsql + " order by id asc limit " + start + "," + pagesize;
    cmd = new MySqlCommand(sql2, conn);
    reader = cmd.ExecuteReader();

    List<Dictionary<string, string>> postlist = new List<Dictionary<string, string>>();
    while (reader.Read())
    {
        Dictionary<string, string> postitem = new Dictionary<string, string>();
        postitem.Add("id", reader.GetInt32("id").ToString());
        postitem.Add("content", reader.GetString("content"));
        postitem.Add("replyUserId", reader.GetInt32("replyUserId").ToString());
        postitem.Add("replyUserNickename", reader.GetString("replyUserNickename"));
        postitem.Add("addTime", reader.GetDateTime("addTime").ToString("yyyy-MM-dd HH:mm:ss"));
        postlist.Add(postitem);
    }
    reader.Close();

    Listdata listdata1 = new Listdata();
    listdata1.totalpage = totalpage;
    listdata1.data = postlist;

    Retlist retlist1 = new Retlist();
    retlist1.code = 0;
    retlist1.msg = "";
    retlist1.data = listdata1;

    conn.Clone();

    string json = JsonConvert.SerializeObject(retlist1);

    return Results.Text(json);
});

app.MapGet("/reply/detail", (int id) => {
    string mysqlcon = "server=127.0.0.1;database=my_bbs;user=root;password=123456";
    MySqlConnection conn = new MySqlConnection(mysqlcon);
    conn.Open();
    MySqlCommand cmd;
    MySqlDataReader reader;

    string sql1 = "select id,content,replyUserId,replyUserNickename,addTime from reply where isDel=0 and id=" + id.ToString();
    cmd = new MySqlCommand(sql1, conn);
    reader = cmd.ExecuteReader();

    Dictionary<string, string> postinfo1 = new Dictionary<string, string>();
    if (reader.Read())
    {
        postinfo1.Add("id", reader.GetInt32("id").ToString());
        postinfo1.Add("content", reader.GetString("content"));
        postinfo1.Add("replyUserId", reader.GetInt32("replyUserId").ToString());
        postinfo1.Add("replyUserNickename", reader.GetString("replyUserNickename"));
        postinfo1.Add("addTime", reader.GetDateTime("addTime").ToString("yyyy-MM-dd HH:mm:ss"));
    }
    reader.Close();

    Retinfo retinfo1 = new Retinfo();
    retinfo1.code = 0;
    retinfo1.msg = "";
    retinfo1.data = postinfo1;

    conn.Clone();

    string json = JsonConvert.SerializeObject(retinfo1);

    return Results.Text(json);
});

app.MapGet("/reply/add", (int contentId, string content, string sessionId) => {
    string mysqlcon = "server=127.0.0.1;database=my_bbs;user=root;password=123456";
    MySqlConnection conn = new MySqlConnection(mysqlcon);
    conn.Open();
    MySqlCommand cmd;
    MySqlDataReader reader;

    Userinfo userinfo1 = new Userinfo();
    Dictionary<string, string> userinfo2 = userinfo1.getloginuserinfo(sessionId);
    RetinfoSp retinfo1 = new RetinfoSp();
    string json = "";

    if (!userinfo2.ContainsKey("id") || String.Compare(userinfo2["id"], "") == 0)
    {
        retinfo1.code = 0;
        retinfo1.msg = "";
        retinfo1.data = "请先登录";
        json = JsonConvert.SerializeObject(retinfo1);
    }
    else
    {
        string sql2 = "update content set replyNum=replyNum+1 where id="+contentId.ToString();
        cmd = new MySqlCommand(sql2, conn);
        cmd.ExecuteNonQuery();

        string sql1 = "insert into reply(contentId, content, replyUserId, replyUserNickename) value("+ contentId.ToString() + ", '"+ content + "', "+ userinfo2["id"] + ", '"+ userinfo2["nickname"] + "')";
        cmd = new MySqlCommand(sql1, conn);
        cmd.ExecuteNonQuery();

        retinfo1.code = 0;
        retinfo1.msg = "";
        retinfo1.data = cmd.LastInsertedId.ToString();
        json = JsonConvert.SerializeObject(retinfo1);
    }

    conn.Clone();

    return Results.Text(json);
});

app.MapGet("/reply/edit", (int id, string content, string sessionId) => {
    string mysqlcon = "server=127.0.0.1;database=my_bbs;user=root;password=123456";
    MySqlConnection conn = new MySqlConnection(mysqlcon);
    conn.Open();
    MySqlCommand cmd;
    MySqlDataReader reader;

    Userinfo userinfo1 = new Userinfo();
    Dictionary<string, string> userinfo2 = userinfo1.getloginuserinfo(sessionId);
    RetinfoSp retinfo1 = new RetinfoSp();
    string json = "";

    if (!userinfo2.ContainsKey("id") || String.Compare(userinfo2["id"], "") == 0)
    {
        retinfo1.code = 0;
        retinfo1.msg = "";
        retinfo1.data = "请先登录";
        json = JsonConvert.SerializeObject(retinfo1);
    }
    else
    {
        string sql1 = "update reply set content='"+ content + "',replyUserId="+ userinfo2["id"] + ",replyUserNickename='"+ userinfo2["nickname"] + "' where id="+id.ToString()+" and replyUserId=" + userinfo2["id"];
        cmd = new MySqlCommand(sql1, conn);
        cmd.ExecuteNonQuery();

        retinfo1.code = 0;
        retinfo1.msg = "";
        retinfo1.data = "";
        json = JsonConvert.SerializeObject(retinfo1);
    }

    conn.Clone();

    return Results.Text(json);
});

app.MapGet("/reply/delete", (int id, string sessionId) => {
    string mysqlcon = "server=127.0.0.1;database=my_bbs;user=root;password=123456";
    MySqlConnection conn = new MySqlConnection(mysqlcon);
    conn.Open();
    MySqlCommand cmd;
    MySqlDataReader reader;

    Userinfo userinfo1 = new Userinfo();
    Dictionary<string, string> userinfo2 = userinfo1.getloginuserinfo(sessionId);
    RetinfoSp retinfo1 = new RetinfoSp();
    string json = "";

    if (!userinfo2.ContainsKey("id") || String.Compare(userinfo2["id"], "") == 0)
    {
        retinfo1.code = 0;
        retinfo1.msg = "";
        retinfo1.data = "请先登录";
        json = JsonConvert.SerializeObject(retinfo1);
    }
    else
    {
        string sql3 = "select id,content,replyUserId,replyUserNickename,addTime,contentId from reply where isDel=0 and id=" + id.ToString();
        cmd = new MySqlCommand(sql3, conn);
        reader = cmd.ExecuteReader();

        Dictionary<string, string> postinfo1 = new Dictionary<string, string>();
        if (reader.Read())
        {
            postinfo1.Add("id", reader.GetInt32("id").ToString());
            postinfo1.Add("content", reader.GetString("content"));
            postinfo1.Add("replyUserId", reader.GetInt32("replyUserId").ToString());
            postinfo1.Add("replyUserNickename", reader.GetString("replyUserNickename"));
            postinfo1.Add("addTime", reader.GetDateTime("addTime").ToString("yyyy-MM-dd HH:mm:ss"));
            postinfo1.Add("contentId", reader.GetInt32("contentId").ToString());
        }
        reader.Close();

        if (postinfo1.ContainsKey("id") && String.Compare(postinfo1["id"], "") != 0)
        {
            string sql2 = "update content set replyNum=replyNum-1 where id=" + postinfo1["contentId"];
            cmd = new MySqlCommand(sql2, conn);
            cmd.ExecuteNonQuery();

            string sql1 = "update reply set isDel=1 where id=" + id.ToString() + " and replyUserId=" + userinfo2["id"];
            cmd = new MySqlCommand(sql1, conn);
            cmd.ExecuteNonQuery();

            retinfo1.code = 0;
            retinfo1.msg = "";
            retinfo1.data = "";
            json = JsonConvert.SerializeObject(retinfo1);
        }
        else {
            retinfo1.code = 0;
            retinfo1.msg = "";
            retinfo1.data = "回复不存在";
            json = JsonConvert.SerializeObject(retinfo1);
        }
    }

    conn.Clone();

    return Results.Text(json);
});

app.Urls.Add("http://localhost:2086");
app.Urls.Add("http://localhost:1086");
app.Run();

public struct Listdata
{
    public int totalpage;
    public List<Dictionary<string, string>> data;
};

public struct Retlist
{
    public int code;
    public string msg;
    public Listdata data;
};

public struct Retinfo
{
    public int code;
    public string msg;
    public Dictionary<string, string> data;
};

public struct RetinfoSp
{
    public int code;
    public string msg;
    public string data;
};

class Userinfo {
    public Dictionary<string, string> getloginuserinfo(string sessionId) {
        string mysqlcon = "server=127.0.0.1;database=my_bbs;user=root;password=123456";
        MySqlConnection conn = new MySqlConnection(mysqlcon);
        conn.Open();
        MySqlCommand cmd;
        MySqlDataReader reader;

        string sql1 = "select id,username,nickname,addTime,sessionId from user where sessionId='" + sessionId + "'";
        cmd = new MySqlCommand(sql1, conn);
        reader = cmd.ExecuteReader();

        Dictionary<string, string> userinfo1 = new Dictionary<string, string>();
        if (reader.Read())
        {
            userinfo1.Add("id", reader.GetInt32("id").ToString());
            userinfo1.Add("username", reader.GetString("username"));
            userinfo1.Add("nickname", reader.GetString("nickname"));
            userinfo1.Add("addTime", reader.GetDateTime("addTime").ToString("yyyy-MM-dd HH:mm:ss"));
            userinfo1.Add("sessionId", reader.GetString("sessionId"));
        }
        reader.Close();

        return userinfo1;
    }
}

 

启动画面:

 

标签:string,c#,bbs,retinfo1,Add,reader,new,好玩,id
From: https://www.cnblogs.com/xuxiaobo/p/18260562

相关文章

  • C语言学生管理系统
    C语言学生管理系统主要功能模块主界面其中教师注册和学生注册使用id(学号注册),由管理员录入id信息,若未查询到id被录入,则无法注册支持修改密码,若已经设置过密码,则验证原来密码后,可以修改密码注册函数代码voidregisterUser(constchar*role){UsernewUser;......
  • (nice!!!)LeetCode LCP 20. 快速公交(记忆化搜索+小顶堆+贪心)
    LCP20.快速公交思路:逆向记忆化搜索。思考从target到0所花的最小时间。通过哈希表来进行记忆化搜索,避免重复遍历。细节看注释classSolution{public:typedeflonglongLL;typedefpair<LL,LL>PII;constintmod=1e9+7;intbusRapidTransit(int......
  • 【AIGC新职业】之:什么是AIGC?其对创意设计行业有什么重要意义?
    【AIGC是什么】AIGC,全称ArtificialIntelligenceGeneratedContent,即人工智能生成内容。这是一种基于生成对抗网络、大型预训练模型等人工智能的技术方法,通过已有数据的学习和识别,以适当的泛化能力生成相关内容的技术。AIGC技术的核心思想是利用人工智能算法生成具有一定......
  • Oracle-调度作业
    DBMS_SCHEDULER与DBMS_JOBSDBMS_SCHEDULER与DBMS_JOBS在Oracle数据库中都是用于创建、管理和调度作业的工具,但它们之间存在一些显著的区别和联系。联系:两者都是Oracle数据库中的作业调度工具,用于创建、管理和调度定时任务。区别:DBMS_SCHEDULER功能更为强大,提供了更多的选......
  • ChatmoneyAI如狂龙腾空,客服领域的赚钱神器!
    本文由ChatMoney团队出品引言在客服行业中,提升效率、降低成本是企业永恒的课题。而智能客服正是解决这一难题的利器。借助ChatmoneyAI这款强大的AI工具,企业可以实现智能化的客服管理,通过自动回复常见问题、智能识别用户需求,大大减轻客服团队的工作负担,提高工作效率。与此同时,Ch......
  • 9.0分下跌至2.5分!110本SSCI影响因子下降!经济学领域SSCI最新IF汇总!
    【欧亚科睿学术】2023影响因子:经济学1影响因子概况在170本经济学领域SSCI期刊中,有60本SSCI期刊2023年影响因子上涨。其中,期刊ECONOMICPOLICY由原来的2.5增长到了4.5,涨幅为2.0,也是经济学领域涨幅最大的SSCI期刊。同时,领域热门期刊ECONOMICANALYSISANDPOLICY、ENERG......
  • 一个练习项目,好玩的bbs-1
    目录结构 nginx配置:upstreambbs_upstream{server127.0.0.1:1081;#phpserver127.0.0.1:1086;#csharpeserver127.0.0.1:1087;#javaserver127.0.0.1:1084;#ruby-sinatraserver127.0.0.1:1104;#ruby-busker......
  • Node.js车牌识别、文档识别、OCR API-自动化录入信息
    为什么人工智能如此受关注?因为人工智能技术在图片处理以及在感知与认知等领域的不断突破,带来更高的效率。没错,智能机器人、人工智能技术衍生的产品已经开始替代人的工作。文字录入工作较为显著,OCR技术成为手动录入的杀手锏,图片识别、扫描识别多样化的解决方案层出不穷。......
  • C# 之 .net core -- 创建项目
    原文链接:https://www.cnblogs.com/hkzw/p/12001420.html一、新建一个Web的应用程序 二、选择项目的基本信息(.netcoer2.2 和带有试图控制器的程序)在这里创建的是MVC模式,也可以用其他的,比如空的、web应用程序。跟平常开的的那种表面上差别不大,总之需要什么选什么 ......
  • CentOS 7 安装 docker-compose
    在CentOS7上配置docker-compose需要几个步骤。以下是详细步骤:1、安装DockerCompose:DockerCompose的官方推荐安装方式是使用pip(Python的包管理工具)。但是,首先你需要确保你的系统上安装了Python和pip。使用以下命令安装pip(如果你还没有安装的话):sudoyuminst......