我正在Windows Mobile 6.5设备中编写原型应用程序。 该应用程序的目标是向用户询问一些输入,收集数据并存储到本地数据库和服务器上。
我完成了创建应用程序的GUI(在C#中),它从用户那里获取所有必要的输入。
现在,我需要将此数据插入本地数据库并上传到服务器数据库。当用户选择这样做时,两个DB都需要通过HTTP同步。我几年前没有在数据库上工作过多,除了几年前在Linux环境中编写一些查询来从PostgreSQL中获取数据。
所以我的问题是,实现我想要的东西的最简单方法是什么?我不需要很多功能。数据只是字符串和数字(没有文件,多媒体东西等) 我应该安装和运行什么服务器?我应该在客户端使用哪些组件?
由于 阿希什
答案 0 :(得分:0)
要在Windows Mobile上使用数据库,您需要适用于Windows Mobile的Microsoft SQL Server Compact 3.5。 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文件。 安装
有关安装内容的详细信息,请参阅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);
同样,您也可以查询数据库以用于其他目的。使用枚举并更改查询,您将得到结果。