将图片转换为二进制文件保存到数据库
我使用fileupload上传图片到SQL数据库时,失败。显示错误为不存在从对象类型System.Drawing.Bitmap到已知的托管提供程序本机类型的映射。代码如下...
我使用fileupload上传图片到SQL数据库时,失败。显示错误为不存在从对象类型 System.Drawing.Bitmap 到已知的托管提供程序本机类型的映射。代码如下:
string picName = fulImage.PostedFile.FileName;
FileStream fs = File.OpenRead(picName);
byte[] picContent = new byte[fs.Length];
fs.Read(picContent, 0,Convert .ToInt32 ( picContent.Length));
MemoryStream ms = new MemoryStream(picContent);
System.Drawing.Image image = null;
image = System.Drawing.Image.FromStream(ms);
videoCourse vcourse = new videoCourse();
vcourse.VideoImage = image;
flowMedia.VCourseAdmin vcourseDAO = new flowMedia.VCourseAdmin();
vcourseDAO.InsertVideoCourse(vcourse);
Response.Write("Success!"); 展开
string picName = fulImage.PostedFile.FileName;
FileStream fs = File.OpenRead(picName);
byte[] picContent = new byte[fs.Length];
fs.Read(picContent, 0,Convert .ToInt32 ( picContent.Length));
MemoryStream ms = new MemoryStream(picContent);
System.Drawing.Image image = null;
image = System.Drawing.Image.FromStream(ms);
videoCourse vcourse = new videoCourse();
vcourse.VideoImage = image;
flowMedia.VCourseAdmin vcourseDAO = new flowMedia.VCourseAdmin();
vcourseDAO.InsertVideoCourse(vcourse);
Response.Write("Success!"); 展开
2个回答
展开全部
private void button1_Click(object sender, EventArgs e)
{
Stream ms;
byte[] picbyte;
OpenFileDialog selectPic = new OpenFileDialog();
if (selectPic.ShowDialog() == DialogResult.OK)
{
if ((ms = selectPic.OpenFile()) != null)
{
picbyte = new byte[ms.Length];
ms.Position = 0;
ms.Read(picbyte, 0, Convert.ToInt32(ms.Length));
SqlConnection conn = new SqlConnection();
conn.ConnectionString = sqlconn;
string strsql = "insert into Pic values(@Image)";
SqlCommand cmd = new SqlCommand(strsql,conn);
cmd.Parameters.Add("@Image", SqlDbType.VarBinary);
cmd.Parameters[0].Value = picbyte;
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
throw ex;
}
conn.Close();
}
}
MessageBox.Show("OK");
}
这个是在WinForm中保存图片的例子,点击保存按钮后,弹出选择图片对话框,然后把图片转化为二进制保存在数据库中..
你可以根据你的情况适当修改下就可以使用..
{
Stream ms;
byte[] picbyte;
OpenFileDialog selectPic = new OpenFileDialog();
if (selectPic.ShowDialog() == DialogResult.OK)
{
if ((ms = selectPic.OpenFile()) != null)
{
picbyte = new byte[ms.Length];
ms.Position = 0;
ms.Read(picbyte, 0, Convert.ToInt32(ms.Length));
SqlConnection conn = new SqlConnection();
conn.ConnectionString = sqlconn;
string strsql = "insert into Pic values(@Image)";
SqlCommand cmd = new SqlCommand(strsql,conn);
cmd.Parameters.Add("@Image", SqlDbType.VarBinary);
cmd.Parameters[0].Value = picbyte;
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
throw ex;
}
conn.Close();
}
}
MessageBox.Show("OK");
}
这个是在WinForm中保存图片的例子,点击保存按钮后,弹出选择图片对话框,然后把图片转化为二进制保存在数据库中..
你可以根据你的情况适当修改下就可以使用..
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询