C#窗口应用程序图像上传到数据库中 [英] C# window application image upload in database
本文介绍了C#窗口应用程序图像上传到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hello Sir / medam,
我想在数据库中上传图片,并在前端获取此图片。我能怎么做 ??????给我代码和说明...
回复我.....
谢谢你...
Hello Sir/medam,
I want to upload image in database and also fetch this image in front end. How can i do ?????? Give me code and instruction for that...
Reply me.....
Thank you...
推荐答案
上传
To Upload
SqlConnection cn= new SqlConnection(connectionString);
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg";
if (open.ShowDialog() == DialogResult.OK)
{
textBoximage.Text = open.FileName;
}
cn.Open();
string image = textBoximage.Text;
Bitmap bmp = new Bitmap(image);
FileStream fs = new FileStream(image, FileMode.Open, FileAccess.Read);
byte[] bimage = new byte[fs.Length];
fs.Read(bimage, 0, Convert.ToInt32(fs.Length));
fs.Close();
SqlCommand cmd = new SqlCommand("insert into TableName(imgdata) values(@imgdata)",cn);
cmd.Parameters.AddWithValue("@imgdata",SqlDbType.Image).Value=bimage;
cmd.ExecuteNonQuery();
cn.Close();
}
要检索
To Retrieve
cn.Open();
SqlDataAdapter da = new SqlDataAdapter(new SqlCommand("Select Picture From TableName where FieldName=condition ", cn));
DataSet ds = new DataSet();
da.Fill(ds);
byte[] myImage = new byte[0];
myImage = (byte[])ds.Tables[0].Rows[0]["Picture"];
MemoryStream stream = new MemoryStream(myImage);
pictureBox1.Image = Image.FromStream(stream);
cn.Close();
试试这个...... :)
http://www.dotnetspider.com/forum/161046-C-upload-image .aspx [ ^ ]
try this...:)
http://www.dotnetspider.com/forum/161046-C-upload-image.aspx[^]
我假设你使用sql server作为数据库。代码下面的代码。
I assume you used sql server as database.Follow below code.
byte[] imag = File.ReadAllBytes("your image path");
SqlCommand cmd = new SqlCommand("INSERT INTO Table(Image) VALUES(@Image)", your connection);
SqlCommand.Parameters.AddWithValue("@Image", imag);
cmd.ExecuteNonQuery();
您还可以在这里查看精彩文章
使用C#向/从Microsoft发送/接收PictureBox图像SQL SERVER [ ^ ]
问候......:笑:
You can also have a look at wonderful article here
Sending/Receiving PictureBox Image in C# To/From Microsoft SQL SERVER[^]
Regards..:laugh:
这篇关于C#窗口应用程序图像上传到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文