Sqlite PetaPoco连接

When before using sql server, with PetaPoco as ORM, using up more easily. There are projects also need to configure the database, because they require less chose sqlite, and in accordance with the configuration are relatively simple.

Following a brief description of what, for later use (no brain before easy to use, feeling down would have been better).

First with SQLite Expert Professional to create a database, then create a table called Student.

 

 Use nuget in wpf add petepoco,

 

 In Solution Explorer adds a Models folder,

 

 Corresponding to the app.config configured as follows:

 <connectionStrings>
    <add name="sqlite" connectionString="Data Source=|DataDirectory|\Test.db;Pooling=true;FailIfMissing=false"
   providerName="System.Data.SQLite" />
</connectionStrings>

Data Source = | DataDirectory | \ Test.db, seems only into the bin of debug or release, in a different directory how to configure the solution has not been found.

Note also that in the SQLite.Interop.dll into the corresponding directory, or will be error.

Student create a class in the program, together with characteristics [TableName ( "Student")] and   [PrimaryKey ( "id")] .

using PetaPoco;

namespace WPF_PetaPoco_sqlite
{
    [TableName("Student")]
    [PrimaryKey("id")]
    public class Student
    {
        public int id { get; set; }
        public string name { get; set; }
        public int age { get; set; }
        public string province { get; set; }
    }
}

code:

var DB = new new PetaPoco.Database ( " SQLite " );
             the try 
            { 
                db.BeginTransaction (); 
                Student Student = new new Student () 
                { 
                    Age = 16 , 
                    name = " Li " , 
                    Province = " jiangsu " 
                }; 
                // inserted 
                var = rel db.Insert (Student); 

                // Iraq Find a primary key value
                Student student2 = db.SingleOrDefault<Student>(3);
                student2.age = 37;

                //sql语句查找
                var students = db.Query<Student>("Select * FROM Student WHERE province=@0", "shandong").ToList();

                //更新
                db.Update(student2);
                db.CompleteTransaction();
            }
            catch (Exception ex)
            {

            }

The results are as follows: Because I manually deleted the line 7, when generating the skipped 7, 8 Id is generated

 

Guess you like

Origin www.cnblogs.com/haozhangcool/p/11446099.html