asp.net MVC5 连接mysql数据库并对数据库进行增删查改

在已经完成vs 已经和mysql连接成功的前提下

增加、删除、修改基本一致,除了sql语句

//添加用户 
public static bool Add(user u)
        {
           
            string constr = "Server=localhost;UserId=root;Password=qhj12345;Database=qhj_asp;pooling=false;CharSet=utf8;port=3306";
            MySqlConnection conn = new MySqlConnection(constr);
            try
            {
                conn.Open();
                
                string mySql = "INSERT INTO user(Name,Email,Password) VALUES(@name,@email,@password)";
                MySqlCommand sqlcmd2 = new MySqlCommand(mySql, conn);

                MySqlParameter p1 = new MySqlParameter("@name", MySqlDbType.VarChar);
                p1.Value = u.Name;
                MySqlParameter p2 = new MySqlParameter("@email", MySqlDbType.VarChar);
                p2.Value = u.Email;
                MySqlParameter p3 = new MySqlParameter("@password", MySqlDbType.VarChar);
                p3.Value = u.Password;

                sqlcmd2.Parameters.Add(p1);
                sqlcmd2.Parameters.Add(p2);
                sqlcmd2.Parameters.Add(p3); 

                int iResult = sqlcmd2.ExecuteNonQuery();
                if (iResult > 0)
                    return true;

            }
            catch (Exception ex)
            {
                Debug.WriteLine("Error: " + ex.Message);
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }

            return false;
        }

查询

//根据flag查询作品
        public static List<works> FindArticleByFlag(int id)
        {
            string constr = "Server=localhost;UserId=root;Password=qhj12345;Database=qhj_asp;pooling=false;CharSet=utf8;port=3306";
            MySqlConnection conn = new MySqlConnection(constr);
            try
            {
                conn.Open();
                string mySql = "select * from works where Flag=@id";
                MySqlCommand sqlcmd2 = new MySqlCommand(mySql, conn);
                MySqlParameter p1 = new MySqlParameter("@id", MySqlDbType.Int32);
                p1.Value = id;
                sqlcmd2.Parameters.Add(p1); 

                List<works> worksList = new List<works>();
                
                MySqlDataReader sqlreader = sqlcmd2.ExecuteReader();
                while (sqlreader.Read())
                {
                   
                    works work = new works();
                    work.Wid = Convert.ToInt32(sqlreader["Wid"]);
                    work.Uid = Convert.ToInt32(sqlreader["Uid"]);
                    work.Flag = Convert.ToInt32(sqlreader["Flag"]);
                    work.Context = sqlreader["Context"].ToString();
                    work.Image = sqlreader["Image"].ToString();
                    work.Title = sqlreader["Title"].ToString();
                    work.CoreSentence = sqlreader["CoreSentence"].ToString();
                    work.author = sqlreader["author"].ToString();
                    worksList.Add(work);
                }
                return worksList;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Error: " + ex.Message);
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }

            return null;
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值