0

我正在尝试从磁盘上传文件,然后将文件插入到 varbinary db 列中。

我似乎无法弄清楚如何插入二进制文件。

我在 WPF 应用程序中使用 C# 和 Linq to Sql。

这是我到目前为止所尝试的!任何建议或意见将不胜感激。

private void UploadFile()
        {
            DatabaseData.DataClassesDataContext context = new DatabaseData.DataClassesDataContext();
            {
            OpenFileDialog dlgOpen = new OpenFileDialog();
            dlgOpen.Title = "Select file";

            FileData fd = new FileData();
            if (dlgOpen.ShowDialog() ?? false)
            {
                FileStream inStream = File.OpenRead(dlgOpen.FileName);
                //FileStream outStream = File.OpenWrite(dlgOpen.FileName + ".xlsx");
                int b;

                while ((b = inStream.ReadByte()) > -1)
                    // outStream.WriteByte((byte)b);


                    fd.FileId = Guid.NewGuid();
                    //fd.DataFile = inStream;//DataFile is the Varbinary column in the db
                    fd.Title = dlgOpen.FileName;
                    fd.FileExtension = txtExtension.text;

                    context.FileDatas.InsertOnSubmit(fd);
                    context.SubmitChanges();

                    //outStream.Flush();
                    //outStream.Close();
                    inStream.Close();
            }
            }
        }
4

4 回答 4

1

要修复编译错误,请删除该while语句。您正在尝试创建一个新的 FileData() ,直到b > -1.

我不知道代码在那之后会起作用,但它会修复这个编译错误。

private void UploadFile()
{
    DatabaseData.DataClassesDataContext context = new DatabaseData.DataClassesDataContext();
    {
        OpenFileDialog dlgOpen = new OpenFileDialog();
        dlgOpen.Title = "Select file";

        if (dlgOpen.ShowDialog() ?? false)
        {
            FileStream inStream = File.OpenRead(dlgOpen.FileName);

            FileData fd = new FileData();
            fd.FileId = Guid.NewGuid();
            fd.DataFile = inStream;
            fd.Title = dlgOpen.FileName;
            fd.FileExtension = txtExtension.text;

            context.FileDatas.InsertOnSubmit(fd);
            context.SubmitChanges();

            inStream.Close();
        }
    }
}
于 2013-06-07T03:49:04.323 回答
1

不确定这是否可行,但试试这个

 if (dlgOpen.ShowDialog() ?? false)
                    {
                        byte[] bytes = System.IO.File.ReadAllBytes(dlgOpen.FileName);

                            fd.FileId = Guid.NewGuid();
                            fd.DataFile = bytes;
                            fd.Title = dlgOpen.FileName;                   
                            context.FileDatas.InsertOnSubmit(fd);
                            context.SubmitChanges();
于 2013-06-07T04:05:51.180 回答
1

好吧,您在评论中阅读了我的免责声明。我不能保证这里的任何专业人士都会同意按照您的要求采用的方法。我只是在以正确的方式学习 C#,并产生了转换工作的非数据库程序的想法。我需要这个来将我所有的现有数据转换成一个新的数据库来接管存储:

/* Spawned from a button click
...
*/
        //
        // Here I bring in the directory which you'll likely replace with
        // a single file
        //
        string[] files = 
            Directory.GetFiles( 
            @"yourDicectory");

            // 
            // At this point you may disregard my loop if needed
            //
            foreach (string file in files)
            {
                //
                // Here the entire files are read and split
                // Handle your data how you like
                //  
                StreamReader fileReader = new StreamReader( file );
                string lines = fileReader.ReadToEnd();
                string[] entries = lines.Split( ',' );

                // 
                // Here, omitted, I declare variables of types to insert "holders" 
                // Every CSV has to go to a corresponding holder of the 
                // the appropriate type (i.e., DateTime, decimal(money), or yourType)
                //

                SqlCeConnection con = new SqlCeConnection( "Data Source = YourDataSource.sdf" );
                con.Open();
                SqlCeCommand cmd  = con.CreateCommand();

                //
                // The insert command that takes the parsed values - value1, value2, ...
                // which are the named and omitted declarations from above
                // You're providing a signature of the table you're inserting into
                // 
                cmd.CommandText = "INSERT INTO YourTable ([Column1], [Column2], [Column3], ... , [Column(n)]) VALUES (value1, value2, value3, ... , value(n))";

                //
                // Here, omitted, I parse and convert the values and store them in the holders
                //

                // Now execute and catch if needed
                    try
                    {
                        cmd.ExecuteNonQuery();
                    }
                    catch( SqlCeException sqle )
                    {
                        myTextbox.Text += sqle.Errors.ToString() + "\n";
                    }
                }
                //
                // Update my view - May not apply
                //
                myGridView1.Update();
                con.Close();
            }
/* Do whatever else you'd like ... */
于 2013-06-07T04:34:34.157 回答
-1

C#中的文件操作(我的博客链接会帮助你)

于 2013-06-07T04:51:05.530 回答