Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.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
C# 如何使用查询在不同列中插入不同的文件_C#_Sql_Asp.net - Fatal编程技术网

C# 如何使用查询在不同列中插入不同的文件

C# 如何使用查询在不同列中插入不同的文件,c#,sql,asp.net,C#,Sql,Asp.net,我想向数据库中添加文件 我有6列来存储文件。如果只上传了1个文件,我想只上传一列中的单个文件。如果上传两个文件,我想使用两列 上载文件时,我的SQL查询应该相应更新哪些内容 我已经将上传的文件数量限制为6个,总大小限制为2MB 我正在使用ASP.NET protected void btnUpload_Click(object sender, EventArgs e) { try { if (fileUpload.PostedFi

我想向数据库中添加文件

我有6列来存储文件。如果只上传了1个文件,我想只上传一列中的单个文件。如果上传两个文件,我想使用两列

上载文件时,我的SQL查询应该相应更新哪些内容

我已经将上传的文件数量限制为6个,总大小限制为2MB

我正在使用ASP.NET

protected void btnUpload_Click(object sender, EventArgs e)
    {
        try
        {
            if (fileUpload.PostedFiles.Count <= 6)
            {
                int a = fileUpload.PostedFile.ContentLength;
                if (a < 2000)
                {
                    foreach (HttpPostedFile postedFile in fileUpload.PostedFiles)
                    {
                        string filename = Path.GetFileName(postedFile.FileName);
                        string contentType = postedFile.ContentType;
                        using (Stream fs = postedFile.InputStream)
                        {
                            using (BinaryReader br = new BinaryReader(fs))
                            {
                                byte[] bytes = br.ReadBytes((Int32)fs.Length);

                                using (SqlConnection con = new SqlConnection(@"Data Source= USER\SQLEXPRESS ; Initial Catalog= BlobUploading ; Integrated Security = True"))
                                {
                                    string query = "insert into tblBlob values (@BloB1, @BloB2, @BloB3,@BloB4 ,@BloB5 ,@BloB6)";
                                    using (SqlCommand cmd = new SqlCommand(query))
                                    {
                                        cmd.Connection = con;
                                        cmd.Parameters.AddWithValue("@BloB1", bytes[0]);
                                        cmd.Parameters.AddWithValue("@BloB2", bytes[1]);
                                        cmd.Parameters.AddWithValue("@BloB3", bytes[2]);
                                        cmd.Parameters.AddWithValue("@BloB4", bytes[3]);
                                        cmd.Parameters.AddWithValue("@BloB5", bytes[4]);
                                    cmd.Parameters.AddWithValue("@BloB6", bytes[5]);
                                    con.Open();
                                    cmd.ExecuteNonQuery();
                                    con.Close();
                                }
                            }
                        }
                    }
                }
            }
protectedvoidbtnupload\u单击(对象发送方,事件参数e)
{
尝试
{

如果(fileUpload.PostedFiles.Count,则需要重新设计数据库结构,以便只有一个blob字段、一个记录ID字段(作为PK)和一个加载标识符,以便在存在多个文件时识别哪个文件。插入查询可能如下所示:

INSERT INTO myTable
(
    fileID,
    fileContents
)
VALUES
(1, fileContent1),
(2 ,fileContent2)
然后,您可以根据需要的记录数来更改添加的记录数

插入多行,而不是@juharr所说的多列

要在ASP.NET中执行此操作,C#需要结构来保存文件:

public class FileHolder
{
    int FileID {get; set; }
    byte[] FileData {get; set;}
}
创建一个
列表文件
来保存数据。然后:

foreach (var item in files)
{
    // write item.FileID and item.FileData
    // into your table
}

这样,您就不受一次可以处理的文件数量的限制。

将插入查询包装到存储过程中。在存储过程中,将Blob的默认值设置为Null

从ASP.NET中,根据文件的可用性设置blob参数。如果未设置blob参数,则在SP中将其设置为null

string strQuery = "sp_SaveFile";

SqlCommand cmd = new SqlCommand(strQuery);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@FormId", SqlDbType.Int).Value = fileId;
if(bytes1 != null)
    cmd.Parameters.Add("@Blob1", SqlDbType.Binary).Value = bytes1;
if(bytes2 != null)
    cmd.Parameters.Add("@Blob2", SqlDbType.Binary).Value = bytes2;
// .... Handle rest of the Blob parameters.

InsertUpdateData(cmd);
protectedvoidbtnupload\u单击(对象发送方,事件参数e)
{
尝试
{

如果在SQL Server中存储二进制对象可能不是最好的选择,因为您的表中有可空列,在查询中需要特殊的东西,当您没有文件要上载时,只在查询中传递NULL。我想您也应该考虑创建一个只有FrimID和BLB字段的表,而不是6个NulLA。ble FIELDS不是答案,但有一条相关的评论:SQL Server Express是您的正确选择吗?通常的方法是存储URL,然后客户端/应用程序从那里加载BLOB(文件服务器更擅长于服务这些东西)。显示您的代码。只需添加一些
if()
语句并指定适当的参数。此外,编号列通常表示您可以从规范化中获益。在数据库中存储同一事物的未知编号的正确方法不是使用多个列,而是使用一个可以插入多行的表。您可以使用一个带有JU的表来规范化此操作t
FormID
、一个
FileId
Blob
列(虽然存储URL会更好)。然后
FormID
可以是原始表的外键(如果它有其他列)他们要么需要
FormID
,要么需要另一个将
FormID
映射到其文件的表。我找不到您提到的我应该只有一个blob字段来存储多个文件的部分,对不起,我是新手,您能再详细说明一下吗,我的编码部分正在等待,因为我无法解决此问题。我很抱歉将文件转换为字节并存储them@Grijan基本上,如果您的文件有一列的表,那么当您存储2个文件时,您将插入两行。当您存储5个文件时,您将插入5行。然后您可以通过
FormID
进行查询,以返回零行或多行匹配并包含这些文件。@Grijan当然,您只需要一个一堆
if
语句来处理您可以插入的有限数量的文件。要么有6个稍微不同的SQL查询,要么有一个SQL查询并将额外参数设置为null。这可能是要求的,但更好的设计是有一个blob列,而不是六个。任何后续处理都可能是一场噩梦,必须进行测试每行查看有多少列包含数据。
 protected void btnUpload_Click(object sender, EventArgs e)
    {
        try
        {
            if (fileUpload.PostedFiles.Count <= 6)
            {
                int a = fileUpload.PostedFile.ContentLength;
                if (a < 2000)
                {
                    foreach (HttpPostedFile postedFile in fileUpload.PostedFiles)
                    {
                        string filename = Path.GetFileName(postedFile.FileName);
                        string contentType = postedFile.ContentType;
                        using (Stream fs = postedFile.InputStream)
                        {
                            using (BinaryReader br = new BinaryReader(fs))
                            {
                                byte[] bytes = br.ReadBytes((Int32)fs.Length);

                                using (SqlConnection con = new SqlConnection(@"Data Source= USER\SQLEXPRESS ; Initial Catalog= BlobUploading ; Integrated Security = True"))
                                {
                                    string query = "insert into tblBlob values (@BloB1, @BloB2, @BloB3,@BloB4 ,@BloB5 ,@BloB6)";
                                    using (SqlCommand cmd = new SqlCommand(query))
                                    {
                                        cmd.Connection = con;
                                        cmd.Parameters.AddWithValue("@BloB1", bytes[0]);
                                        cmd.Parameters.AddWithValue("@BloB2", bytes[1]);
                                        cmd.Parameters.AddWithValue("@BloB3", bytes[2]);
                                        cmd.Parameters.AddWithValue("@BloB4", bytes[3]);
                                        cmd.Parameters.AddWithValue("@BloB5", bytes[4]);
                                        cmd.Parameters.AddWithValue("@BloB6", bytes[5]);
                                        con.Open();
                                        cmd.ExecuteNonQuery();
                                        con.Close();
                                    }
                                }
                            }
                        }
                    }
                }