开发者

What is the best way for me to connect to a offline database through C#

I have an application that needs to store loads of data in a table format. I want something easy to configure, wh开发者_如何学Cich is also in built with C#.NET. I don't want to have to include additional DLL files.

Also some links to tutorials, explaining the connection process and querying would be great. I'm assuming this is just like PHP, but which database type do I need?

It needs to be able to hold a lot of data and the ability to perform backups would be nice.


I'm not sure what you mean by "built in with C#.NET", but SQL Server Express comes with Visual Studio.

If you're looking for "a self-contained, embeddable, zero-configuration SQL database engine", you could try System.Data.SQLite.


If you want an offline database you could use SQL Server CE, as its a in-process database that does not require being attached to a server instance, which is really what you want then. Here is an example in C# on how you would connect, and populate a data table to manipulate some data.

// this connectionstring can also be an absolute file path
string connectionString = "Data Source=|DataDirectory|\mydatabase.sdf";
using (SqlCeConnection connection = new SqlCeConnection(connectionString)) {
    try {
        connection.Open();
    } 
    catch (SqlCeException) {
        // connection failed
    }
    using (SqlCeDataAdapter adapter = new SqlCeDataAdapter("SELECT * FROM <table>", connection)) {
        using (DataTable table = new DataTable("<table>")) {
            adapter.Fill(); // Populate the table with your select statement

            // do stuff with the datatable
            // example:
            foreach (DataRow row in table.Rows) {
                row["mycolumn"] = "somedata";
            }

            table.AcceptChanges();
        }
    }
}

You can even use commands instead of data tables

using (SqlCeCommand command = new SqlCeCommand("DELETE FROM <table> WHERE id = '0'", connection)) {
    command.ExecuteNonQuery(); // executes command
}


Have a look at the ease of SQL Server Compact

Not build-in but easily added, no install and free.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜