[Original] Application of SQL Server mobile in WM (C #)
SQL Server mobile is from SQL Server 2005 Function One, the following introduces SQL mobile Data Library creation...
1. Start-> AllProgram -> Microsoft SQL Server 2005-> SQL Server Management studio, which starts SQL Server 2005; 2. After the instance is started, you will see Interface In "server type", select "SQL Server mobile","Database File "Select" New Database "; 3. the "Create a new SQL Server 2005 mobile edition Database" page appears. Enter the database file name, including the path. I use D: \ EMS. the SDF password is determined based on individual needs. If the password is too simplePrompt Do you want to continue using it; 4. After the above steps are completed, the "connect to server" interface will be returned, and click "Connect" to connect to the database; 5. Create a table and initialize the table.
Note: After the database is created, you can copy it to the PDA separately or package it together with the program to the PDA. I use the package together.
Here I amExample Create a simple table: Create Table oprinfo ( Oprid nvarchar (10) primary key, Oprname nvarchar (20) not null, Password nvarchar (10) Not null, );
Next let's take a look at how to use C # To use SQL Server mobile in WM. I am using WM 5...
First, we need to reference system. Data. sqlserverce. We should be very clear about how to do this, so we will not detail it here,
1. query the database Using system. Data. sqlserverce; Private void button#click (Object sender, eventargs E) { Listview1.items. Clear (); String connectionstring = "Data Source = \ Program Files \ testdbofsdf \ EMS. SDF; Password = EMS "; Sqlceconnection Cn = new sqlceconnection (connectionstring ); CN. open (); String SQL = "select * From oprinfo "; Sqlcecommand COM = new sqlcecommand (SQL, CN );
Sqlcedatareader READ = com. executereader (); While (read. Read ()) { String custid = read [0]. tostring (); String Company = read [1]. tostring (); String custname = read [2]. tostring (); Listview1.beginupdate (); Listviewitem item = new listviewitem (); Item. subitems [0]. Text = custid; Item. subitems. Add (company ); Item. subitems. Add (custname ); Listview1.items. Add (item ); Listview1.endupdate (); } Read. Close (); Com. Dispose (); CN. Close (); } 2. insert records Private void button#click (Object sender, eventargs E) { String oprid = textbox1.text. Trim (); String oprname = textbox2.text. Trim (); String Password = textbox3.text. Trim (); If (oprid! = "") & (Oprname! = "") & (Password! = "")) { String connectionstring = "Data Source = \ Program Files \ testdbofsdf \ EMS. SDF; Password = EMS "; Try { Sqlceconnection Cn = new sqlceconnection (connectionstring ); CN. open (); String SQL = "insert into oprinfo values ('" + oprid + "', '" + oprname + "', '" + password + "')"; Sqlcecommand COM = new sqlcecommand (SQL, CN ); Com. executenonquery (); Com. Dispose (); CN. Close (); Label4.text = "operation successful! "; } Catch (exception exp) { Label4.text = exp. tostring (); }
} Else { Label4.text = "complete information! "; } } 3. Modify records Private void button#click (Object sender, eventargs E) { String connectionstring = "Data Source = \ Program Files \ testdbofsdf \ EMS. SDF; Password = EMS "; Try { Sqlceconnection Cn = new sqlceconnection (connectionstring ); CN. open (); String SQL = "Update oprinfo set oprname = 'pkwsh' where oprid = '000000 '"; Sqlcecommand COM = new sqlcecommand (SQL, CN ); Com. executenonquery (); Com. Dispose (); CN. Close (); Label1.text = "operation successful! "; } Catch (exception exp) { Label1.text = exp. tostring (); } } 4. delete records Private void button#click (Object sender, eventargs E) { String connectionstring = "Data Source = \ Program Files \ testdbofsdf \ EMS. SDF; Password = EMS "; Try { Sqlceconnection Cn = new sqlceconnection (connectionstring ); CN. open (); String SQL = "delete from oprinfo where oprid = '000000 '"; Sqlcecommand COM = new sqlcecommand (SQL, CN ); Com. executenonquery (); Com. Dispose (); CN. Close (); Label1.text = "operation successful! "; } Catch (exception exp) { Label1.text = exp. tostring (); } }
Here is just a brief introduction to the basic Application In actual application, you can make changes as needed .... |