将照片上传到数据库

将照片上传到数据库

本文介绍了将照片上传到数据库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 =文件成功上传;
解决方案

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";
解决方案


这篇关于将照片上传到数据库C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 14:02