将照片上传到数据库C#

编程入门 行业动态 更新时间:2024-10-22 16:45:04
本文介绍了将照片上传到数据库C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好我使用代码将照片上传到数据库:

//读取文件并将其转换为字节数组 string filepath = upload_image.PostedFile.FileName; string filename = Path.GetFileName(filepath); string ext = Path.GetExtension(filename); string contenttype = string.Empty; switch(ext){ case.doc: contenttype =application / vnd.ms-word; 休息; case.docx: contenttype =application / vnd.ms-word; 休息; case.xls: contenttype =application / vnd.ms-excel; 休息; case.xlsx: contenttype =application / vnd.ms-excel; 休息; case.jpg: contenttype =image / jpg; 休息; case.png: contenttype =image / png; 休息; case.gif: contenttype =image / gif; 休息; case.pdf: contenttype =application / pdf; 休息; } if(contenttype!= String.Empty) { Stream fs = upload_image.PostedFile.InputStream; BinaryReader br = new BinaryReader(fs); Byte [] bytes = br.ReadBytes((Int32)fs.Length); SqlConnection conn = new SqlConnection(Data Source = DESKTOP-06QKCFT \\SQLEXPRESS; Initial Catalog = CMS1; Integrated Security = True); conn.Open(); string insert_query =INSERT INTO [image]([name],[contenttype],[data])VALUES(@ name,@ contenttype,@ data); SqlCommand cmd = new SqlCommand(insert_query,conn); cmd.Parameters.AddWithValue(@ name,SqlDbType.VarChar).Value = filename; cmd.Parameters.AddWithValue(@ contenttype,SqlDbType.VarChar).Value = contenttype; cmd.Parameters.AddWithValue(@ data,SqlDbType.VarChar).Value = bytes; cmd.ExecuteNonQuery(); Lb1.ForeColor = System.Drawing.Color.Green; Lb1.Text =文件已成功上传; conn.Close(); } 其他 { Lb1.ForeColor = System.Drawing.Color.Red; Lb1.Text =无法识别文件格式。 + 上传图片/ Word / PDF / Excel格式; } }

我是什么尝试过: 但是当我运行时它给了我这个错误:第66行的错误

第64行:cmd.Parameters.AddWithValue(@ contenttype,SqlDbType.VarChar).Value = contenttype; 第65行:cmd.Parameters.AddWithValue(@ data,SqlDbType.VarChar).Value = bytes; 第66行:cmd.ExecuteNonQuery(); 第67行:Lb1.ForeColor = System.Drawing.Color.Green; 第68行:Lb1.Text =文件成功上传;

解决方案

我想我刚刚发现了这个问题...

cmd.Parameters.AddWithValue( @name,SqlDbType.VarChar).Value = filename; cmd.Parameters.AddWithValue( @ contenttype,SqlDbType.VarChar).Value =内容类型; cmd.Parameters.AddWithValue( @ data,SqlDbType.VarChar).Value = bytes;

你混淆了 Parameters.Add 和 Parameters.AddWithValue 要么使用

cmd.Parameters.Add( @ name,SqlDbType.VarChar).Value = filename; cmd.Parameters.Add( @ contenttype,SqlDbType.VarChar).Value =内容类型; cmd.Parameters.Add( @ data,SqlDbType.VarChar).Value = bytes;

或更好......

cmd.Parameters.AddWithValue( @ name,filename); cmd.Parameters.AddWithValue( @ contenttype,contenttype); cmd.Parameters.AddWithValue( @ data,bytes);

Hello every one I use the code to upload photo to data base :

// Read the file and convert it to Byte Array string filepath = upload_image.PostedFile.FileName; string filename = Path.GetFileName(filepath); string ext = Path.GetExtension(filename); string contenttype = string.Empty; switch (ext){ case ".doc": contenttype = "application/vnd.ms-word"; break; case ".docx": contenttype = "application/vnd.ms-word"; break; case ".xls": contenttype = "application/vnd.ms-excel"; break; case ".xlsx": contenttype = "application/vnd.ms-excel"; break; case ".jpg": contenttype = "image/jpg"; break; case ".png": contenttype = "image/png"; break; case ".gif": contenttype = "image/gif"; break; case ".pdf": contenttype = "application/pdf"; break; } if (contenttype != String.Empty) { Stream fs = upload_image.PostedFile.InputStream; BinaryReader br = new BinaryReader(fs); Byte[] bytes = br.ReadBytes((Int32)fs.Length); SqlConnection conn = new SqlConnection("Data Source=DESKTOP-06QKCFT\\SQLEXPRESS;Initial Catalog=CMS1;Integrated Security=True"); conn.Open(); string insert_query = "INSERT INTO [image] ([name], [contenttype], [data]) VALUES (@name, @contenttype, @data)"; SqlCommand cmd = new SqlCommand(insert_query, conn); cmd.Parameters.AddWithValue("@name", SqlDbType.VarChar).Value = filename; cmd.Parameters.AddWithValue("@contenttype", SqlDbType.VarChar).Value = contenttype; cmd.Parameters.AddWithValue("@data", SqlDbType.VarChar).Value = bytes; cmd.ExecuteNonQuery(); Lb1.ForeColor = System.Drawing.Color.Green; Lb1.Text = "File Uploaded Successfully"; conn.Close(); } else { Lb1.ForeColor = System.Drawing.Color.Red; Lb1.Text = "File format not recognised." + " Upload Image/Word/PDF/Excel formats"; } }

What I have tried: but when I run it gave me this error: the error in line 66

Line 64: cmd.Parameters.AddWithValue("@contenttype", SqlDbType.VarChar).Value = contenttype; Line 65: cmd.Parameters.AddWithValue("@data", SqlDbType.VarChar).Value = bytes; Line 66: cmd.ExecuteNonQuery(); Line 67: Lb1.ForeColor = System.Drawing.Color.Green; Line 68: Lb1.Text = "File Uploaded Successfully";

解决方案

I think I've just spotted the problem...

cmd.Parameters.AddWithValue("@name", SqlDbType.VarChar).Value = filename; cmd.Parameters.AddWithValue("@contenttype", SqlDbType.VarChar).Value = contenttype; cmd.Parameters.AddWithValue("@data", SqlDbType.VarChar).Value = bytes;

You've mixed up Parameters.Add and Parameters.AddWithValue Either use

cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = filename; cmd.Parameters.Add("@contenttype", SqlDbType.VarChar).Value = contenttype; cmd.Parameters.Add("@data", SqlDbType.VarChar).Value = bytes;

or better...

cmd.Parameters.AddWithValue("@name", filename); cmd.Parameters.AddWithValue("@contenttype", contenttype); cmd.Parameters.AddWithValue("@data", bytes);

更多推荐

将照片上传到数据库C#

本文发布于:2023-10-12 17:13:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485352.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   照片

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!