如何在SQL Server 2008r2中将图像另存为varbinay? [英] How to save image as varbinay in sql server 2008r2?

查看:101
本文介绍了如何在SQL Server 2008r2中将图像另存为varbinay?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Visual Studio10和sql server 2008r2中创建Windows应用程序。我想将图像保存在数据库中并检索更新。当我使用图像数据类型时,我的查询已正确执行,但是对于varbinary数据类型,它不起作用。

I am creating a windows application in visual studio10 and sql server 2008r2.i want to save image in database and retrieve for updation.when i use image data type my query is executed correctly ,but for varbinary datatype it doesn't work.

推荐答案

这是从前一段时间开始的,因此它可能需要对路径和其他内容进行一些修补(来自旧项目) )

This was from a while back, so it may need some tinkering with paths and what-not (from an old project)

    protected void btn_upload_file_server_Click(object sender, EventArgs e)
    {
        if (this.ddl_determinationBit.SelectedIndex != 2)
        {
          btn_upload_file_server.Enabled = false;
          string fileDirectory = @"Y:\Files\"; // Change all reference locations for this: pages Autism.cs & SpecUserAdmin.cs
          string FilePath = Request.PhysicalApplicationPath;
          if (FileUpload1.HasFile)
          {
              string fileExt = ".html";
              string fileName = this.txt_fileName.Text + fileExt;
              string SaveFilePath = fileDirectory + Server.HtmlEncode(fileName);
              FileUpload1.SaveAs(SaveFilePath);


              string connstring = WebConfigurationManager.ConnectionStrings["SomeConnectionString"].ConnectionString;
              SqlConnection conn = new SqlConnection(connstring);
              SqlCommand SaveMyFile = new SqlCommand("usp_store_document", conn);
              SaveMyFile.CommandType = CommandType.StoredProcedure;
              string SQLFileDir = fileDirectory + fileName;
              SaveMyFile.Parameters.Add(new SqlParameter("@fullpath", SQLFileDir));
              SaveMyFile.Parameters.Add(new SqlParameter("@filename", this.txt_fileName.Text));
              SaveMyFile.Parameters.Add(new SqlParameter("@ext", fileExt));

              int detBit = this.ddl_determinationBit.SelectedIndex;
              SaveMyFile.Parameters.Add(new SqlParameter("@document_destination_bit", detBit));

              conn.Open();
              SaveMyFile.Connection = conn;
              SaveMyFile.ExecuteNonQuery();
              conn.Close();

              // delete all files from the file folder
              string[] filepaths = Directory.GetFiles(@"Y:\Files\");
              foreach (string filepath in filepaths)
                  File.Delete(filepath);

              ddl_determinationBit.SelectedIndex = 2;
              txt_fileName.Text = "";
              btn_upload_file_server.Enabled = true;
          }
          else if (ddl_determinationBit.SelectedIndex == 2) lbl_error.Text = "Please where this Document will go";
        }

您还需要此SQL代码

    --set up database for filestream
    ALTER DATABASE SOAR ADD
    FILEGROUP FileStreamLibrary CONTAINS FILESTREAM;
    GO

    --set file folder location of files
    ALTER DATABASE SOAR ADD FILE (
           NAME = FSGroup1File,
           FILENAME = 'D:\SOAR\FSDATA')
    TO FILEGROUP FileStreamLibrary;
    GO

    /****** Object:  Table [dbo].[tbl_document]    Script Date: 02/01/2012 11:15:15 ******/
    SET ANSI_NULLS ON
    GO

    SET QUOTED_IDENTIFIER ON
    GO

    SET ANSI_PADDING ON
    GO

    CREATE TABLE [dbo].[tbl_document](
        [document_ID] [int] IDENTITY(1,1) NOT NULL,
        [documents_10k] [varbinary](max) FILESTREAM  NULL,
        [document_name] [varchar](300) NULL,
        [document_ext] [char](4) NULL,
        [document_path] [varchar](500) NULL,
        [document_destination_bit] [int] NULL,
        [document_GUID] [uniqueidentifier] ROWGUIDCOL  NOT NULL,
        [document_date] [datetime] NULL,
     CONSTRAINT [PK__tbl_docu__9679EC941CBC4616] PRIMARY KEY CLUSTERED 
    (
        [document_ID] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY] FILESTREAM_ON [FileStreamLibrary],
     CONSTRAINT [UQ__tbl_docu__09BF7B501F98B2C1] UNIQUE NONCLUSTERED 
    (
        [document_GUID] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY] FILESTREAM_ON [FileStreamLibrary]

    GO

    SET ANSI_PADDING OFF
    GO


    ALTER TABLE [dbo].[tbl_document] ADD  CONSTRAINT [DF__tbl_docum__docum__2180FB33]  DEFAULT ((0)) FOR [document_destination_bit]
    GO

    ALTER TABLE [dbo].[tbl_document] ADD  CONSTRAINT [DF__tbl_docum__docum__22751F6C]  DEFAULT (newid()) FOR [document_GUID]
    GO

这篇关于如何在SQL Server 2008r2中将图像另存为varbinay?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆