📄 datasync.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Threading;
namespace PDA_test
{
/// <summary>
/// dataSync 的摘要说明。
/// </summary>
public class dataSync : System.Windows.Forms.Form
{
private System.Windows.Forms.Button CloseButton;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Label label1;
private PDA_test.ReaderForm mF_Form ;
private System.Data.SqlClient.SqlConnection sql_conn;
private System.Data.SqlClient.SqlCommand sql_cmd;
private System.Data.SqlClient.SqlConnection sql_conn2;
private System.Data.SqlClient.SqlCommand sql_cmd2;
private SqlDataReader myReader2;
public string Sql_string;
//private System.Data.DataTable data_uid;
private string connSql;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.PictureBox pictureBox2;
private System.Windows.Forms.PictureBox pictureBox3;
private System.Windows.Forms.Label label5;
private string fname="table";
private string fname2="id";
private Boolean finishFlag;
public int lcounter,i;
public System.Windows.Forms.Timer timer2;
public StreamReader lineCount;
public StreamReader textID;
public string lc_sql_cmd="";
public string line;
//public string[] sArray;
public dataSync(ReaderForm myform)
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
this.mF_Form = myform ;
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(dataSync));
this.CloseButton = new System.Windows.Forms.Button();
this.timer1 = new System.Windows.Forms.Timer();
this.label1 = new System.Windows.Forms.Label();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.pictureBox3 = new System.Windows.Forms.PictureBox();
this.label5 = new System.Windows.Forms.Label();
this.timer2 = new System.Windows.Forms.Timer();
//
// CloseButton
//
this.CloseButton.Font = new System.Drawing.Font("楷体_GB2312", 18F, System.Drawing.FontStyle.Bold);
this.CloseButton.Location = new System.Drawing.Point(73, 216);
this.CloseButton.Size = new System.Drawing.Size(95, 34);
this.CloseButton.Text = "返 回";
this.CloseButton.Click += new System.EventHandler(this.CloseButton_Click);
//
// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 16);
this.label1.Size = new System.Drawing.Size(208, 40);
this.label1.Text = "label1";
this.label1.ParentChanged += new System.EventHandler(this.label1_ParentChanged);
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(24, 168);
this.progressBar1.Size = new System.Drawing.Size(192, 20);
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 192);
this.label2.Size = new System.Drawing.Size(216, 16);
this.label2.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// label3
//
this.label3.Location = new System.Drawing.Point(64, 72);
this.label3.Size = new System.Drawing.Size(136, 24);
this.label3.Text = "同步通信";
//
// label4
//
this.label4.Location = new System.Drawing.Point(64, 104);
this.label4.Size = new System.Drawing.Size(136, 24);
this.label4.Text = "更新登录信息";
//
// pictureBox1
//
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(32, 64);
this.pictureBox1.Size = new System.Drawing.Size(24, 24);
this.pictureBox1.Visible = false;
//
// pictureBox2
//
this.pictureBox2.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox2.Image")));
this.pictureBox2.Location = new System.Drawing.Point(32, 96);
this.pictureBox2.Size = new System.Drawing.Size(24, 24);
this.pictureBox2.Visible = false;
//
// pictureBox3
//
this.pictureBox3.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox3.Image")));
this.pictureBox3.Location = new System.Drawing.Point(32, 128);
this.pictureBox3.Size = new System.Drawing.Size(24, 24);
this.pictureBox3.Visible = false;
//
// label5
//
this.label5.Location = new System.Drawing.Point(64, 136);
this.label5.Size = new System.Drawing.Size(136, 24);
this.label5.Text = "同步数据";
//
// timer2
//
this.timer2.Tick += new System.EventHandler(this.timer2_Tick);
//
// dataSync
//
this.Controls.Add(this.pictureBox3);
this.Controls.Add(this.label5);
this.Controls.Add(this.pictureBox2);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.label1);
this.Controls.Add(this.CloseButton);
this.Text = "dataSync";
this.Load += new System.EventHandler(this.dataSync_Load);
}
#endregion
private void CloseButton_Click(object sender, System.EventArgs e)
{
this.mF_Form.Show();
this.Close();
}
private void dataSync_Load(object sender, System.EventArgs e)
{
CloseButton.Enabled=false;
label1.Text="正在连接数据库...";
timer1.Interval=1000;
timer1.Enabled=false;
finishFlag=true;
connSql="data source=172.16.80.25;user id=sa;password=";
try
{
sql_conn = new SqlConnection(connSql);
}
catch(SqlException ex)
{
MessageBox.Show("请确认与计算机连接"+ex.Message);
}
try
{
sql_conn.Open();
sql_cmd = new SqlCommand();
sql_cmd.Connection=sql_conn;
label1.Text="数据库连接成功";
pictureBox1.Visible=true;
}
catch(Exception ex)
{
MessageBox.Show("连接数据库失败"+ex.ToString());
}
if (updateUser())
{
pictureBox2.Visible=true;
CloseButton.Enabled=true;
}
else
CloseButton.Enabled=true;
try
{
SqlDataReader myReader;
int tableColCount,tableRowCount;
// sql_cmd.CommandText="select count(*) colNum from zcgl.dbo.syscolumns where id in (select id from zcgl.dbo.sysobjects where name='dataTable')";
// myReader=sql_cmd.ExecuteReader();
// myReader.Read();
// tableColCount=Convert.ToInt32(myReader["colNum"]);
// myReader.Close();
sql_cmd.CommandText="select count(*) rowNum from zcgl.dbo.dataTable";
myReader=sql_cmd.ExecuteReader();
myReader.Read();
tableRowCount=Convert.ToInt32(myReader["rowNum"]);
myReader.Close();
progressBar1.Minimum=0;
progressBar1.Maximum=tableRowCount;
StreamWriter sw = File.CreateText(fname);
sw.Write("");
sw.Close();
// sql_cmd.CommandText="select name from zcgl.dbo.syscolumns where id in (select id from zcgl.dbo.sysobjects where name='dataTable') order by colid";
// myReader=sql_cmd.ExecuteReader();
// string strTitle="";
// myReader.Read();
// strTitle+=myReader["name"].ToString();
// while(myReader.Read())
// {
// strTitle+="*";
// strTitle+=myReader["name"].ToString();
// }
// myReader.Close();
// StreamWriter sw2 = File.AppendText(fname);
// sw2.WriteLine(strTitle);
// sql_cmd.CommandText="select * from zcgl.dbo.dataTable";
sql_cmd.CommandText="exec zcgl.dbo.移动_设备资产查询";
myReader=sql_cmd.ExecuteReader();
tableColCount=myReader.FieldCount;
string strTitle="";
strTitle+=myReader.GetName(0).ToString().Trim();
for(int i=1;i<tableColCount;i++)
{
strTitle+="*";
strTitle+=myReader.GetName(i).ToString().Trim();
}
StreamWriter sw2 = File.AppendText(fname);
sw2.WriteLine(strTitle);
while(myReader.Read())
{
string str="";
str=myReader[0].ToString();
for(int i=1;i<tableColCount;i++)
{
str+="*";
str+=myReader[i].ToString();
}
// str=myReader["UserID"].ToString()+"*"+myReader["password"].ToString()+"*"+myReader["name"].ToString();
sw2.WriteLine (str);
progressBar1.Value++;
label2.Text="完成"+progressBar1.Value.ToString()+"条,共"+progressBar1.Maximum+"条";
}
pictureBox3.Visible=true;
myReader.Close();
sw2.Close();
}
catch(Exception ex)
{
MessageBox.Show("资产表写入失败"+ex);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -