如何使用WPF将图像插入数据库

fem*_*emi 6 sql wpf image insert

我有一个WPF问题.

我在WPF表单中有2个文本框和一个图像控件.图像控件中有图像.

我想在SQL数据库的3个单独的列中插入每个文本框和图像的内容.文本框将输入varchar列,而图像本身则加载到具有数据类型图像的列中.

我怎样才能做到这一点?

谢谢

ibr*_*maz 0

制作2个表,第一个表包含textbox1的文本[可能是“name”],textbox2的文本[可能是“surname”]和imageId[],另一个表包含文件id,文件字节和文件扩展名。当您用图片保存上述信息时。获取图像字节并扩展保存它。当您获取图像以便在某处显示时,您可以通过其扩展名将字节转换为文件 http://www.beansoftware.com/ASP.NET-Tutorials/Save-Read-Image-Database.aspx 在这里用于 ASP.NET 但是.net 中的控件通常是相同的。(textbox.Text 等)

private void Button1_Click(object sender, System.EventArgs e)
{
 Stream img_strm = upload_file.PostedFile.InputStream;

//Retrieving the length of the file to upload
int img_len = upload_file.PostedFile.ContentLength;
//retrieving the type of the file to upload
string strtype = upload_file.PostedFile.ContentType.ToString();
string strname = txtimgname.Text.ToString();
byte[] imgdata = new byte[img_len];
int n = img_strm.Read(imgdata, 0, img_len);
int result = SaveToDB(strname, imgdata, strtype);}
Run Code Online (Sandbox Code Playgroud)