usingSystem;usingSystem.IO;usingSystem.Text;usingSystem.Data;usingSystem.Data.SqlServerCe;usingSystem.Collections;usingSystem.Windows.Forms;usingsystem.data.common;//explains that--wince database operations are similar to Windows, suggesting that some statements can be tested in the WinCE database. namespacecedb{/// <summary> ///Database Operations Classes/// </summary> Public Static classDbsqlserver {/// <summary> ///Get a DataTable/// </summary> /// <param name= "SQL" ></param> Public StaticDataTable Getdt (stringSQL) {SqlCeConnection conn=NULL; Try{conn=NewSqlCeConnection ("Data Source = 3190.sdf"); Try{Conn. Open (); } Catch(Exception ex) {Throw; } sqlcecommand cmd=Conn. CreateCommand (); Cmd.commandtext=SQL; SqlCeDataAdapter Ada=NewSqlCeDataAdapter (CMD); DataTable DTS=NewDataTable (); Ada. Fill (dts); Ada. Dispose (); Conn. Close (); returnDTS; } Catch { Throw; } finally { if(NULL!=conn) Conn. Close (); } } /// <summary> ///get ExecuteScalar, which is the first row of values in the data/// </summary> /// <param name= "SQL" ></param> Public StaticObject Getexecutescalar (stringSQL) {SqlCeConnection conn=NULL; Try{conn=NewSqlCeConnection ("Data Source = 3190.sdf"); Try{Conn. Open (); } Catch { Throw NewNullReferenceException ("failed to connect to database"); } sqlcecommand cmd=Conn. CreateCommand (); Cmd.commandtext=SQL; returncmd. ExecuteScalar (); } Catch(SqlCeException e) {Throw NewNullReferenceException ("Operation Database Failed"); } finally { if(Conn. state = =ConnectionState.Open) Conn. Close (); } } Public Static voidExecuteNonQuery (stringsql//Execute SQL statement, no return{SqlCeConnection conn=NULL; Try{conn=NewSqlCeConnection ("Data Source = 3190.sdf"); Try{Conn. Open (); } Catch { Throw NewNullReferenceException ("failed to connect to database"); } sqlcecommand cmd=Conn. CreateCommand (); Cmd.commandtext=SQL; Cmd. ExecuteNonQuery (); } Catch(SqlCeException e) {Throw NewNullReferenceException ("Operation Database Failed"); } finally { if(Conn. state = =ConnectionState.Open) Conn. Close (); } } }}
WinCE database Operation class