在已经完成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 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;
}