I. SQL mobile terminal database operations
1. Create an SDF Database
After vs2005 is installed, the SQL ce name is SQL mobile. Create a mobile application and add an SQL mobile database to the project, then, you can directly design the structure of the new database and add data in vs2005. When using the simulator to debug a program, if you want to retain the database data on the simulator, you can remove the. SDF file in the vs2005 project, so that each debugging will not overwrite the database file on the simulator.
2. Database Connection
Read the msdn documentation clearly, but note that the msdn connection strings are mostly written as Conn. connectionstring = "Data Source = 'cookbook. SDF '";
The database file does not contain a path. It cannot be written on a mobile device. It seems that the relative path cannot be used and the absolute path must be used,
Conn. connectionstring = "Data Source = '" + path. getdirectoryname (system. reflection. assembly. getexecutingassembly (). getname (). codebase) + @ "\ cookbook. SDF '";
In this way, you can succeed.
3. Data Operations
I wrote it clearly on msdn.
Sqlceconnection conn = This. getconn ();
Conn. open ();
Try
{
String xiid = This. cbox. selecteditem. tostring ();
Xiid = xiid. substring (0, 1 );
Sqlcecommand COM = new sqlcecommand ();
Com. Connection = conn;
Com. commandtext = "insert into cookmain (name, descr, xiid) values ('" + this. cookname. Text. Trim ()
+ "','" + This. content. Text. Trim () + "'," + xiid + ")";
If (COM. executenonquery ()> 0)
{
MessageBox. Show ("saved successfully! ");
}
Else
MessageBox. Show ("failed to save, please add again ");
}
Finally
{
Conn. Close ();
}
This is a simple insert operation.