📄 同步.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlServerCe;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Reflection;
using System.ComponentModel;
using System.Threading;
namespace AIFACE
{
public partial class 同步 : Form
{
private SqlCeConnection CEconnection = null;
private SqlCeCommand CEcommand = null;
private SqlCeResultSet CEresultSet = null;
public 同步()
{
InitializeComponent();
if (null == this.CEconnection)
{
string strCon = GetConnectionString();
this.CEconnection = new SqlCeConnection(strCon);
this.CEconnection.Open();
}
}
private string GetConnectionString()
{
return String.Format(System.Globalization.CultureInfo.InvariantCulture,
@"Data Source = {0}\AIFACE.sdf;password=wiseyes", Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase));
}
private void 同步_Load(object sender, EventArgs e)
{
try
{
if (null != this.CEresultSet)
this.CEresultSet.Dispose(); //释放Dataset
this.CEcommand = this.CEconnection.CreateCommand();
String query = "sb_zjk";
this.CEcommand.CommandText = query;
CEcommand.CommandType = CommandType.TableDirect;
CEcommand.IndexName = "PK_sb_zjk";
ResultSetOptions options = ResultSetOptions.Scrollable | ResultSetOptions.Sensitive | ResultSetOptions.Updatable;
this.CEresultSet = this.CEcommand.ExecuteResultSet(options);
}
catch (InvalidOperationException ex)
{
MessageBox.Show(String.Format(System.Globalization.CultureInfo.CurrentCulture,
"连接数据库发生错误: \n {0}", ex.ToString()));
}
catch (SqlCeException ex)
{
ShowErrors(ex);
}
}
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 button2_Click(object sender, EventArgs e)
{
this.pictureBox1.BackColor = Color.Transparent;
this.pictureBox1.Visible = true;
String path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
FileInfo info = new FileInfo(path + "\\Out.txt");
if (info.Exists)
{
info.Delete();
}
String query = "sb_zjk";
this.CEcommand.CommandText = query;
CEcommand.CommandType = CommandType.TableDirect;
CEcommand.IndexName = "PK_sb_zjk";
ResultSetOptions options = ResultSetOptions.Scrollable | ResultSetOptions.Sensitive | ResultSetOptions.Updatable;
this.CEresultSet = this.CEcommand.ExecuteResultSet(options);
using (FileStream fs = info.Create())
{
byte[] b1 = new byte[8];
long j = 0;
while (CEresultSet.Read())
{
if (CEresultSet.GetBoolean(9))
{
fs.Write(Encoding.Default.GetBytes(CEresultSet.GetString(0)),0,8);
fs.Write(Encoding.Default.GetBytes(CEresultSet.GetString(12)), 0, 4);
fs.WriteByte(13);
}
if ((j % 1000) == 0)
{
this.pictureBox1.Left = this.pictureBox1.Left - 42;
if (this.pictureBox1.Left < -496)
this.pictureBox1.Left = 84;
this.pictureBox1.Refresh();
}
j++;
}
MessageBox.Show("数据导出完成!");
this.Close();
}
this.Visible = false;
}
private void button1_Click(object sender, EventArgs e)
{
//this.ControlDisable(this);
this.pictureBox1.BackColor = Color.Transparent;
this.pictureBox1.Visible = true;
aaa();
this.pictureBox1.Visible = false;
}
private void 同步_Deactivate(object sender, EventArgs e)
{
//this.Dispose();
}
private void ControlDisable(Form f)
{
for (int i = 0; i < f.Controls.Count; i++)
{
f.Controls[i].Enabled = false;
}
f.Refresh();
}
private void ControlEnable(Form f)
{
for (int i = 0; i < f.Controls.Count; i++)
{
f.Controls[i].Enabled = true;
}
f.Refresh();
}
#region 导入
private void aaa()
{
String path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
FileInfo info = new FileInfo(path + "\\In.txt");
if (info.Exists)
{
this.CEresultSet.Close();
this.CEcommand.CommandText = "Delete from sb_zjk";
this.CEcommand.CommandType = CommandType.Text;
this.CEcommand.ExecuteNonQuery();
String query = "sb_zjk";
this.CEcommand.CommandText = query;
CEcommand.CommandType = CommandType.TableDirect;
CEcommand.IndexName = "PK_sb_zjk";
ResultSetOptions options = ResultSetOptions.Scrollable | ResultSetOptions.Sensitive | ResultSetOptions.Updatable;
this.CEresultSet = this.CEcommand.ExecuteResultSet(options);
using (FileStream fs = info.OpenRead())
{
byte[] b1 = new byte[8];
byte[] b2 = new byte[30];
byte[] b3 = new byte[8];
byte[] b4 = new byte[30];
byte[] b5 = new byte[20];
byte[] b6 = new byte[24];
byte[] b7 = new byte[14];
byte[] b8 = new byte[8];
byte[] b9 = new byte[14];
byte[] b0 = new byte[2];
byte[] ba = new byte[4];
byte[] bb = new byte[4];
SqlCeUpdatableRecord rs = this.CEresultSet.CreateRecord();
int i = 0;
long j = 0;
fs.Seek(0, SeekOrigin.Begin);
while (fs.Read(b1, i, b1.Length) > 0)
{
//MessageBox.Show(Encoding.Default.GetString(b1, 0, b1.Length));
rs.SetString(0, Encoding.Default.GetString(b1, 0, b1.Length));
fs.Read(b2, i, b2.Length);
//MessageBox.Show(Encoding.Default.GetString(b2, 0, b2.Length));
rs.SetString(1, Encoding.Default.GetString(b2, 0, b2.Length));
fs.Read(b3, i, b3.Length);
rs.SetString(2, Encoding.Default.GetString(b3, 0, b3.Length));
fs.Read(b4, i, b4.Length);
rs.SetString(3, Encoding.Default.GetString(b4, 0, b4.Length));
fs.Read(ba, i, ba.Length);
rs.SetString(11, Encoding.Default.GetString(ba, 0, ba.Length));
fs.Read(b5, i, b5.Length);
rs.SetString(4, Encoding.Default.GetString(b5, 0, b5.Length));
fs.Read(b6, i, b6.Length);
rs.SetString(5, Encoding.Default.GetString(b6, 0, b6.Length));
fs.Read(b7, i, b7.Length);
rs.SetString(6, Encoding.Default.GetString(b7, 0, b7.Length).Trim());
fs.Read(b8, i, b8.Length);
rs.SetString(7, Encoding.Default.GetString(b8, 0, b8.Length).Trim());
fs.Read(b9, i, b9.Length);
rs.SetString(8, Encoding.Default.GetString(b9, 0, b9.Length).Trim());
fs.Read(b0, i, b0.Length);
rs.SetValue(9, 0);
rs.SetValue(10, 0);
rs.SetValue(12, 0);
try
{
this.CEresultSet.Insert(rs);
}
catch (Exception ex)
{
}
if ((j % 100) == 0)
{
this.pictureBox1.Left = this.pictureBox1.Left - 42;
if (this.pictureBox1.Left < -496)
this.pictureBox1.Left = 84;
this.pictureBox1.Refresh();
}
j++;
}
this.CEcommand.CommandText = "Delete from dm_sydw where 部门代码 <> '0000'";
this.CEcommand.CommandType = CommandType.Text;
this.CEcommand.ExecuteNonQuery();
this.CEcommand.CommandText = "insert into dm_sydw(部门代码,使用部门) select distinct 部门代码,使用部门 from sb_zjk";
this.CEcommand.CommandType = CommandType.Text;
this.CEcommand.ExecuteNonQuery();
this.CEcommand.CommandText = "update system set Value = '0000' where Item = 'UNIT'";
this.CEcommand.CommandType = CommandType.Text;
this.CEcommand.ExecuteNonQuery();
MessageBox.Show("数据导入完成");
this.Close();
}
}
else
{
MessageBox.Show("指定文件不存在");
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -