Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.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 通过vb.net在sql server中上载图像文件时卡住_Sql Server_Vb.net_Image - Fatal编程技术网

Sql server 通过vb.net在sql server中上载图像文件时卡住

Sql server 通过vb.net在sql server中上载图像文件时卡住,sql-server,vb.net,image,Sql Server,Vb.net,Image,重新发布同一问题,因为发布后无法编辑该问题。如果你以前读过这个问题,请特别阅读下面的内容- 这是我在这个网站上使用的代码。一切都很好,但我需要一个小不同的代码上传图像,我不知道该怎么做-这里的代码- Private Sub btnAttach_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnAttach.Click Dim iLength As Integer = CTy

重新发布同一问题,因为发布后无法编辑该问题。如果你以前读过这个问题,请特别阅读下面的内容-

这是我在这个网站上使用的代码。一切都很好,但我需要一个小不同的代码上传图像,我不知道该怎么做-这里的代码-

Private Sub btnAttach_Click(ByVal sender As System.Object, _  
ByVal e As System.EventArgs) Handles btnAttach.Click  
    Dim iLength As Integer = CType(File1.PostedFile.InputStream.Length, Integer)  
    If iLength = 0 Then Exit Sub 'not a valid file  
    Dim sContentType As String = File1.PostedFile.ContentType  
    Dim sFileName As String, i As Integer  
    Dim bytContent As Byte()  
    ReDim bytContent(iLength) 'byte array, set to file size  

'strip the path off the filename  '
i = InStrRev(File1.PostedFile.FileName.Trim, "\")  
If i = 0 Then  
    sFileName = File1.PostedFile.FileName.Trim  
Else  
    sFileName = Right(File1.PostedFile.FileName.Trim, Len(File1.PostedFile.FileName.Trim) - i)  
End If  

Try  
    File1.PostedFile.InputStream.Read(bytContent, 0, iLength)  
    With cmdInsertAttachment  
        .Parameters("@FileName").Value = sFileName  
        .Parameters("@FileSize").Value = iLength  
        .Parameters("@FileData").Value = bytContent  
        .Parameters("@ContentType").Value = sContentType  
        .ExecuteNonQuery()  
    End With  
Catch ex As Exception  
    'Handle your database error here  
    dbConn.Close()  
End Try  
Response.Redirect(Request.Url.ToString) 'Refresh page 
End Sub  
除了这部分,一切都很好-

With cmdInsertAttachment  
    .Parameters("@FileName").Value = sFileName  
    .Parameters("@FileSize").Value = iLength  
    .Parameters("@FileData").Value = bytContent  
    .Parameters("@ContentType").Value = sContentType  
    .ExecuteNonQuery()  
End With  
我没有与cmdinsertattachment一起使用。我正在使用Html工具箱中的Html输入(文件)。输入文件的ID为ID=“upldimg”

那么我如何将它插入到我的表中-

Column1 ID identity
Column2 Img image
Column 3 Description varchar(200).
请告诉我插入语句,如-

INSERT into table1 (Img, Description) values (???, txtdescription.text)
是否在insert语句中执行upldimg.text

  • 使用适当的数据类型。映像已弃用,请改用VARBINARY(MAX)
  • 不要为任意大小的图像分配字节数组。使用块
  • 不要阻止响应以等待整个文件的数据库上载
  • 如果这样做,我将使用IIS模块在HTTP请求中接收文件时处理文件的分块,以避免完全创建临时文件。非常简单的方法如下所示:

    create table Uploads (
      Id int identity(1,1) primary key
      , FileName varchar(256)
      , ContentType varchar(256)
      , FileData varbinary(max)); 
    
    以及aspx:

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack && FileUpload1.HasFile)
            {
                ThreadPool.QueueUserWorkItem(
                    new WaitCallback(TransferToDatabase), FileUpload1);
            }
        }
    
        protected void TransferToDatabase(object args)
        {
            try
            {
                Debug.Assert(args is FileUpload);
                FileUpload upload = (FileUpload)args;
                using (SqlConnection conn = new SqlConnection(
                    Settings.Default.connString))
                {
                    conn.Open();
                    using (SqlTransaction trn = conn.BeginTransaction())
                    {
                        SqlCommand cmd = new SqlCommand(@"
    INSERT INTO Uploads(FileName, ContentType, FileData)
    VALUES (@FileName, @ContentType, @initialChunk);
    SET @id = SCOPE_IDENTITY();", conn, trn);
                        cmd.Parameters.AddWithValue("@FileName", upload.PostedFile.FileName);
                        cmd.Parameters.AddWithValue("@contentType", upload.PostedFile.ContentType);
                        SqlParameter paramId = new SqlParameter("@id", SqlDbType.Int);
                        paramId.Direction = ParameterDirection.Output;
                        cmd.Parameters.Add(paramId);
    
                        byte[] chunk = new byte[4096];
                        int offset = upload.FileContent.Read(chunk, 0, 4096);
                        byte[] initialChunk = chunk;
                        if (offset < 4096)
                        {
                            // can't pass only part of a byte[] as parameter value
                            // must copy out the appropiate size
                            initialChunk = new byte[offset];
                            Array.Copy(chunk, initialChunk, offset);
                        }
                        cmd.Parameters.AddWithValue("@initialChunk", initialChunk);
                        cmd.ExecuteNonQuery();
    
                        // Add the rest of the data
                        if (offset == 4096)
                        {
                            SqlParameter paramChunk = new SqlParameter("@chunk", SqlDbType.VarBinary, 4096);
                            SqlParameter paramLength = new SqlParameter("@length", SqlDbType.BigInt);
                            SqlCommand cmdAddChunk = new SqlCommand(@"
    UPDATE Uploads
       SET FileData.Write(@chunk, NULL, @length)
    WHERE id = @id", conn, trn);
                            cmdAddChunk.Parameters.AddWithValue("@id", paramId.Value);
                            cmdAddChunk.Parameters.Add(paramChunk);
                            cmdAddChunk.Parameters.Add(paramLength);
    
                            do
                            {
                                int chunkSize = upload.FileContent.Read(chunk, 0, 4096);
                                if (0 == chunkSize)
                                {
                                    break;
                                }
                                paramChunk.Value = chunk;
                                paramLength.Value = chunkSize;
    
                                cmdAddChunk.ExecuteNonQuery();
    
                                offset += chunkSize;
    
                            } while (true);
                        }
    
                        trn.Commit();
                    }
                }
            }
            catch (Exception e)
            {
                // Log to the appropiate error logging infrastructure
                Debug.Write(e);
            }
        }
    
    受保护的无效页面加载(对象发送方,事件参数e)
    {
    if(IsPostBack&&FileUpload1.HasFile)
    {
    ThreadPool.QueueUserWorkItem(
    新的WaitCallback(TransferToDatabase),FileUpload1);
    }
    }
    受保护的void TransferToDatabase(对象参数)
    {
    尝试
    {
    Assert(args是FileUpload);
    FileUpload upload=(FileUpload)参数;
    使用(SqlConnection conn=newsqlconnection(
    Settings.Default.connString)
    {
    conn.Open();
    使用(SqlTransaction trn=conn.BeginTransaction())
    {
    SqlCommand cmd=新的SqlCommand(@“
    插入上载(文件名、内容类型、文件数据)
    值(@FileName、@ContentType、@initialChunk);
    设置@id=SCOPE_IDENTITY();”,conn,trn);
    cmd.Parameters.AddWithValue(“@FileName”,upload.PostedFile.FileName);
    cmd.Parameters.AddWithValue(“@contentType”,upload.PostedFile.contentType);
    SqlParameter paramId=新的SqlParameter(“@id”,SqlDbType.Int);
    paramId.Direction=ParameterDirection.Output;
    cmd.Parameters.Add(paramId);
    byte[]chunk=新字节[4096];
    int offset=upload.FileContent.Read(chunk,04096);
    字节[]初始块=块;
    如果(偏移量<4096)
    {
    //无法仅将字节[]的一部分作为参数值传递
    //必须复印出合适的尺寸
    initialChunk=新字节[偏移量];
    复制(块、初始块、偏移量);
    }
    cmd.Parameters.AddWithValue(“@initialChunk”,initialChunk);
    cmd.ExecuteNonQuery();
    //添加其余的数据
    如果(偏移量==4096)
    {
    SqlParameter paramChunk=新的SqlParameter(“@chunk”,SqlDbType.VarBinary,4096);
    SqlParameter paramLength=新的SqlParameter(“@length”,SqlDbType.BigInt);
    SqlCommand cmdAddChunk=新的SqlCommand(@)
    更新上传
    SET FileData.Write(@chunk,NULL,@length)
    其中id=@id”,conn,trn);
    cmdAddChunk.Parameters.AddWithValue(“@id”,paramId.Value);
    cmdAddChunk.Parameters.Add(paramChunk);
    cmdAddChunk.Parameters.Add(paramLength);
    做
    {
    int chunkSize=upload.FileContent.Read(chunk,04096);
    如果(0==chunkSize)
    {
    打破
    }
    paramChunk.Value=chunk;
    paramLength.Value=chunkSize;
    cmdAddChunk.ExecuteOnQuery();
    偏移量+=块大小;
    }虽然(正确);
    }
    trn.Commit();
    }
    }
    }
    捕获(例外e)
    {
    //记录到适当的错误记录基础结构
    Debug.Write(e);
    }
    }
    
    您无法编辑问题,因为您是以derti而不是drien身份登录的(请参阅)