How to embed/attach SQL Database into Visual C#?

后端 未结 4 1820
执笔经年
执笔经年 2021-01-02 08:34

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.

What

相关标签:
4条回答
  • 2021-01-02 09:13

    You'll want to look at SQL Server Compact Edition

    0 讨论(0)
  • 2021-01-02 09:18

    SQLite is great to use too, fast, lightweight ...

    0 讨论(0)
  • 2021-01-02 09:24

    Just for getting a grip (VS 2010):

    1. Create a console project
    2. Add a reference to System.Data.SqlServerCe (in Program Files\Microsoft SQL Server Compact Edition\v3.5\Desktop\System.Data.SqlServerCe.dll on my computer)
    3. Right click the project node in Solution Explorer, select "Add => New Item...", choose "Local Database", name it MyDB
    4. A new file MyDB.sdf will be added to the project (a MS SQL Server Compact Database)
    5. Right click the new file, click "Open", the database will be open in "Server Explorer"
    6. In "Server Explorer" expand MyDB.sdf, right click Tables, "Create Table" (name it MyTable)
    7. Add two columns "Field1" and "Field2" (leave them nvarchar(100) for now)
    8. Right click the new table, choose "Show Table Data", fill in your data

    The code:

    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.

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

    0 讨论(0)
  • 2021-01-02 09:31

    Yes, there is http://en.wikipedia.org/wiki/SQL_Server_Compact for such purpose.

    0 讨论(0)
提交回复
热议问题