日期:2014-05-18 浏览次数:21091 次
private const string connectString = "server=127.0.0.1;uid=root;pwd=root;database=dbname"; public static bool MysqlExcute(MySqlCommand cmd) { MySqlConnection conn = new MySqlConnection(connectString); conn.Open(); cmd.Connection = conn; int rtn = cmd.ExecuteNonQuery(); conn.Close(); return rtn > 0; }
// 读取图片内容 FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); Byte[] imgByte = new Byte[fs.Length]; fs.Read(imgByte, 0, imgByte.Length); // 插入图片 MySqlCommand cmd = new MySqlCommand("INSERT INTO pic(pic_data, upload_username, upload_datetime) " + "VALUES(?pic_name, ?pic_data, ?upload_username, ?upload_datetime)"); cmd.Parameters.AddWithValue("?pic_name", pic_name); cmd.Parameters.AddWithValue("?pic_data", MySqlDbType.VarBinary).Value = imgByte; cmd.Parameters.AddWithValue("?upload_username", Session.username); cmd.Parameters.AddWithValue("?upload_datetime", DateTime.Now.ToString()); if (MySqlHelper.MysqlExcute(cmd)) { // 上传成功 // ... } else { // ... }
MySqlParameter param = new MySqlParameter(); param.MySqlDbType = MySqlDbType.VarBinary; param.Value = imgByte; cmd.Parameters.Add(param);