如何将SQL数据库嵌入/附加到Visual C#中? [英] How to embed/attach SQL Database into Visual C#?

查看:110
本文介绍了如何将SQL数据库嵌入/附加到Visual C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用SQL,这可能是一个愚蠢的问题,但是我进行了一些研究,但我认为我没有找到我想要的东西.

This is the first time I've used SQL and this is probably a stupid question but I've done some research and I don't think I'm finding what I'm looking for.

我想要的是一种制作私有SQL数据库的方法,该数据库将在我的C#程序中使用.我已经在SQL Server Express中建立了数据库,并将其连接到Visual Studio 2010.

What I want is a way to make a private SQL database that will be used within my C# program. I've already made a database within SQL Server Express and I've connected that to Visual Studio 2010.

SqlCommand DBAccess = new SqlCommand();
DBAccess.Connection = new SqlConnection(
    "Data Source = localhost;" +
    "Initial Catalog = My_Database;" +
    "Trusted_Connection = True");

  • 我可以将数据源嵌入程序中并将与解决方案的其余部分一起编译吗?
  • 该程序有一些额外的背景;

    Some extra background on the program;

    该程序需要搜索数据库中的6个表,并在搜索字符串与特定字段匹配时输出 DataRow 的内容.

    It's a program that needs to search through 6 tables within the database and output the contents of a DataRow when search string matches a particular field.

    EG.

    Field 1     Field 2
    quick       AA
    brown       AA
    fox         AB
    jumps       AB
    over        AA
    the         AB
    lazy        AB
    dog         AA
    

    Search_String = AB

    Search_String = AB

    输出;

    fox
    jumps
    the
    lazy
    

    任何帮助将不胜感激!!!!!

    Any help will be much appreciated!!!!!

    谢谢

    推荐答案

    刚掌握(VS 2010):

    Just for getting a grip (VS 2010):

    1. 创建控制台项目
    2. 添加对System.Data.SqlServerCe的引用(在我的计算机上的Program Files \ Microsoft SQL Server Compact Edition \ v3.5 \ Desktop \ System.Data.SqlServerCe.dll中)
    3. 在解决方案资源管理器中右键单击项目节点,选择添加=>新建项...",选择本地数据库",将其命名为MyDB
    4. 新文件MyDB.sdf将添加到项目(MS SQL Server Compact数据库)中
    5. 右键单击新文件,单击打开",数据库将在服务器资源管理器"中打开.
    6. 在服务器资源管理器"中,展开MyDB.sdf,右键单击表",然后单击创建表"(将其命名为MyTable)
    7. 添加两列"Field1"和"Field2"(暂时将其保留为nvarchar(100))
    8. 右键单击新表,选择显示表数据",填写数据

    代码:

    using System.Data.SqlServerCe;
    
    namespace ConsoleApplication6
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (var cn = new SqlCeConnection("Data Source=MyDB.sdf"))
                {
                    cn.Open();
                    using (var cmd = cn.CreateCommand())
                    {
                        cmd.CommandText = "select * from MyTable where Field2 like '%AB%'";
                        using (var reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                Console.WriteLine("Field1: {0}", reader[0]);
                            }
                        }
                    }
                }
                Console.ReadKey();
            }
        }
    }
    

    输出的狐狸会让懒惰跳起来.

    Will output fox jumps the lazy.

    ,为简单起见,我会使用 SQlite .包装器在此处: http://system.data.sqlite.org/index.html/doc/trunk/www/index.wiki

    BUT, I would go with SQlite for simple purposes. Wrapper is here: http://system.data.sqlite.org/index.html/doc/trunk/www/index.wiki

    这篇关于如何将SQL数据库嵌入/附加到Visual C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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