日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

c winform 上传文件到mysql_C# winform DevExpress上传图片到数据库【转】

發布時間:2025/3/11 数据库 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c winform 上传文件到mysql_C# winform DevExpress上传图片到数据库【转】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

實現功能如下圖:

注明:此文使用的是DevExpress控件,winform 原生控件也是一樣使用方法。

1.點擊選擇圖片按鈕,功能為通過對話框選擇要上傳的文件,并將該文件在下面的PictureEdit中顯示出來。具體代碼如下:

private void btnChoosePic_Click(object sender, EventArgs e)

{

ShowPic(pictureEdit1);

}

///

/// 選擇圖片

///

///

public static void ShowPic(PictureEdit picEdit)

{

OpenFileDialog ofd = new OpenFileDialog();

ofd.InitialDirectory = @"C:\";

ofd.Filter = "Image Files(*.JPG;*.PNG;*.jpeg;*.GIF;*.BMP)|*.JPG;*.PNG;*.GIF;*.BMP;*.jpeg|All files(*.*)|*.*";

ofd.RestoreDirectory = true;

if (ofd.ShowDialog() == DialogResult.OK)

{

PicAddress = ofd.FileName;

Image imge = Image.FromFile(PicAddress);

Bitmap bm = new Bitmap(imge, picEdit.Width, picEdit.Height);

picEdit.Image = bm;

}

}

ShowPic()方法為靜態方法,可以直接調用,其中的PicAddress變量為靜態全局變量,用于記錄要上傳文件的文件地址。PictureEdit顯示圖片的方式,是通過PictureEdit的image屬性設定的,將圖片轉成Bitmap格式,位圖文件是最簡單的圖片格式。

2.上傳圖片,該按鈕的功能是將選定的圖片上傳到數據庫中,具體的實現代碼如下:

///

/// 上傳圖片

///

///

///

private void btnUploadPic_Click(object sender, EventArgs e)

{

if (PicAddress != null)

{

if (PicType.Equals("教師"))

{

var sqlSearch =

$@"select count(*) from studentmanager.picture where PicTypeId = '{TeacherId}'

and PicType='{PicType}'";

var dsSearch = _db.GetResult(sqlSearch);

if (dsSearch.Tables[0].Rows[0][0].ToString().Equals("0")) //沒有重復的,則進行新增插入操作

{

byte[] pic = CommonFunction.GetContent(PicAddress);

var result = _db.SavePictureToDB(pic, PicAddress, PicType, TeacherId);

if (result > 0)

{

CommonFunction.MessageShow("頭像添加成功", "提示", "OK", "Information");

DialogResult = DialogResult.OK;

}

else

{

CommonFunction.MessageShow("頭像添加失敗");

}

}

else

{

//更新頭像

if (PicAddress.Equals(String.Empty))

{

CommonFunction.MessageShow("沒有重新選擇圖片進行更新");

return;

}

byte[] pic = CommonFunction.GetContent(PicAddress);

var result = _db.UpdatePictureToDb(pic, PicAddress, PicType, TeacherId);

if (result > 0)

{

CommonFunction.MessageShow("頭像更新成功", "提示", "OK", "Information");

DialogResult = DialogResult.OK;

}

else

{

CommonFunction.MessageShow("頭像更新失敗");

}

}

}

else if(PicType.Equals("學生"))

{

var sqlSearch =

$@"select count(*) from studentmanager.picture where PicTypeId = '{StudentId}'

and PicType='{PicType}'";

var dsSearch = _db.GetResult(sqlSearch);

if (dsSearch.Tables[0].Rows[0][0].ToString().Equals("0")) //沒有重復的,則進行新增插入操作

{

byte[] pic = CommonFunction.GetContent(PicAddress);

var result = _db.SavePictureToDB(pic, PicAddress, PicType, StudentId);

if (result > 0)

{

CommonFunction.MessageShow("頭像添加成功", "提示", "OK", "Information");

DialogResult = DialogResult.OK;

}

else

{

CommonFunction.MessageShow("頭像添加失敗");

}

}

else

{

//更新頭像

if (PicAddress.Equals(String.Empty))

{

CommonFunction.MessageShow("沒有重新選擇圖片進行更新");

return;

}

byte[] pic = CommonFunction.GetContent(PicAddress);

var result = _db.UpdatePictureToDb(pic, PicAddress, PicType, StudentId);

if (result > 0)

{

CommonFunction.MessageShow("頭像更新成功", "提示", "OK", "Information");

DialogResult = DialogResult.OK;

}

else

{

CommonFunction.MessageShow("頭像更新失敗");

}

}

}

}

else

{

CommonFunction.MessageShow("請先選擇圖片!", "提示", "OK", "Error");

}

}

上傳的過程大概就是:根據文件地址將對應文件轉換成數據流二進制格式–>編寫對應的SQL語句–>執行該SQL語句,將圖片添加到數據庫中。

上面代碼中SavePictureToDB方法代碼如下:

///

/// 保存圖片到數據庫

///

///

///

///

///

///

///

public int SavePictureToDB(byte[] imageByte,

string Picturename,

string PicType,

int PicTypeId)

{

var result = 0;

try

{

if (imageByte != null && imageByte.Length != 0)

{

using (var conn = new MySqlConnection())

{

conn.ConnectionString = ConnectionString;

conn.Open();

var insertStr = @"INSERT INTO studentmanager.picture

(

Picturename,

PicType,

PicTypeId,

imageByte

)

VALUES

(

@Picturename,

@PicType,

@PicTypeId,

@imageByte

);";

var comm = new MySqlCommand();

comm.Connection = conn;

comm.CommandText = insertStr;

comm.CommandType = CommandType.Text;

//設置數據庫字段類型MediumBlob的值為圖片字節數組imageByte

comm.Parameters.Add(new MySqlParameter("@imageByte", MySqlDbType.MediumBlob)).Value = imageByte;

comm.Parameters.Add(new MySqlParameter("@Picturename", MySqlDbType.VarChar)).Value = Picturename;

comm.Parameters.Add(new MySqlParameter("@PicType", MySqlDbType.VarChar)).Value = PicType;

comm.Parameters.Add(new MySqlParameter("@PicTypeId", MySqlDbType.Int32)).Value = PicTypeId;

//execute sql

result = comm.ExecuteNonQuery();

comm.Dispose();

conn.Close();

conn.Dispose();

}

}

}

catch (Exception)

{

// throw ex;

}

return result;

}

3.加載圖片顯示到PictureEdit;

///

/// 窗口加載

///

///

///

private void HeadManager_Load(object sender, EventArgs e)

{

LoadImage(PicType, PicType.Equals("教師") ? TeacherId : StudentId);

}

///

/// 獲取圖片

///

///

///

private void LoadImage(string picType, int picTypeid)

{

try

{

var imageBytes = _db.GetImage(picType, picTypeid);

var image = CommonFunction.GetImageByBytes(imageBytes);

Bitmap bm = new Bitmap(image, pictureEdit1.Width, pictureEdit1.Height);

pictureEdit1.Image = bm;

}

catch (Exception)

{

pictureEdit1.Image = Resource.DefaultUser;

}

}

4.用到的公共方法:

///

/// 轉換為Byte[]

///

///

///

public static byte[] GetContent(string filepath)//將指定路徑下的文件轉換成二進制代碼,用于傳輸到數據庫

{

FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);

byte[] byData = new byte[fs.Length];//新建用于保存文件流的字節數組

fs.Read(byData, 0, byData.Length);//讀取文件流

fs.Close();

return byData;

}

///

/// 讀取byte[]并轉化為圖片

///

/// byte[]

/// Image

public static Image GetImageByBytes(byte[] bytes)

{

Image photo;

using (MemoryStream ms = new MemoryStream(bytes))

{

ms.Write(bytes, 0, bytes.Length);

photo = Image.FromStream(ms, true);

ms.Dispose();

ms.Close();

}

return photo;

}

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的c winform 上传文件到mysql_C# winform DevExpress上传图片到数据库【转】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。