使用实体从SQL2008提取数据 [英] Extract Data From SQL2008 with Entities

查看:114
本文介绍了使用实体从SQL2008提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要生成一个脚本,该脚本会将数据插入SQL 2008数据库.除了将文件存储在varbinary字段中的数据库中之外,其他所有东西都工作正常.我提取了数据,但是当我尝试运行脚本时,结果却很奇怪.这就是我的做法.

I need to generate a script that will insert data into SQL 2008 database. Everything is working fine except that I am storing files in the database in a varbinary field. I extract the data but when I try run the scripts it just freaks out. This is how I am doing it.

using (StreamWriter sw = new StreamWriter(Console.ReadLine() + ".sql"))
            {
                foreach (File b in context.Files)
                {
                    sw.WriteLine("INSERT INTO [File] VALUES ({0}, {1}, {2}, {3})", b.FileID, b.FileName, b.MIME, ASCIIEncoding.UTF8.GetString(b.BinaryData));
                }
                sw.Flush();
                sw.Close();
            }



任何帮助都会很棒!


我不需要执行脚本,只需要将其写入文件即可.在应用程序中不会引发异常,我只需要知道如何从varbinary中写入内容,就可以在以后的阶段执行脚本以将ot添加到另一个db.



Any help would be great!


I don''t need to execute the script, I just need to write it to a file. The exception is not thrown in the app, I just need to know how to write the content from the varbinary in such a way that I can execute the scripts to add ot to another db at a later stage.

推荐答案

我错过了什么吗?该StreamWriter不执行任何操作.您先写,然后冲洗,关闭并处理.另外,context.files从哪里来?您的问题提到实体,这使我认为上下文是数据库中的ObjectContext,文件是表.
那么这段代码是做什么的呢?迭代遍历File表的记录,编写一条SQL语句,将已经存在的记录插入同一表中的Stream中,然后处理该Stream的末尾.

那么context到底是什么,您想达到什么目的?如果我读到此内容,则无法得出结论,但是该代码除了执行任何操作外,都不会引发Exceptions.您还能提及例外吗?

编辑; OP编辑了他的问题.
下面的代码将在C:\驱动器上创建一个新的.txt文件.它的初始文本将为"Test",但每次代码再次通过时,"Test"将附加在新行上.
Am I missing something? That StreamWriter does nothing. You write to it, then Flush, Close and Dispose. Also, where does the context.Files come from? Your question mentions Entities which leads me to think context is an ObjectContext and File is a Table in your Database.
So what does this code do? It iterates through the File table records, writes a SQL statement that inserts the already existing records into the same table to a Stream, then Disposes of the Stream the end.

So what is context exactly and what are you trying to achieve? If I read this I cannot conclude, but that this code does nothing except, throw Exceptions. Could you mention the Exception too?

Edit; OP has edited his question.
The following code will create a new .txt file on the C:\ drive. It''s initial text will be "Test", but every time the code passes this again "Test" will be appended on a new line.
string s = "Test" + Environment.NewLine;
using (System.IO.FileStream fs = new System.IO.FileStream(@"C:\MyFile.txt", System.IO.FileMode.Append)) {
    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(fs)) {
        writer.Write(s);
        writer.Flush();
        fs.Flush();
        writer.Close();
        fs.Close();
    }
}


我建议您在循环中构建String,然后使用此代码将其附加到文件中.
希望这就是您要寻找的/您需要的.


I suggest you build the String in your loop and then use this code to append it to your file.
Hope this is what you are looking for/what you need.


这篇关于使用实体从SQL2008提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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