private void btnStart_Click(object sender, EventArgs e) { try { if (!this.rdbConnected) { if (!this.ldbConnected) { this.localConn = new MsSqlOps(getConnStr(true)); this.ldbConnected = true; this.log("Local database connected!"); } this.remoteConn = new MsSqlOps(getConnStr(false)); this.rdbConnected = true; this.log("Remote database connected!"); this.timer.Start(); // 成功启动则使启动无效 this.btnStart.Enabled = false; this.btnClose.Enabled = true; } } catch (SqlException ex) { this.log("Start link failed!"); this.log(ex.Message); } }
public Server() { // init ocnfiguration file this.ini = new IniFile("Config.ini"); // init local database connection this.dbConn = new MsSqlOps(getConnStr()); this.dbConnected = true; // init tcp listener var lIP = this.ini.ReadString("TCPServer", "IP", "0.0.0.0"); var lPort = this.ini.ReadInteger("TCPServer", "Port", 54321); this.listener = new TcpListener(IPAddress.Parse(lIP), lPort); this.listenerOpened = true; // init last syncID and tables //this.tableNames = new StringCollection(); //this.ini.ReadSection("LastID", this.tableNames); //this.lastIDs = new Dictionary<string, int>(); //foreach (string tableName in this.tableNames) //{ // this.lastIDs[tableName] = this.ini.ReadInteger("LastID", tableName, 0); //} // init log file this.wLog = new StreamWriter("ServerSync.log", true); this.wLog.AutoFlush = true; }
// 启动时加载配置 private void Client_Load(object sender, EventArgs e) { IniFile ini = new IniFile("Config.ini"); this.dbServerName.Text = ini.ReadString("DBConnection", "Server", "."); this.dbName.Text = ini.ReadString("DBConnection", "DB", ""); this.dbIP.Text = ini.ReadString("DBConnection", "IP", "127.0.0.1"); this.dbPort.Value = ini.ReadInteger("DBConnection", "Port", 1433); this.userName.Text = ini.ReadString("DBConnection", "UID", "sa"); this.password.Text = ini.ReadString("DBConnection", "PW", "sa"); this.tcpServerIP.Text = ini.ReadString("TCPServer", "IP", "0.0.0.0"); this.tcpServerPort.Value = ini.ReadInteger("TCPServer", "Port", 54321); this.syncCycle.Value = ini.ReadInteger("SyncConfig", "Cycle", 5); var mode = ini.ReadInteger("DBConnection", "Mode", 1); if (0 == mode) { this.modeSql.Checked = false; this.modeWin.Checked = true; this.dbGroupBox.Enabled = false; } else { this.modeSql.Checked = true; this.modeWin.Checked = false; this.dbServerName.Enabled = false; } this.log("Load configuration done!"); // init last syncID and back-off time this.tableItems = new List <TableItem>(); NameValueCollection values = new NameValueCollection(); ini.ReadSectionValues("LastID", values); foreach (string key in values.Keys) { string col = values.Get(key).Split(',')[0]; int id = int.Parse(values.Get(key).Split(',')[1]); this.tableItems.Add(new TableItem(key, col, id)); } this.log("Load table items done!"); // init last syncID plus and back-off time values.Clear(); ini.ReadSectionValues("LastIDPlus", values); foreach (string key in values.Keys) { string col = values.Get(key).Split(',')[0]; int id = int.Parse(values.Get(key).Split(',')[1]); this.tableItems.Add(new TableItem(key, col, id, true)); } this.log("Load table items plus done!"); // init mssql connection this.dbConn = new MsSqlOps(); }
// 启动时加载配置 private void Client_Load(object sender, EventArgs e) { FileInfo fInfo = new FileInfo("Config.ini"); if (!fInfo.Exists) { this.log("Missing configure file!"); this.log("Now load default configures."); this.log("Please press save configuration button after you filled other blanks."); } IniFile ini = new IniFile("Config.ini"); var mode = ini.ReadInteger("DBConnection", "Mode", 1); if (0 == mode) { this.dbServerName.Text = ini.ReadString("DBConnection", "Server", "."); this.dbGroupBox.Enabled = false; this.modeSql.Checked = false; } else { this.modeWin.Checked = false; this.dbServerName.Enabled = false; this.dbIP.Text = ini.ReadString("DBConnection", "IP", "127.0.0.1"); this.dbPort.Value = ini.ReadInteger("DBConnection", "Port", 1433); this.userName.Text = ini.ReadString("DBConnection", "UID", "sa"); this.password.Text = ini.ReadString("DBConnection", "PW", "sa"); } this.dbName.Text = ini.ReadString("DBConnection", "DB", ""); this.tcpServerIP.Text = ini.ReadString("TCPServer", "IP", "0.0.0.0"); this.tcpServerPort.Value = ini.ReadInteger("TCPServer", "Port", 54321); this.syncCycle.Value = ini.ReadInteger("SyncConfig", "Cycle", 5); this.log("Load configuration done!"); // init last syncID and back-off time this.tableNames = new StringCollection(); ini.ReadSection("LastID", this.tableNames); this.lastIDs = new Dictionary <string, int>(); this.backoffTime = new Dictionary <string, int[]>(); foreach (string tableName in this.tableNames) { this.lastIDs[tableName] = ini.ReadInteger("LastID", tableName, 0); this.backoffTime[tableName] = new int[2] { 0, 2 }; } this.log("Load last syncIDs done!"); // init mssql connection this.dbConn = new MsSqlOps(); }
public Client() { // init config file this.ini = new IniFile("Config.ini"); // init mssql connection this.dbConn = new MsSqlOps(getConnStr()); this.dbConnected = true; // init tcp connection var addr = this.ini.ReadString("TCPServer", "IP", "localhost"); var port = this.ini.ReadInteger("TCPServer", "Port", 1433); this.client = new TcpClient(addr, port); this.stream2Server = this.client.GetStream(); this.reader = new StreamReader(this.stream2Server); this.writer = new StreamWriter(this.stream2Server); this.writer.AutoFlush = true; this.tcpConnected = true; // init last syncID and tables this.tableNames = new StringCollection(); this.ini.ReadSection("LastID", this.tableNames); this.lastIDs = new Dictionary <string, int>(); this.backoffTime = new Dictionary <string, int[]>(); foreach (string tableName in this.tableNames) { this.lastIDs[tableName] = this.ini.ReadInteger("LastID", tableName, 0); this.backoffTime[tableName] = new int[2] { 0, 2 }; } // init log file this.wLog = new StreamWriter("ClientSync.log", true); this.wLog.AutoFlush = true; // init timer this.timer = new Timer(); this.timer.Interval = this.ini.ReadInteger("SyncConfig", "Cycle", 5) * 1000; this.timer.Elapsed += new ElapsedEventHandler(this.timerCallback); }
private void btnStart_Click(object sender, EventArgs e) { try { if (this.dbConnected == false) { // init mssql connection this.dbConn = new MsSqlOps(getConnStr()); this.dbConnected = true; this.log("Connect to database succeed!"); } // init tcp link var lIP = this.tcpServerIP.Text; var lPort = (int)this.tcpServerPort.Value; this.client = new TcpClient(lIP, lPort); this.tcpConnected = true; this.btnStart.Enabled = false; this.btnExit.Enabled = true; this.log("Connect to tcp server succeed!"); // 新线程开启 this.tcpThread = new Thread(this.clientCallback); this.tcpThread.IsBackground = true; this.tcpThread.Start(); } catch (SqlException ex) { this.log("Link to database failed!"); this.log(ex.Message); } catch (SocketException ex) { this.log("Open tcp listener failed!"); this.log(ex.Message); } catch (Exception ex) { this.log(ex.Message); } }
public Transporter() { // init files this.ini = new IniFile("Config.ini"); //FileInfo fi = new FileInfo("Transporter.log"); //if (fi.Length > 20 * 1024 * 1024) //{ // fi.MoveTo(string.Format("Transporter-{0}.log", DateTime.Now.ToString())); //} this.wLog = new StreamWriter("Transporter.log", true); this.wLog.AutoFlush = true; // init database connection this.localConn = new MsSqlOps(getConnStr("LocalDBConnection")); this.ldbConnected = true; this.log("Local database connected!"); this.remoteConn = new MsSqlOps(getConnStr("RemoteDBConnection")); this.rdbConnected = true; this.log("Remote database connected!"); // init last syncID and back-off time this.tableNames = new StringCollection(); this.ini.ReadSection("LastID", this.tableNames); this.lastIDs = new Dictionary <string, int>(); this.backoffTime = new Dictionary <string, int[]>(); foreach (string tableName in this.tableNames) { this.lastIDs[tableName] = this.ini.ReadInteger("LastID", tableName, 0); this.backoffTime[tableName] = new int[2] { 0, 2 }; // 0为计数器,2为轮次 } // init timer this.timer = new Timer(); this.timer.Interval = this.ini.ReadInteger("SyncConfig", "Cycle", 5) * 1000; this.timer.Elapsed += new ElapsedEventHandler(this.timerCallback); }