要在 windows mobile 上使用数据库,您需要 Microsoft SQL Server Compact 3.5 for Windows Mobile。http://www.microsoft.com/en-in/download/details.aspx?id=8831。您可以从给出的链接下载和安装。安装后 C:\Program Files\Microsoft SQL Server Compact Edition\v3.5\Devices\wce500\armv4i 将包含所有需要安装到您的手机的 CAB 文件。安装
- sqlce.ppc.wce5.armv4i.CAB
- sqlce.repl.ppc.wce5.armv4i.CAB
有关安装内容的更多信息,请参阅http://msdn.microsoft.com/en-us/library/bb986876.aspx
我写了一个小助手类来做所有的数据库事务。
public class DataBaseHelper
{
public enum typeOfQuery
{
insert,
update,
delete,
getScalar,
getDataSet,
getDataTable
};
private string connectionString = Program.Connection;
public object ExecuteDatabaseQuery(string query, Dictionary<string, object> dictionary, typeOfQuery typeOfQuery)
{
try
{
using (SqlCeConnection oCon = new SqlCeConnection(connectionString))
{
oCon.Open();
string oSql = query;
using (SqlCeCommand oCmd = new SqlCeCommand(oSql, oCon))
{
oCmd.CommandType = CommandType.Text;
if (dictionary != null)
{
if (dictionary.Count != 0)
{
foreach (KeyValuePair<string, object> pair in dictionary)
{
if (pair.Value is DateTime)
oCmd.Parameters.Add(pair.Key, SqlDbType.DateTime).Value = pair.Value ?? DBNull.Value;
else if (pair.Value is bool || pair.Value is Boolean)
oCmd.Parameters.Add(pair.Key, SqlDbType.Bit).Value = pair.Value ?? DBNull.Value;
else
oCmd.Parameters.Add(pair.Key, SqlDbType.NVarChar).Value = pair.Value ?? DBNull.Value;
}
}
}
// check what type of query using the enums in the constants.cs file
if ((typeOfQuery == (typeOfQuery.insert)) || (typeOfQuery == typeOfQuery.update) ||
(typeOfQuery == typeOfQuery.delete))
{
return oCmd.ExecuteNonQuery();
}
else if (typeOfQuery == typeOfQuery.getDataSet)
{
SqlCeDataAdapter adapter = new SqlCeDataAdapter(oCmd);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
return dataSet;
}
else if (typeOfQuery == typeOfQuery.getDataTable)
{
SqlCeDataAdapter adapter = new SqlCeDataAdapter(oCmd);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
return dataSet.Tables[0];
}
else if (typeOfQuery == typeOfQuery.getScalar)
{
object returnValue = oCmd.ExecuteScalar();
if (returnValue == null)
{
return string.Empty;
}
else
return returnValue;
}
}
}
}
catch (SqlCeException ex)
{
throw;
}
catch (Exception ex)
{
throw;
}
finally
{
}
return false;
}
}
您可以按如下方式调用此类
string query = @"SELECT * FROM TABLE
WHERE COL1 = @COL1";
Dictionary<string, object> dictionaryToInsert = new Dictionary<string, object>();
dictionaryToInsert.Add("@COL1", Col1Value);
return (DataTable)new DataBaseHelper().ExecuteDatabaseQuery(query,
dictionaryToInsert, DataBaseHelper.typeOfQuery.getDataTable);
同样,您也可以出于其他目的查询数据库。使用枚举并更改查询,您将得到结果。