CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  硬件/嵌入开发 >  嵌入开发(WinCE)

求WINCE.NET下SQL数据库和ACCESS操作的例子!(VS2003下C#开发)

楼主54783szg(百里洲)2005-07-27 16:11:04 在 硬件/嵌入开发 / 嵌入开发(WinCE) 提问

如题,最好有代码,本人刚开始学,请大家别见笑! 问题点数:100、回复次数:1Top

1 楼slek(我浮躁,可是我很厚道!)回复于 2005-07-27 22:38:26 得分 100

Creating   a   SQL   Server   CE   Database  
  The   following   procedures   show   how   to   create   a   new   Microsoft®   SQL   Server™   Windows®   CE   Edition   (SQL   Server   CE)   database   in   a   new   Microsoft   Visual   Studio®   .NET   project.  
   
  By   performing   the   steps   in   these   procedures,   you   can:    
   
  Create   a   new   SQL   Server   CE   database.    
  Create   a   table   in   the   database.    
  Populate   the   table   with   data.    
  For   the   complete   code   that   you   can   copy,   paste,   and   run   in   a   Visual   Studio   .NET   project,   see   "Walkthrough   Sample"   earlier   in   this   topic.  
   
  To   create   a   new   SQL   Server   CE   database    
   
  Start   Visual   Studio   .NET   and   open   a   new   project.    
  Create   references   to   the   namespaces   you   are   using:    
  using   System;  
  using   System.IO;  
  using   System.Text;  
  using   System.Data;  
  using   System.Data.SqlServerCe;  
  using   System.Collections;  
  using   System.Windows.Forms;  
  using   System.Data.Common;  
   
  Create   the   WalkThrough   class:    
  public   class   WalkThrough    
  {  
          static   void   Main()    
          {  
                  SqlCeConnection   conn   =   null;  
   
                  try    
                  {  
   
  Verify   that   a   database   with   the   name   you   plan   to   use   does   not   already   exist:    
                          if   (File.Exists   ("Test.sdf")   )  
                                  File.Delete   ("Test.sdf");  
   
  Create   an   empty   database,   by   using   the   SqlCeEngine   object:    
                          SqlCeEngine   engine   =   new   SqlCeEngine   ("Data   Source   =   Test.sdf");  
                          engine.CreateDatabase   ();  
   
  Connect   to   the   new   database,   by   using   the   SqlCeConnection   object:    
  conn   =   new   SqlCeConnection   ("Data   Source   =   Test.sdf");  
  conn.Open();  
   
  To   create   a   new   table    
   
  Create   an   instance   of   the   command   class,   by   using   the   SqlCeCommand   object:    
  SqlCeCommand   cmd   =   conn.CreateCommand();  
   
  Run   a   command   to   create   the   table:    
  cmd.CommandText   =   "CREATE   TABLE   TestTbl(col1   int   PRIMARY   KEY,   col2   ntext,   col3   money)";  
  cmd.ExecuteNonQuery();  
   
  To   populate   a   new   table   with   data    
   
  Run   a   command   to   insert   a   row   of   data:    
  cmd.CommandText   =     "INSERT   INTO   TestTbl(col1,   col2,   col3)   VALUES   (0,   'abc',   15.66)";  
  cmd.ExecuteNonQuery();  
   
  Using   SqlCeParameter,   create   a   command   using   parameters   to   insert   data   to   the   table   multiple   times:    
  cmd.CommandText   =   "INSERT   INTO   TestTbl(col1,   col2,   col3)   VALUES   (?,   ?,   ?)";  
   
  cmd.Parameters.Add(new   SqlCeParameter("p1",   SqlDbType.Int));  
  cmd.Parameters.Add(new   SqlCeParameter("p2",   SqlDbType.NText));  
  cmd.Parameters.Add(new   SqlCeParameter("p3",   SqlDbType.Money));  
   
  cmd.Parameters["p2"].Size   =   50;  
   
  cmd.Prepare();  
   
  Execute   the   parameterized   command   to   insert   data   to   the   table:    
  cmd.Parameters["p1"].value   =   1;  
  cmd.Parameters["p2"].value   =   "abc";  
  cmd.Parameters["p3"].value   =   15.66;  
  cmd.ExecuteNonQuery();  
   
  Clear   the   parameter,   and   check   the   data   you   inserted   into   the   table:    
  cmd.Parameters.Clear();  
  //Set   the   command   text   to   a   SELECT   query.  
  //  
  cmd.CommandText   =   "SELECT   *   FROM   TestTbl";  
   
  Reading   SQL   Server   CE   Database   Data  
  The   following   example   shows   how   to   read   data   in   an   existing   SQL   Server   CE   database   by   using   the   SqlCeDataReader   class:  
   
  SqlCeDataReader   rdr   =   cmd.ExecuteReader();  
  while   (rdr.Read())  
  {  
        MessageBox.Show("col1   =   "   +   rdr.GetInt32(0)   +    
              "col2   =   "   +   rdr.GetString(1)   +  
              "col3   =   "   +   rdr.GetSqlMoney(2));  
   
  For   the   complete   code   that   you   can   copy,   paste,   and   run   in   a   Visual   Studio   .NET   project,   see   "Walkthrough   Sample"   earlier   in   this   topic.  
   
  Updating   Data   in   a   SQL   Server   CE   Database  
  The   following   procedures   show   how   to   update   a   SQL   Server   CE   table   by   using   the   objects   and   classes   that   are   used   earlier   in   Creating   a   SQL   Server   CE   Database   and   Reading   SQL   Server   CE   Data.  
   
  By   performing   the   steps   in   these   procedures,   you   can:    
   
  Change   existing   data   in   a   SQL   Server   CE   table.    
  Read   data   in   a   SQL   Server   CE   table.    
  Handle   errors.    
  For   the   complete   code   that   you   can   copy,   paste,   and   run   in   a   Visual   Studio   .NET   project,   see   the   "Walkthrough   Sample"   earlier   in   this   topic.  
   
  To   update   data   in   a   SQL   Server   CE   table    
   
  Set   the   command   object   to   use   the   UPDATE   statement:    
  cmd.CommandText   =   "UPDATE   TestTbl   SET   col2   =   'some   new   value'   WHERE   col1   =   0";  
  cmd.ExecuteNonQuery();  
   
  To   read   data   in   a   SQL   Server   CE   table    
   
  Set   the   command   object   to   use   the   SELECT   statement:    
  cmd.CommandText   =   "SELECT   *   FROM   TestTbl";  
   
   
  Create   an   instance   of   SqlCeDataReader   to   read   the   data:    
  rdr   =   cmd.ExecuteReader();  
  while   (rdr.Read())  
  {  
  MessageBox.Show("col1   =   "   +   rdr.GetInt32(0)   +  
        "col2   =   "   +   rdr.GetString(1)   +    
        "col3   =   "   +   rdr.GetSqlMoney(2));  
  }  
  }  
   
  Catch   any   errors   by   using   SqlCeException,   and   close   the   connection   to   the   database:    
  catch   (SqlCeException   e)    
  {  
              ShowErrors(e);  
  }  
  finally    
  {  
        if(conn.State   ==   ConnectionState.Open)  
              conn.Close();  
  }  
  }  
   
          public   static   void   ShowErrors(SqlCeException   e)    
          {  
                  SqlCeErrorCollection   errorCollection   =   e.Errors;  
   
                  StringBuilder   bld   =   new   StringBuilder();  
                  Exception       inner   =   e.InnerException;  
   
                  foreach   (SqlCeError   err   in   errorCollection)    
                  {  
                          bld.Append("\n   Error   Code:   "   +   err.HResult.ToString("X"));  
                          bld.Append("\n   Message       :   "   +   err.Message);  
                          bld.Append("\n   Minor   Err.:   "   +   err.NativeError);  
                          bld.Append("\n   Source         :   "   +   err.Source);  
                                   
                          foreach   (int   numPar   in   err.NumericErrorParameters)    
                          {  
                                  if   (0   !=   numPar)   bld.Append("\n   Num.   Par.   :   "   +   numPar);  
                          }  
                                   
                          foreach   (string   errPar   in   err.ErrorParameters)    
                          {  
                                  if   (String.Empty   !=   errPar)   bld.Append("\n   Err.   Par.   :   "   +   errPar);  
                          }  
   
                          MessageBox.Show(bld.ToString());  
                          bld.Remove(0,   bld.Length);  
                  }  
          }  
  }  
   
  Top

相关问题

  • SQL数据库与C++ Builder编程
  • 用C++怎么连SQL数据库~
  • 关于在C#下连接SQL数据库的问题
  • C#的SQL数据库高手请解答
  • 求助:急求啊!!怎样在C#更新SQL数据库啊
  • Visual C#中如何连接SQL数据库
  • C# web service与sql数据库连接的问题,高手来
  • 怎么用ASP.net(C#)在SQL数据库里建表?
  • C#中SQL数据库关于中文字段的问题
  • 请问用C语言怎么连SQL数据库?

关键词

  • .net
  • ce
  • database
  • visual
  • sql
  • walkthrough
  • visual studio
  • create
  • project
  • sql server ce

得分解答快速导航

  • 帖主:54783szg
  • slek

相关链接

  • CSDN Blog
  • 技术文档
  • 代码下载
  • 第二书店
  • 读书频道

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo