从WinForms C#应用程序从SQL Server 2005/2008上载/下载文件?

从WinForms C#应用程序从SQL Server 2005/2008上载/下载文件?,c#,winforms,sql-server-2005,sql-server-2008,c#-3.0,C#,Winforms,Sql Server 2005,Sql Server 2008,C# 3.0,我正在使用DocX创建.DocX文件。我宁愿将它们存储在SQL数据库中,而不是存储在硬盘、共享或SharePoint上。因此,我的问题是: 如何编写适当的代码将文件保存到该数据库 如何编写适当的代码将文件检索到该数据库 我应该为表设置什么样的数据类型来保存docx文件?是图像吗 关于, 疯子 另外,我更希望在使用旧的“学校”方式方面有一个合适的代码: using (var varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnect

我正在使用DocX创建.DocX文件。我宁愿将它们存储在SQL数据库中,而不是存储在硬盘、共享或SharePoint上。因此,我的问题是:

  • 如何编写适当的代码将文件保存到该数据库
  • 如何编写适当的代码将文件检索到该数据库
  • 我应该为表设置什么样的数据类型来保存docx文件?是图像吗
  • 关于,

    疯子

    另外,我更希望在使用旧的“学校”方式方面有一个合适的代码:

    using (var varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetailsDepozyt))
    using (var sqlQuery = new SqlCommand(preparedCommand, varConnection))
    using (var sqlQueryResult = sqlQuery.ExecuteReader())
        while (sqlQueryResult != null && sqlQueryResult.Read())
    

    我知道它不使用读卡器,但这里有一个示例可以查询它并保存到磁盘。要将其放回去,只需插入或更新一个字节[]。数据类型将是sql中的varbinary max

    SqlConnection connection = new SqlConnection ("...");
        connection.Open ();
        SqlCommand command = new 
          SqlCommand ("select...e", connection);
        byte[] buffer = (byte[]) command.ExecuteScalar ();
        connection.Close();
        FileStream fs = new FileStream(@"C:\test.pdf", FileMode.Create);
        fs.Write(buffer, 0, buffer.Length);
        fs.Close();
    
    读者可能是这样的

    while (myReader.Read())
            {
                byte[] file = myReader.GetBytes(0));
                // might be 
                file = (byte[])GetValue(0);
            }
    

    docx文件类似于zip文件,它是多个文件的集合。如何将其转换为二进制,然后保存到DB中

    下面的方法会奏效

    保存

            string filePath = "";
            string connectionString = "";
            FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            BinaryReader reader = new BinaryReader(stream);
            byte[] file = reader.ReadBytes((int)stream.Length);
            reader.Close();
            stream.Close();
    
            SqlCommand command;
            SqlConnection connection = new SqlConnection(connectionString);
            command = new SqlCommand("INSERT INTO FileTable (File) Values(@File)", connection);
            command.Parameters.Add("@File", SqlDbType.Binary, file.Length).Value = file;
            connection.Open();
            command.ExecuteNonQuery();
    
    检索是一个有点复杂的过程,如下所示

                SqlConnection connection = new SqlConnection("");
                string query = 
                @" SELECT File FROM FileTable where FileID =" + 125;
                SqlCommand command = new SqlCommand(query, connection);
    
                FileStream stream;
                BinaryWriter writer;
    
                int bufferSize = 100;
                byte[] outByte = new byte[bufferSize];
    
                long retval;
                long startIndex = 0;
    
                string pubID = "";
    
                connection.Open();
                SqlDataReader reader = command.ExecuteReader(CommandBehavior.Default);
    
                while (reader.Read())
                {    
                    pubID = reader.GetString(0);
    
                    stream = 
                    new FileStream("abc.docx", FileMode.OpenOrCreate, FileAccess.Write);
                    writer = new BinaryWriter(stream);
                    startIndex = 0;
                    retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize);
    
                    while (retval == bufferSize)
                    {
                        writer.Write(outByte);
                        writer.Flush();
                        startIndex += bufferSize;
                        retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize);
                    }
                    writer.Write(outByte, 0, (int)retval - 1);
                    writer.Flush();
                    writer.Close();
                    stream.Close();
                } reader.Close();
                connection.Close();
    
    请阅读以下文章以获取帮助和详细信息

    保存

    检索


    谢谢,似乎就是这个。如果其他人有更多的想法和方法,我会等一会儿:-)如果没有,我会接受它作为最终答案。你知道为什么这个过程这么慢吗?