FileStream创建以及插入和读取 [英] FileStream Creation and inserting and reading

查看:75
本文介绍了FileStream创建以及插入和读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我需要创建一个简单的数据库来存储通过应用程序中的流读取器生成的文件.因此,基本上,一旦创建了文件,就必须将其插入表中.该表已经存在,因此只能进行更改以允许文件流式传输(已完成).我需要创建一个文件组并添加一个要存储的表:AutoID-FileName-文件(将是实际文件)-dateTime.

那么我需要再次在C#应用程序中读取文件并以字符串形式对其进行编译,并能够完全按原样查看文件.

1.更改文件组的数据库(我们可以将数据库称为TESTDB)SQL
2.创建用于存储文件SQl的表
3.创建程序以插入文件SQL
4.读取文件内容并将其存储为字符串C#的代码

任何例子都会有所帮助.谢谢.

Hi
I need to create a simple database to store files that are generated through stream readers in an application. so basically once the file has been created it has to be inserted into a table . this table already exists and therefore can only be altered to allow file streaming (which has already been done) . All I need to create a file group and add a table that will store : AutoID - FileName - file(which will be the actual file) - dateTime.

then a i need to read the file again in a C# app and compile it in a string and be able to view the file exactly as it was.

1.Alter database for filegroup (we can call the databse TESTDB) SQL
2. Create table for storing files SQl
3. create procedure to insert files SQL
4. code to read file content and store it as string C#

any examples will help. thank you.

推荐答案



试试这个解决方案,


Hi,
try this solution,

-- First Create the database
Create Database Test_DB;
-- After execution of First command execute the following query
use Test_DB;
CREATE TABLE [dbo].[file_db](
    [file_name] [varchar](50) NOT NULL,
    [file_Content] [varbinary](max) NULL,
    )


// It will help you to insert the file into the db
void insertFile()
        {
            FileStream fs = new FileStream(@"c:\file\input.txt", FileMode.Open, FileAccess.Read);
            int length = Convert.ToInt32(fs.Length);
            byte[] data = new byte[length];
            fs.Read(data, 0, length);
            fs.Close();
            string insert = "insert into file_db values('input.txt',@file_content)";
            DataSet ds = new DataSet();
            SqlConnection conn = new SqlConnection(con);
            conn.Open();
            SqlCommand cmd = new SqlCommand(insert, conn);
            cmd.Parameters.Add(new SqlParameter("@file_content", (object)data));
            cmd.ExecuteNonQuery();
            conn.Close();
        }
// It will help you to read the inserted file content from db
 void ReadFile()
        {
            SqlConnection conn = new SqlConnection(con);
            string qry = "select file_content from file_db where file_name='input.txt'";
            SqlCommand cmd = new SqlCommand(qry, conn);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            dr.Read();
            byte[] output = (byte[])dr[0];
            dr.Close();
            conn.Close();
            FileStream fs = new FileStream(@"C:\file\output.txt", FileMode.Create, FileAccess.Write);
            fs.Write(output, 0, output.Length);
            fs.Close();
        }


问候,
Sowraaj


Regards,
Sowraaj


这篇关于FileStream创建以及插入和读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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