对于windows mobile sql 中的sdf文件,很多朋友都会用到。方法也很简单。同PC上操作SQL server,Oracle等都差不多。
下面共享一个操作SDF数据库文件的一个类,供Mobile开发新入门的朋友参考。欢迎留言进行交流。
在用此代码之前,需要 using System.Data.SqlServerCe;
class
CESql
... {
public void ConnOpen(SqlCeConnection Conn)
...{
if (Conn.State.ToString().Trim().Equals("Open"))
...{
Conn.Close();
}
Conn.Open();
}
public void ConnClose(SqlCeConnection Conn)
...{
if (Conn.State.ToString().Trim().Equals("Open"))
...{
Conn.Close();
}
}
public void ExecuteSql(string sql, SqlCeConnection Conn)
...{
SqlCeCommand MyCommand = new SqlCeCommand(sql, Conn);
MyCommand.ExecuteNonQuery();
Conn.Close();
}
public SqlCeDataReader GetDataReader(string strCMD, SqlCeConnection Conn)
...{
SqlCeDataReader myReader;
SqlCeCommand mycommand = new SqlCeCommand();
mycommand.CommandText = strCMD;
mycommand.Connection = Conn;
myReader = mycommand.ExecuteReader();
return myReader;
}
}
... {
public void ConnOpen(SqlCeConnection Conn)
...{
if (Conn.State.ToString().Trim().Equals("Open"))
...{
Conn.Close();
}
Conn.Open();
}
public void ConnClose(SqlCeConnection Conn)
...{
if (Conn.State.ToString().Trim().Equals("Open"))
...{
Conn.Close();
}
}
public void ExecuteSql(string sql, SqlCeConnection Conn)
...{
SqlCeCommand MyCommand = new SqlCeCommand(sql, Conn);
MyCommand.ExecuteNonQuery();
Conn.Close();
}
public SqlCeDataReader GetDataReader(string strCMD, SqlCeConnection Conn)
...{
SqlCeDataReader myReader;
SqlCeCommand mycommand = new SqlCeCommand();
mycommand.CommandText = strCMD;
mycommand.Connection = Conn;
myReader = mycommand.ExecuteReader();
return myReader;
}
}