简体   繁体   中英

how to connect silverlight with sqlite without IsolatedStorageFile

IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();

isf.DeleteFile("mydb.db");

using (SqliteConnection conn = new SqliteConnection())
{
    conn.ConnectionString = "Version=3,uri=file:mydb.db";
    conn.Open();

    using (SqliteCommand cmd = conn.CreateCommand())
    {


        cmd.CommandText = "CREATE TABLE test_table ( [id] INTEGER PRIMARY KEY, [col] INTEGER UNIQUE, [col2] INTEGER, [col3] REAL, [col4] TEXT, [col5] BLOB)";
        cmd.ExecuteNonQuery();

        cmd.Transaction = conn.BeginTransaction();
        cmd.CommandText = "INSERT INTO test_table(col, col2, col3, col4, col5) VALUES(@col, @col2, @col3, @col4, @col5);SELECT last_insert_rowid();";
        cmd.Parameters.Add("@col", null);
        cmd.Parameters.Add("@col2", null);
        cmd.Parameters.Add("@col3", null);
        cmd.Parameters.Add("@col4", null);
        cmd.Parameters.Add("@col5", null);

        DateTime start = DateTime.Now;
        this.lstResult.Items.Add("Inserting 100 Rows with transaction");

        for (int i = 0; i < 100; i++)
        {
            cmd.Parameters["@col"].Value = i;
            cmd.Parameters["@col2"].Value = i;
            cmd.Parameters["@col3"].Value = i * 0.515;
            cmd.Parameters["@col4"].Value = "hello world" + i;
            cmd.Parameters["@col5"].Value = Encoding.UTF8.GetBytes("test");

            object s = cmd.ExecuteScalar();
        }
        cmd.Transaction.Commit();
        cmd.Transaction = null;
        this.lstResult.Items.Add("Time taken :" + DateTime.Now.Subtract(start).TotalMilliseconds + " ms.");

        cmd.CommandText = "SELECT * FROM test_table";
        using (SqliteDataReader reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                var bytes = (byte[])reader.GetValue(5);
                this.lstResult.Items.Add(string.Format("{0},{1},{2},{3},{4}, {5}",
                    reader.GetInt32(0),
                    reader.GetInt32(1),
                    reader.GetInt32(2),
                    reader.GetDouble(3),
                    reader.GetString(4),
                    Encoding.UTF8.GetString(bytes, 0, bytes.Length)));
            }
        }

        conn.Close();
    }
}

This code works good, but is there anyway to connect to a database on my D: drive

conn.ConnectionString = @"Version=3,uri=file:d:\mydb.db";

Or any other way to connect to any type of databases without using IsolatedStorageFile

Is the database on the server or the users computer?

If it is on the server, you will want to create a WCF service that accesses the database, then have Silverlight interact with the WCF service.

If it is on the users computer, you cannot access the local file system directly. You will need to present the user with an OpenFileDialog . The user will select the file, which will be made available as a FileStream on the OpenFileDialog .

If this is a Silverlight OOB app, things change a little bit. You have more access to the user's system, but I'm not familiar enough with Silverlight OOB to make any recommendations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM