SQLServe Mobile数据库以前的版本是SQLServe CE,现在最新的是3.5的版本,最近用vs2008做了一个小程序,是对SQLServe Mobile数据库读写数据。
注:SQLServe Mobile数据库安装已经有人写过了,这里不再赘述。
1、界面是四个button按钮,分别是“插入数据”、“读取数据”、“删除数据”、“退出系统”和一个detagrid控件;两个textbox控件,方便录入插入的数据。
数据库文件时myDB.sdf 用SQL Server2008创建的,方法是:打开连接,服务器类型选择“SQL Server Compact
Edition”,数据库文件选择“创建新数据库”,然后根据提示创建数据库文件。需要注意的是,加密类型选择后两种,然后把数据库文件放到设备的NandFlash里(我测试用的设备是飞淩家的TE6410)。
项目中要引用“System.Data.SqlServerCe”,并且需要添加三个dll文件:sqlceme35.dll,sqlceqp35.dll,sqlcese35.dll,开发平台不安装SQLServe Mobile数据库是不能引用“System.Data.SqlServerCe”的,我安装的是英文版的Mobile数据库,这些文件在网上都能找到。
2、代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 定义连接字符串
/// </summary>
///
public string connectionString = "Data Source=\\NandFlash\\myDB.sdf;Database Password=xxxxx;";
private void button_Insert_Click(object sender, EventArgs e)
{
APP_function.CloseKeyBoard();//关闭软键盘
string name = this.textBox_Name.Text;
string pass = this.textBox_Pass.Text;
SqlCeParameter[] sqlParameters=new SqlCeParameter[2];
sqlParameters[0] = new SqlCeParameter("@Name", textBox_Name.Text);
sqlParameters[1] = new SqlCeParameter("@Password", textBox_Pass.Text);
string sqlCmd = "insert into userinfo (用户名,密码) values (@Name,@Password)";
if (name != "" || pass != "")
{
APP_function.P_ExecuteNonQuery(sqlCmd,connectionString,ref sqlParameters);
//更新datagrid
string sqlUpdataCmd = "select * from userinfo order by ID";
DataSet ds = new DataSet();
APP_function.ExecuteDataset(sqlUpdataCmd, connectionString, out ds);
dataGrid_Read.DataSource = ds.Tables[0];
ifReadSelected = true;
textBox_Name.Text = "";
textBox_Pass.Text = "";
}
else
{
MessageBox.Show("插入的用户名和密码不能为空!");
}
}
private void button_Read_Click(object sender, EventArgs e)
{
string sqlCmd = "select * from userinfo order by ID";
try
{
DataSet ds = new DataSet();
APP_function.ExecuteDataset(sqlCmd, connectionString, out ds);
dataGrid_Read.DataSource = ds.Tables[0];
ifReadSelected = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
/// <summary>
/// 定义是否选择了读数据变量ifReadSelected
/// </summary>
public static bool ifReadSelected = false;
private void button_Delete_Click(object sender, EventArgs e)
{
if (ifReadSelected)
{
//取得主键的值
DataGridCell selectCell = dataGrid_Read.CurrentCell;
int id = Convert.ToInt32(dataGrid_Read[selectCell.RowNumber, 0].ToString());
string sqlCmd = "delete userinfo where ID='" + id + "'";
APP_function.ExecuteNonQuery(sqlCmd, connectionString);
//更新datagrid
string sqlUpdataCmd = "select * from userinfo order by ID";
DataSet ds = new DataSet();
APP_function.ExecuteDataset(sqlUpdataCmd, connectionString, out ds);
dataGrid_Read.DataSource = ds.Tables[0];
}
else
{
MessageBox.Show("请选择删除的行!","提示");
}
}
private void textBox_Name_GotFocus(object sender, EventArgs e)
{
//打开键盘
APP_function.OpenKeyBoard();
}
private void textBox_Pass_GotFocus(object sender, EventArgs e)
{
//打开键盘
APP_function.OpenKeyBoard();
}
private void button_Exit_Click(object sender, EventArgs e)
{
this.Close();
Application.Exit();
}
}
3、APP_function是另外一个类文件,其中定义了需要的功能函数,代码如下:
public partial class APP_function
{
/// <summary>
/// 打开、关闭软键盘
/// </summary>
public static uint SIPF_OFF = 0x00;//软键盘关闭
public static uint SIPF_ON = 0x01;//软键盘打开
[DllImport("coredll.dll")]
public extern static void SipShowIM(uint dwFlag);
public static void OpenKeyBoard()
{
SipShowIM(SIPF_ON);
}
public static void CloseKeyBoard()
{
SipShowIM(SIPF_OFF);
}
/// <summary>
/// 向数据库插入、更新、删除数据(不带参数)
/// </summary>
/// <param name="sqlCmd">命令字符串</param>
/// <param name="connectionString">连接字符串</param>
///
public static void ExecuteNonQuery(string sqlCmd, string connectionString)
{
using (SqlCeConnection conn=new SqlCeConnection(connectionString))
{
try
{
using (SqlCeCommand cmd = new SqlCeCommand(sqlCmd, conn))
{
conn.Open();
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString(),"提示");
}
finally
{
conn.Close();
}
}
}
/// <summary>
/// 带参数向数据库插入、更新、删除数据
/// </summary>
/// <param name="sqlCmd">命令字符串</param>
/// <param name="connectionString">连接字符串</param>
///
static public void P_ExecuteNonQuery(string sqlCmd,string connectionString,ref SqlCeParameter[] sqlParameters)
{
using (SqlCeConnection conn = new SqlCeConnection(connectionString))
{
try
{
using (SqlCeCommand cmd = new SqlCeCommand(sqlCmd, conn))
{
conn.Open();
cmd.CommandType = CommandType.Text;
for (int i = 0; i < sqlParameters.Length;i++ )
{
cmd.Parameters.Add(sqlParameters[i]);
}
cmd.ExecuteNonQuery();
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString(), "提示");
}
finally
{
conn.Close();
}
}
}
/// <summary>
/// 查询数据库,返回dataset
/// </summary>
/// <param name="sqlCmd">命令字符串</param>
/// <param name="connectionString">连接字符串</param>
/// <param name="connectionString">连接字符串</param>
///
public static void ExecuteDataset(string sqlCmd, string connectionString, out DataSet ds)
{
ds = new DataSet();
using(SqlCeConnection conn=new SqlCeConnection(connectionString))
{
try
{
using(SqlCeDataAdapter da=new SqlCeDataAdapter())
{
da.SelectCommand = new SqlCeCommand(sqlCmd, conn);
da.Fill(ds);
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
finally
{
conn.Close();
}
}
}
}