📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.IO;
using System.Reflection;
namespace RDA_demo
{
public partial class Form1 : Form
{
private string sqlAgent = "http://192.168.1.20/sqlmobile/sqlcesa30.dll";
string rdaOleDbConnStr = "Provider=sqloledb;Data Source=bjb-libo;Initial Catalog=Northwind;" +
"User Id=sa;Password=1234;";
private string dbfile = null;
private string connStr = null;
public Form1()
{
InitializeComponent();
dbfile = string.Format(@"{0}\Northwind.sdf",
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase));
connStr = string.Format(@"Data Source = {0}", dbfile);
}
private void UpdateData()
{
Cursor.Current = Cursors.WaitCursor;
try
{
SqlCeConnection conn = new SqlCeConnection(connStr);
SqlCeCommand cmd = new SqlCeCommand(
"Update Employees Set LastName = '波', FirstName='黎' Where EmployeeID=10", conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("数据更新成功!");
}
catch (SqlCeException ex)
{
ShowErrors(ex);
}
Cursor.Current = Cursors.Default;
}
private void PullData()
{
Cursor.Current = Cursors.WaitCursor;
try
{
SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess(sqlAgent, connStr);
rda.Pull(
"Employees",
"Select EmployeeID, LastName, FirstName, Title From Employees",
rdaOleDbConnStr,
RdaTrackOption.TrackingOnWithIndexes,
"errEmployeesTable");
rda.Dispose();
MessageBox.Show("数据下载成功!");
}
catch (SqlCeException ex)
{
ShowErrors(ex);
}
Cursor.Current = Cursors.Default;
}
private void PushData()
{
Cursor.Current = Cursors.WaitCursor;
try
{
SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess(sqlAgent, connStr);
rda.Push("Employees", rdaOleDbConnStr);
rda.Dispose();
MessageBox.Show("数据上传成功!");
}
catch (SqlCeException ex)
{
ShowErrors(ex);
}
Cursor.Current = Cursors.Default;
}
private void CreateDatebase()
{
Cursor.Current = Cursors.WaitCursor;
try
{
string dbFile = string.Format(@"{0}\Northwind.sdf",
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase));
if (File.Exists(dbFile))
{
File.Delete(dbFile);
}
SqlCeEngine engine = new SqlCeEngine(connStr);
engine.CreateDatabase();
engine.Dispose();
MessageBox.Show("数据库创建成功!");
}
catch (SqlCeException ex)
{
ShowErrors(ex);
}
Cursor.Current = Cursors.Default;
}
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",
System.Globalization.CultureInfo.CurrentCulture));
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());
}
private void button1_Click(object sender, EventArgs e)
{
CreateDatebase();
}
private void button2_Click(object sender, EventArgs e)
{
PullData();
}
private void button3_Click(object sender, EventArgs e)
{
UpdateData();
}
private void button4_Click(object sender, EventArgs e)
{
PushData();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -