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

54783szg 2005-07-27 04:11:04
如题,最好有代码,本人刚开始学,请大家别见笑!
...全文
408 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
西山小月 2005-07-27
  • 打赏
  • 举报
回复
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);
}
}
}

19,502

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 嵌入开发(WinCE)
社区管理员
  • 嵌入开发(WinCE)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧