Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何将选定的多个图像上载到数据库中_Sql Server_C# 4.0 - Fatal编程技术网

Sql server 如何将选定的多个图像上载到数据库中

Sql server 如何将选定的多个图像上载到数据库中,sql-server,c#-4.0,Sql Server,C# 4.0,我需要将网站中选择的图像上载到数据库我的项目有多个文件选择“文件上载”我如何才能将这些多个文件上载到数据库中二进制格式的图像数据,我的代码是 protected void Button1_Click(object sender, EventArgs e) { string Qry = "insert into tblFiles values(@data)"; SqlConnection con = new SqlConnection(@"Data

我需要将网站中选择的图像上载到数据库我的项目有多个文件选择“文件上载”我如何才能将这些多个文件上载到数据库中二进制格式的图像数据,我的代码是

    protected void Button1_Click(object sender, EventArgs e)

   {

        string Qry = "insert into tblFiles values(@data)";
        SqlConnection con = new SqlConnection(@"Data Source=ESLHPC17\SQL2008;Initial Catalog=dbFiles;User ID=user;Password=password");

        SqlCommand cmd = new SqlCommand(Qry,con);
        cmd.Parameters.Add("@data") = FileUpload1.FileBytes;

    }
我正在使用以下Web处理程序将文件保存在本地文件夹中

    protected void Button1_Click(object sender, EventArgs e)

   {

        string Qry = "insert into tblFiles values(@data)";
        SqlConnection con = new SqlConnection(@"Data Source=ESLHPC17\SQL2008;Initial Catalog=dbFiles;User ID=user;Password=password");

        SqlCommand cmd = new SqlCommand(Qry,con);
        cmd.Parameters.Add("@data") = FileUpload1.FileBytes;

    }
<%@ WebHandler Language="C#" Class="Upload" %>

using System;
using System.Web;
using System.IO;


public class Upload : IHttpHandler {

public void ProcessRequest (HttpContext context) {
    context.Response.ContentType = "text/plain";
    context.Response.Expires = -1;
    try
    {
        HttpPostedFile postedFile = context.Request.Files["Filedata"];

        string savepath = "";
        string tempPath = "";
        tempPath = System.Configuration.ConfigurationManager.AppSettings["FolderPath"]; 
        savepath = context.Server.MapPath(tempPath);
        string filename = postedFile.FileName;
        if (!Directory.Exists(savepath))
            Directory.CreateDirectory(savepath);

        postedFile.SaveAs(savepath + @"\" + filename);
        context.Response.Write(tempPath + "/" + filename);
        context.Response.StatusCode = 200;
    }
    catch (Exception ex)
    {
        context.Response.Write("Error: " + ex.Message);
    }
}

public bool IsReusable {
    get {
        return false;
    }
}
试一试

    protected void Button1_Click(object sender, EventArgs e)

   {

        string Qry = "insert into tblFiles values(@data)";
        SqlConnection con = new SqlConnection(@"Data Source=ESLHPC17\SQL2008;Initial Catalog=dbFiles;User ID=user;Password=password");

        SqlCommand cmd = new SqlCommand(Qry,con);
        cmd.Parameters.Add("@data") = FileUpload1.FileBytes;

    }
foreach (HttpPostedFile uploadedFile in FileUpload1.PostedFiles)
{
    SaveImage(uploadedFile);
}

private void SaveImage(HttpPostedFile file)
{
   using(SqlConnection con = new SqlConnection(ConnectionString))// set ConnectionString
   {
       using(SqlCommand cmd = new SqlCommand(Qry,con)) // set Qry
       {
          cmd.Parameters.AddWithValue("@data", ReadFile(file));
          con.Open();
          cmd.ExecuteNonQuery();
       }
   }
}

private byte[] ReadFile(HttpPostedFile file)
{
    byte[] data = new Byte[file.ContentLength];
    file.InputStream.Read(data, 0, file.ContentLength);
    return data;
}
如果需要插入服务器文件夹中的图像,并假设图像路径数组为
imageArray
,则

    protected void Button1_Click(object sender, EventArgs e)

   {

        string Qry = "insert into tblFiles values(@data)";
        SqlConnection con = new SqlConnection(@"Data Source=ESLHPC17\SQL2008;Initial Catalog=dbFiles;User ID=user;Password=password");

        SqlCommand cmd = new SqlCommand(Qry,con);
        cmd.Parameters.Add("@data") = FileUpload1.FileBytes;

    }
foreach (var path in imageArray)
{
    SaveImage(path);
}

private void SaveImage(string path)
{
   using(SqlConnection con = new SqlConnection(ConnectionString))// set ConnectionString
   {
       using(SqlCommand cmd = new SqlCommand(Qry,con)) // set Qry
       {
          cmd.Parameters.AddWithValue("@data", System.IO.File.ReadAllBytes(path));
          con.Open();
          cmd.ExecuteNonQuery();
       }
   }
}
试一试

    protected void Button1_Click(object sender, EventArgs e)

   {

        string Qry = "insert into tblFiles values(@data)";
        SqlConnection con = new SqlConnection(@"Data Source=ESLHPC17\SQL2008;Initial Catalog=dbFiles;User ID=user;Password=password");

        SqlCommand cmd = new SqlCommand(Qry,con);
        cmd.Parameters.Add("@data") = FileUpload1.FileBytes;

    }
foreach (HttpPostedFile uploadedFile in FileUpload1.PostedFiles)
{
    SaveImage(uploadedFile);
}

private void SaveImage(HttpPostedFile file)
{
   using(SqlConnection con = new SqlConnection(ConnectionString))// set ConnectionString
   {
       using(SqlCommand cmd = new SqlCommand(Qry,con)) // set Qry
       {
          cmd.Parameters.AddWithValue("@data", ReadFile(file));
          con.Open();
          cmd.ExecuteNonQuery();
       }
   }
}

private byte[] ReadFile(HttpPostedFile file)
{
    byte[] data = new Byte[file.ContentLength];
    file.InputStream.Read(data, 0, file.ContentLength);
    return data;
}
如果需要插入服务器文件夹中的图像,并假设图像路径数组为
imageArray
,则

    protected void Button1_Click(object sender, EventArgs e)

   {

        string Qry = "insert into tblFiles values(@data)";
        SqlConnection con = new SqlConnection(@"Data Source=ESLHPC17\SQL2008;Initial Catalog=dbFiles;User ID=user;Password=password");

        SqlCommand cmd = new SqlCommand(Qry,con);
        cmd.Parameters.Add("@data") = FileUpload1.FileBytes;

    }
foreach (var path in imageArray)
{
    SaveImage(path);
}

private void SaveImage(string path)
{
   using(SqlConnection con = new SqlConnection(ConnectionString))// set ConnectionString
   {
       using(SqlCommand cmd = new SqlCommand(Qry,con)) // set Qry
       {
          cmd.Parameters.AddWithValue("@data", System.IO.File.ReadAllBytes(path));
          con.Open();
          cmd.ExecuteNonQuery();
       }
   }
}


你真的不应该公开你的
sa
密码。您的应用程序也不应该以
sa
用户身份登录。执行该命令,您将被设置。你的问题是什么?@CodeCaster问题是,它一次只处理一个文件moment@LukeHennerley你真好,但OP应该提到这一点,特别是因为之后的所有信息都丢失了:他在哪一部分遇到了麻烦。正在动态创建
文件上载
?从多个
文件上传
获取输入?从单个
文件上载读取所有上载的文件
?一次执行多个查询,或者对多个数据运行单个
insert
查询?@Colin Mackay Okie Mackay先生谢谢你的建议…你真的不应该暴露你的
sa
密码。您的应用程序也不应该以
sa
用户身份登录。执行该命令,您将被设置。你的问题是什么?@CodeCaster问题是,它一次只处理一个文件moment@LukeHennerley你真好,但OP应该提到这一点,特别是因为之后的所有信息都丢失了:他在哪一部分遇到了麻烦。正在动态创建
文件上载
?从多个
文件上传
获取输入?从单个
文件上载读取所有上载的文件
?一次执行多个查询,或者对多个数据运行单个
插入
查询?@Colin Mackay Okie Mackay先生感谢您的建议…查询、命令和连接可以在
foreach
外部初始化,命令不会执行。查询、命令和连接可以在
foreach
外部初始化,命令未被执行。@Damith文件上载1只有已发布的文件未发布的文件,我需要添加一些引用吗?
.NET Framework 4.5
需要这样做。@Damith我只有.Net Framework 4.0,我的操作系统是Windows XP,因此它不支持.Net Framework 4.5。我还有其他方法可以访问“FileUpload1.PostedFiles”吗像一些引用一样,然后从保存的文件夹中读取并逐个插入数据库。@Damith我已经尝试了你所说的方式,并在下面的答案部分提到了代码,但没有起作用@Damith文件上载1只发布了文件而没有发布文件我需要添加一些引用吗???.NET Framework 4.5需要这样做。@Damith我只有.Net Framework 4.0,我的操作系统是Windows XP,因此它不支持.Net Framework 4.5。我还有其他方法可以访问“FileUpload1.PostedFiles”吗就像一些引用一样,然后从保存的文件夹中读取并逐个插入数据库。@Damith我已经尝试了你所说的方法,并在下面的答案部分提到了代码,但没有成功