Summary of operations related to WinPhone development database

Summary of operations related to WinPhone development database

1. First of all, let's talk about WP's support for .sqlite database operations. The following methods were found from Google, Baidu, Codeplex, etc.;

(1) The oldest one is to use the third-party Community.CsharpSqlite.WP.dll assembly (Note: If you want to operate an existing .sqlite database, instead of creating a data table from scratch, adding data, etc., then you need to find this source code on the codeplex website and make corresponding modifications to support use. Otherwise, you will encounter very depressing errors such as "Unable to open database connection...")

(2) The second type is similar to the first type, but its package is different. C#-SQLiteWP7.Preview1.Release, which is also on Codeplex. The code usage is similar to the first type, but the method inside returns objects such as DataReader, which makes it convenient for us to perform corresponding data reading operations. Although the database is also copied to the root directory of the independent storage, the connection string here is different. The format is as follows:
"Version=database version number, uri=file:your database full name"

Simple Code operation process:

  1. using (SqliteConnection conn = new SqliteConnection( "Version=3,uri=file:test.db" ))
  2.  
  3. {
  4.  
  5. conn.Open();
  6.  
  7. using (SqliteCommand cmd = conn.CreateCommand())
  8.  
  9. {
  10.  
  11. cmd.CommandText = "sql statement" ;
  12.  
  13. cmd.ExecuteNonQuery();
  14.  
  15.  
  16.  
  17. cmd.Transaction = conn.BeginTransaction();
  18.  
  19. //SQL statement to add parameters  
  20. cmd.CommandText = "INSERT INTO test(col, col2, col3, col4, col5) VALUES(@col, @col2, @col3, @col4, @col5);SELECT last_insert_rowid();" ;
  21.  
  22. cmd.Parameters.Add( "@col" , null );
  23.  
  24. cmd.Parameters.Add( "@col2" , null );
  25.  
  26. cmd.Parameters.Add( "@col3" , null );
  27.  
  28. cmd.Parameters.Add( "@col4" , null );
  29.  
  30. cmd.Parameters.Add( "@col5" , null );
  31.  
  32. cmd.Transaction.Commit();
  33.  
  34. cmd.Transaction = null ;

If you don't want to modify the source code of Community.CsharpSqlite.WP, then look for the two assemblies Vici.CoolStorage.WP7 and Vici.Core.WP7 on the Internet. I personally feel that this method is simple to operate and has slightly better performance than the first method.

  1. //Note: Add Vici.CoolStorage.WP7.dll and Vici.Core.WP7.dll to the project first  
  2. string fn = "MNSECRET.DB" ; //your database name, please put it in the project root directory, and set the generation operation to Resource, do not copy  
  3. Assembly assem = Assembly.GetExecutingAssembly();
  4. string assemeblyName=assem.FullName.Substring( 0 , assem.FullName.IndexOf( ',' ));
  5. Uri dbURi= new Uri( "/" + assemeblyName + ";component/" + fn,
  6. UriKind.Relative);
  7. //The first run of the program copies the SQLite database to local storage  
  8. StreamResourceInfo sr = Application.GetResourceStream(dbURi);
  9. IsolatedStorageFile iStorage = IsolatedStorageFile.GetUserStoreForApplication();
  10. if (!iStorage.FileExists(fn))
  11. {
  12. using (var outputStream = iStorage.OpenFile(fn, FileMode.CreateNew))
  13. {
  14. byte [] buffer = new   byte [ 10000 ];
  15. for (; ; )
  16. {
  17. int read = sr.Stream.Read(buffer, 0 , buffer.Length);
  18.  
  19. if (read <= 0 )
  20. break ;
  21.  
  22. outputStream.Write(buffer, 0 , read);
  23. }
  24. }
  25. }
  26. //Connect to the database  
  27. CSConfig.SetDB(fn);
  28. //Data operation  
  29. CSGenericRecordList cslis = CSDatabase.RunQuery( "SELECT* FROM CITY" ); //It can be understood as returning a table  
  30. foreach (CSGenericRecord cs in cslis)
  31. {
  32. //Get each row of data in the table  
  33. string result= cs[ "data table field name" ].ToString();
  34.  
  35. }

Link to this article: http://wp.662p.com/thread-8290-1-1.html

<<:  Jack Ma and Zhang Chaoyang, the story of worshipping Buddha and believing in religion

>>:  Don't bend: Fighting nation boycotts iPhone 6

Recommend

How to post videos on Douyin and become popular?

There is no doubt that Tik Tok is so popular that...

How to downgrade Win10 without loss after 30 days of regret

It’s been almost a month since the first batch of...

To survive in a sea of ​​fire, what tricks do plants have?

Last year, the Amazon rainforest fire burned 500,...

Why do we go to the bathroom more often when the weather gets cold?

Friends You should have had this experience. When...

Kuaishou Information Stream Advertising Guide

Short videos have become the main carrier of info...

CPC model optimization guide, for reference in delivery!

Search ads have low display and poor clickthrough...

Android 2.3 is still young after four years

[[125534]] I always thought that at least 99% of ...

How did Momo gain tens of millions of users in just one year?

In June 2011, Boss Wang (Wang Li called himself B...