public int InsertUploadRow(EntityUpload entity) { string SqlStr = ""; SqlStr = "insert into tc_upload_log(upload_date, upload_result,upload_ip)"; SqlStr += " values('" + entity.Upload_date + "','" + entity.Upload_result.Trim() + "','" + entity.Upload_ip.Trim() + "')"; SqlCommand sqlCommand = new SqlCommand(); sqlCommand.CommandText = SqlStr; try { ExcuteSql(sqlCommand); } catch { return(Constants.SystemConfig.SERVER_ERROR); } return(Constants.SystemConfig.SERVER_SUCCESS); }
private void FileUpload() { Setlabel("正在压缩数据..."); DataAccess dataAccess = new DataAccess(); dataAccess.Open(); GetData getData = new GetData(dataAccess.Connection); DataTable dtCompany = getData.GetSingleTable("tc_company"); if (dtCompany == null || dtCompany.Rows.Count == 0) { SetformSize(new System.Drawing.Size(298, 170)); SetprocBar(proBar); SetlabelVisible(labTarg); MessageBox.Show("先填写公司信息后才可以数据申报!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } string companyName = dtCompany.Rows[0]["company_name"].ToString(); string businessLis = dtCompany.Rows[0]["company_business_licence"].ToString(); dataAccess.Open(); SqlCommand cmdBK = new SqlCommand(); cmdBK.CommandType = CommandType.Text; cmdBK.Connection = dataAccess.Connection; if (!File.Exists(LoginUser.SqlSetUpPath.Substring(0, LoginUser.SqlSetUpPath.Length - 1))) { Directory.CreateDirectory(LoginUser.SqlSetUpPath.Substring(0, LoginUser.SqlSetUpPath.Length - 1)); } DateTime dateNow = DateTime.Now; file = LoginUser.SqlSetUpPath + companyName + "_" + businessLis; cmdBK.CommandText = "backup database tc_invoicing to disk='" + file + "' with init"; cmdBK.ExecuteNonQuery(); //dataAccess.Close(); try { //压缩 ZipOutputStream output = new ZipOutputStream(File.Create(file + ".dat")); output.SetLevel(5); Crc32 crc = new Crc32(); FileStream fsR = new FileStream(file, FileMode.Open, FileAccess.Read); byte[] bt = new byte[fsR.Length]; fsR.Read(bt, 0, bt.Length); //存储要压缩的文件 ZipEntry entry = new ZipEntry(file.Substring(file.LastIndexOf('\\') + 1)); entry.Size = fsR.Length; entry.DateTime = DateTime.Now; fsR.Close(); crc.Reset(); //清除crc内容 crc.Update(bt); //更新文件内容到crc中 entry.Crc = crc.Value; //将文件内容放到压缩文件中 output.PutNextEntry(entry); //将数据写入压缩流中 output.Write(bt, 0, bt.Length); output.Close(); stream = this.TransferDocument(file + ".dat"); long maxTransBuffer = long.Parse(ConfigurationSettings.AppSettings["maxTransBuffer"]); if (stream.Length > maxTransBuffer) { SetformSize(new System.Drawing.Size(298, 170)); SetprocBar(proBar); SetlabelVisible(labTarg); MessageBox.Show("数据文件过大,无法上传!最大数据量为:5M。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } fileTransportServiceClient = new FileTransportServiceClient(); fileTransportServiceClient.ChannelFactory.Endpoint.Address = new EndpointAddress("http://" + labAddress.Text.Trim() + ":80/FileUpLoad/"); Setlabel("正在连接服务器..."); fileTransportServiceClient.Open(); Setlabel("上传数据中,请稍候..."); fileTransportServiceClient.UploadFile(file.Substring(file.LastIndexOf('\\') + 1) + ".dat", stream); SetprocBar(proBar); SetlabelVisible(labTarg); MessageBox.Show("数据上传成功!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); uploadSuccess = true; Setbtn(btnSend, btnSetAddress); SetformSize(new System.Drawing.Size(298, 170)); } catch (CommunicationException commEx) { SetformSize(new System.Drawing.Size(298, 170)); SetprocBar(proBar); SetlabelVisible(labTarg); MessageBox.Show("上传失败:远程服务器无法连接,请查看服务器地址是否正确或服务器是否开启!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); uploadSuccess = false; Setbtn(btnSend, btnSetAddress); } catch (System.TimeoutException timeEx) { SetformSize(new System.Drawing.Size(298, 170)); SetprocBar(proBar); SetlabelVisible(labTarg); MessageBox.Show("连接人数过多,请等待5分钟后再次申报!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); uploadSuccess = false; Setbtn(btnSend, btnSetAddress); } catch (Exception ex) { SetformSize(new System.Drawing.Size(298, 170)); SetprocBar(proBar); SetlabelVisible(labTarg); MessageBox.Show("上传失败:" + ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); uploadSuccess = false; Setbtn(btnSend, btnSetAddress); } finally { Setbtn(btnSend, btnSetAddress); if (stream != null) { stream.Close(); stream.Dispose(); stream = null; } if (File.Exists(file)) { File.Delete(file); } if (File.Exists(file + ".dat")) { File.Delete(file + ".dat"); } if (fileTransportServiceClient != null && fileTransportServiceClient.State == CommunicationState.Opened) { fileTransportServiceClient.Close(); fileTransportServiceClient = null; } if (uploadSuccess) { entityUpload = new EntityUpload(); entityUpload.Upload_date = dateNow; entityUpload.Upload_result = "上传成功"; entityUpload.Upload_ip = txtAddress1.Text.Trim() + "." + txtAddress2.Text.Trim() + "." + txtAddress3.Text.Trim() + "." + txtAddress4.Text.Trim(); getData = new GetData(dataAccess.Connection, dataAccess.Transaction); int result = getData.InsertUploadRow(entityUpload); } else { entityUpload = new EntityUpload(); entityUpload.Upload_date = dateNow; entityUpload.Upload_result = "上传失败"; entityUpload.Upload_ip = txtAddress1.Text.Trim() + "." + txtAddress2.Text.Trim() + "." + txtAddress3.Text.Trim() + "." + txtAddress4.Text.Trim(); getData = new GetData(dataAccess.Connection, dataAccess.Transaction); int result = getData.InsertUploadRow(entityUpload); } if (dataAccess != null && dataAccess.Connection != null) { dataAccess.Close(); } } Setbtn(btnSend, btnSetAddress); }