Use of SQL CE under the. NET Compact Framework

Source: Internet
Author: User
Tags compact microsoft sql server versions sql server express

The most commonly used databases under wince and Windows Mobile are SQL Ce,sql CE, also known as SQL Server for Windows CE and SQL Server

Mobile Edition, the latest version is named SQL Server Compact 3.5 SP1. The SQL Server Compact not only runs on wince and Windows Mobile, but also runs on Windows PCs and is an effective alternative to access, if you do not use stored procedures, in SQL Server Programs developed under the Compact can be ported to other server versions of SQL Server almost without modification. See the application of SQL Server Express and SQL Server Compact in this article. In this article I will use the name of the SQL CE.

Under the. NET Compact framework for SQL CE usage and development, you need to apply library System.Data.SqlServerCe.dll, and note that different versions of SQL CE use DLLs that are not used. The SQL CE 3.5 library generally corresponds to the following directory C:\Program Files\Microsoft SQL Server Compact edition\v3.5 ado.net DLL, while the SQL CE 3.0 library generally corresponds to the following directory C:\Pr Ogram Files\Microsoft Visual Studio 8\SmartDevices\SDK\SQL Server\mobile are not compatible with each other. Because the developed namespaces (namespace) are consistent, the developed programs can be used in the SQL CE version that is not available.

Helper class

The following is an example of a SQL CE helper class that is only intended for the SQL CE database and is not considered for porting to other databases, so all classes use SQL CE-related classes.

Class Sqlcehelper:idisposable
{
private SqlCeConnection connection;
Private SqlCeCommand command;
Private Const string connectionString = "Data source=/db/db.sdf";

#region Open/close
public void Open ()
{
Try
{
Connection = new SqlCeConnection (connectionString);
Command = connection. CreateCommand ();
Command. Connection = Connection;
Command.commandtype = CommandType.Text;

Connection. Open ();
}
catch (DataException e)
{
Console.WriteLine (E.message);
}
}

public void Close ()
{
Connection. Close ();
Connection. Dispose ();
}

public void Dispose ()
{
Connection. Close ();
Connection. Dispose ();
Command. Dispose ();
}
#endregion

#region Operatons
Public SqlCeDataReader ExecuteReader (String sql)
{
Command.commandtext = SQL;
SqlCeDataReader reader = null;
Try
{
Reader = command. ExecuteReader ();
}
catch (DataException e)
{
Console.WriteLine (E.message);
}
return reader;
}

Public DataSet ExecuteDataset (String sql)
{
Command.commandtext = SQL;
SqlCeDataAdapter adapter = new SqlCeDataAdapter (command);
DataSet ds = new DataSet ();;

Try
{
Adapter. Fill (DS);
}
catch (DataException e)
{
Console.WriteLine (E.message);
}
return DS;
}

public int ExecuteNonQuery (String sql)
{
Command.commandtext = SQL;
int result =-1;

Try
{
result = command. ExecuteNonQuery ();
}
catch (DataException e)
{
Console.WriteLine (E.message);

}
return result;
}

public Object ExecuteScalar (String sql)
{
Command.commandtext = SQL;
Object o = null;
Try
{
o = command. ExecuteScalar ();
}
catch (DataException e)
{
Console.WriteLine (E.message);
}
return o;
}
#endregion

#region Transaction
public void BeginTransaction ()
{
Command. Transaction = connection. BeginTransaction ();
}

public void CommitTransaction ()
{
Command.Transaction.Commit ();
}

public void RollbackTransaction ()
{
Command. Transaction.rollback ();
}
#endregion
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.