SqlServerCe 数据库操作示例

 

代码
 
   
using System;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlServerCe;
using System.IO;
using System.Text;


namespace SmartDeviceTest1
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[MTAThread]
static void Main()
{

CreatDB();

// Application.Run(new Form1());
}

public static void CreatDB()
{
SqlCeConnection conn
= null ;
try
{
if (File.Exists( " Test.sdf " )) File.Delete( " Test.sdf " );
SqlCeEngine engine
= new SqlCeEngine( " Data Source = Test.sdf " );
engine.CreateDatabase();

conn
= new SqlCeConnection( " Data Source = Test.sdf " );
conn.Open();

SqlCeCommand cmd
= conn.CreateCommand();
cmd.CommandText
= " CREATE TABLE TestTbl(col1 int PRIMARY KEY, col2 ntext, col3 money) " ;

cmd.ExecuteNonQuery();

cmd.CommandText
= " INSERT INTO TestTbl(col1, col2, col3) VALUES (0, 'abc', 15.66) " ;

cmd.ExecuteNonQuery();

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();
cmd.Parameters[
" p1 " ].Value = 1 ;
cmd.Parameters[
" p2 " ].Value = " abc " ;
cmd.Parameters[
" p3 " ].Value = 15.66 ;
cmd.ExecuteNonQuery();

cmd.Parameters.Clear();

cmd.CommandText
= " SELECT * FROM TestTbl " ;

SqlCeDataReader rdr
= cmd.ExecuteReader();

while (rdr.Read())
{
MessageBox.Show(
" col1 = " + rdr.GetInt32( 0 ) +
" col2 = " + rdr.GetString( 1 ) +
" col3 = " + rdr.GetSqlMoney( 2 ));
}


cmd.CommandText
= " UPDATE TestTbl SET col2 = 'some new value' WHERE col1 = 0 " ;
cmd.ExecuteNonQuery();

cmd.CommandText
= " SELECT * FROM TestTbl " ;
rdr
= cmd.ExecuteReader();
while (rdr.Read())
{
MessageBox.Show(
" col1 = " + rdr.GetInt32( 0 ) +
" col2 = " + rdr.GetString( 1 ) +
" col3 = " + rdr.GetSqlMoney( 2 ));
}


}
// end try
catch (SqlCeException ee)
{
ShowErrors(ee);
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
}

public static void ShowErrors(SqlCeException e)
{
SqlCeErrorCollection errorCollection
= e.Errors;
StringBuilder bld
= new StringBuilder();
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);
}
}

}


}

 

转载于:https://www.cnblogs.com/fightLonely/archive/2010/05/13/1734691.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
sql server ce 的工具 非常有用 SQL Server 2005 移动版(SQL Server Mobile)或SQL Server 2000 Windows CE 2.0版(SQL Server CE 2.0)的企业和个人用户如果计划与SQL Server 2000或SQL Server 2005数据库保持同步,需要在您运行Microsoft Internet Information Services(IIS)的服务器上安装互联工具。 本页内容表述的是互联工具。请注意文件名中的“xx”是代表安装语言的标识符。最初仅有英文互联工具信息(“en”即标识符“XX”)。为满足向后兼容的需要,我们提供SQL Server CE 2.0复制软件。 请查阅安装互联工具的设置说明。 1. Microsoft SQL Server 2005移动版服务器工具(sqlce30setupxx.msi)在IIS 箱中安装SQL Server Mobile复制组件。 这个组件用于把移动设备中的SQL Server Mobile连接到SQL Server 2005、SQL Server 2000 SP3a、及 SQL Server 2000 SP4数据库。 2. Microsoft SQL Server 2000 Service Pack 4复制组件(sql2kxxsp4.msi)在IIS机器中安装SQL Server 2000 SP3a复制组件。这个组件用于把移动设备中的SQL Mobile数据库连接到SQL Server 2000 SP4数据库。 3. Microsoft SQL Server 2000 Service Pack 3a复制组件(sql2kxxsp3a.msi)在IIS机器中安装 SQL Server CE 2.0 及 SQL Server 2000 SP4 复制组件。 该组件用于把移动设备中的SQL Server CE 2.0数据库连接到SQL Server 2000 SP3a数据库。 4. 用于SQL Server 2000 SP4 (sqlce20sql2ksp4.exe)的SQL Server CE 2.0 复制组件在IIS机器中安装 SQL Server CE 2.0 及 SQL Server 2000 SP4 复制组件。该组件用于把移动设备中的SQL Server CE 2.0数据库连接到SQL Server 2000 SP4。 5. 用于SQL Server 2000 SP3a (sqlce20sql2ksp3a.exe)的SQL Server CE 2.0复制组件在IIS机器中安装SQL Server CE 2.0 及 SQL Server 2000 SP3a 复制组件。该组件用于把移动设备中的SQL Server CE 2.0数据库连接到SQL Server 2000 SP3a 数据库
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值