您的位置:程序门 -> 硬件/嵌入开发 -> 非技术区



用c#编写wince串口通信应用程序的源代码


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


用c#编写wince串口通信应用程序的源代码
发表于:2007-09-07 15:55:20 楼主
最近要编写一个关于wince串口通信应用程序,谁有用c#编写的,能贴出来让大家分享吗?谢谢!
发表于:2007-10-08 09:00:441楼 得分:0
using   system;
using   system.io;
using   system.data.sqlserverce;
using   system.text;
using   system.data;
using   system.windows.forms;
using   system.data.sqlclient;
using   system.data.common;
using   system.runtime.interopservices;  
using   system.threading;


namespace   serialport_sqlce
{
///   <summary>
///   dataaccess   的摘要说明。
///   </summary>
public   class   class1
{

#region     对数据库的操作
private   sqlceconnection     conn   =   null;

///   <summary>
///   打开数据库连接
///   </summary>
public   void   opensqlceconnection()
{
if(conn   ==   null)
{
conn   =   new   sqlceconnection();
}
if(conn.state   ==   connectionstate.closed)
{
try
{
conn.open();
}
catch(exception   ex)
{
messagebox.show(ex.message);
}
}
}
///   <summary>
///   关闭数据库连接
///   </summary>
public   void   closesqlceconnection()
{
if(conn   !=   null)
{
if(conn.state   ==   connectionstate.open)
{
conn.close();
}
}
}
///   <summary>
///   创建数据库
///   </summary>
///   <returns> </returns>
public     sqlceengine   createsqlcedatabase()
{
if   (file.exists   ( "residentflash\\test.sdf ")   )     //确定指定的文件是否存在
file.delete   ( "residentflash\\test.sdf ");       //删除指定的文件。如果指定的文件不存在,则不引发异常。

sqlceengine   engine   =   new   sqlceengine   ( "data   source   =   residentflash\\test.sdf ");
engine.createdatabase   ();

conn   =   new   sqlceconnection   ( "data   source   =     residentflash\\test.sdf ");
opensqlceconnection();

closesqlceconnection();
return   engine;
}
///   <summary>
///   创建表格
///   </summary>
public   sqlcecommand   createtable()
{
conn   =   new   sqlceconnection   ( "data   source   =     residentflash\\test.sdf ");
opensqlceconnection();         //打开连接

sqlcecommand   sqlcecommand   =   conn.createcommand   ();

sqlcecommand.commandtext   =
"create   table   testtbl   (col1   int   primary   key) ";
sqlcecommand.EXECutenonquery();

closesqlceconnection();
return   sqlcecommand;
}
///   <summary>
///   向表中插入数据
///   </summary>
///   <returns> </returns>
public   sqlcecommand   insertdata(int   p1)
{
conn   =   new   sqlceconnection   ( "data   source   =     residentflash\\test.sdf ");
opensqlceconnection();                                                             //打开连接
 
sqlcecommand   cmd   =   conn.createcommand   ();

cmd.commandtext   =   "insert   into   testtbl   (col1)   values   (?) ";

cmd.parameters.add(new   sqlceparameter( "p1 ",   sqldbtype.int));

cmd.prepare();

cmd.parameters[ "p1 "].value   =   p1;

cmd.EXECutenonquery();

cmd.parameters.clear();
messagebox.show( "insert   data   ok! ");

closesqlceconnection();
return   cmd;
}
///   <summary>
///执行读的命令
///   </summary>
///   <returns> </returns>
public   string   readdata()
{
conn   =   new   sqlceconnection   ( "data   source   =     residentflash\\test.sdf ");
opensqlceconnection();     //打开连接
messagebox.show( "open   read   ");
                     
sqlcecommand   sqlcecommand   =   conn.createcommand();
sqlcecommand.commandtext   =  
"select   *   from   testtbl ";

sqlcecommand.EXECutenonquery();

messagebox.show( "select   *   from   testtb1 ");

sqlcedatareader   rdr   =   sqlcecommand.EXECutereader();

string   aa   =   null;
if(rdr.read())
{
aa   =   ( "   col1   =   "   +   rdr.getint32(0)  
// "   col2   =   "   +   rdr.getstring(1)   );
          );
messagebox.show( "read   data   ok! ");
return   aa;
}
else
{
messagebox.show( "read   data   failed ");
return   aa;
}  
}
#endregion

发表于:2007-10-08 09:01:342楼 得分:0
using   system;
using   system.io;
using   system.data.sqlserverce;
using   system.text;
using   system.data;
using   system.windows.forms;
using   system.data.sqlclient;
using   system.data.common;
using   system.runtime.interopservices;  
using   system.threading;


namespace   serialport_sqlce
{
///   <summary>
///   dataaccess   的摘要说明。
///   </summary>
public   class   class1
{

#region     对数据库的操作
private   sqlceconnection     conn   =   null;

///   <summary>
///   打开数据库连接
///   </summary>
public   void   opensqlceconnection()
{
if(conn   ==   null)
{
conn   =   new   sqlceconnection();
}
if(conn.state   ==   connectionstate.closed)
{
try
{
conn.open();
}
catch(exception   ex)
{
messagebox.show(ex.message);
}
}
}
///   <summary>
///   关闭数据库连接
///   </summary>
public   void   closesqlceconnection()
{
if(conn   !=   null)
{
if(conn.state   ==   connectionstate.open)
{
conn.close();
}
}
}
///   <summary>
///   创建数据库
///   </summary>
///   <returns> </returns>
public     sqlceengine   createsqlcedatabase()
{
if   (file.exists   ("residentflash\\test.sdf")   )     //确定指定的文件是否存在
file.delete   ("residentflash\\test.sdf");       //删除指定的文件。如果指定的文件不存在,则不引发异常。

sqlceengine   engine   =   new   sqlceengine   ("data   source   =   residentflash\\test.sdf");
engine.createdatabase   ();

conn   =   new   sqlceconnection   ("data   source   =     residentflash\\test.sdf");
opensqlceconnection();

closesqlceconnection();
return   engine;
}
///   <summary>
///   创建表格
///   </summary>
public   sqlcecommand   createtable()
{
conn   =   new   sqlceconnection   ("data   source   =     residentflash\\test.sdf");
opensqlceconnection();         //打开连接

sqlcecommand   sqlcecommand   =   conn.createcommand   ();

sqlcecommand.commandtext   =
"create   table   testtbl   (col1   int   primary   key)";
sqlcecommand.EXECutenonquery();

closesqlceconnection();
return   sqlcecommand;
}
///   <summary>
///   向表中插入数据
///   </summary>
///   <returns> </returns>
public   sqlcecommand   insertdata(int   p1)
{
conn   =   new   sqlceconnection   ("data   source   =     residentflash\\test.sdf");
opensqlceconnection();                                                             //打开连接
 
sqlcecommand   cmd   =   conn.createcommand   ();

cmd.commandtext   =   "insert   into   testtbl   (col1)   values   (?)";

cmd.parameters.add(new   sqlceparameter("p1",   sqldbtype.int));

cmd.prepare();

cmd.parameters["p1"].value   =   p1;

cmd.EXECutenonquery();

cmd.parameters.clear();
messagebox.show("insert   data   ok!");

closesqlceconnection();
return   cmd;
}
///   <summary>
///执行读的命令
///   </summary>
///   <returns> </returns>
public   string   readdata()
{
conn   =   new   sqlceconnection   ("data   source   =     residentflash\\test.sdf");
opensqlceconnection();     //打开连接
messagebox.show("open   read   ");
                     
sqlcecommand   sqlcecommand   =   conn.createcommand();
sqlcecommand.commandtext   =  
"select   *   from   testtbl";

sqlcecommand.EXECutenonquery();

messagebox.show("select   *   from   testtb1");

sqlcedatareader   rdr   =   sqlcecommand.EXECutereader();

string   aa   =   null;
if(rdr.read())
{
aa   =   ("   col1   =   "   +   rdr.getint32(0)  
// "   col2   =   "   +   rdr.getstring(1)   );
          );
messagebox.show("read   data   ok!");
return   aa;
}
else
{
messagebox.show("read   data   failed");
return   aa;
}  
}
#endregion



快速检索

最新资讯
热门点击