using System; using System.Collections.Generic; using System.Configuration; using System.Data.SqlClient; using System.Data; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using MySql.Data.MySqlClient; namespace Txgy.EWS.Client.PageModule.Services { public class DownloadWavedata { public int Download(string eventTime, string savePath, string saveName, string tableName) { string connStr = ConfigurationManager.ConnectionStrings["TencetnMySQL"].ConnectionString; MySqlConnection RemoteConn = new MySqlConnection(connStr); string courseSql = @"select WaveData from " + tableName + " where EventTime=@eventTime"; MySqlCommand Comm = new MySqlCommand(courseSql, RemoteConn); MySqlDataReader DataReader = null; try { if (RemoteConn.State != ConnectionState.Open) { RemoteConn.Open(); } Comm.Parameters.AddWithValue("@eventTime", eventTime); DataReader = Comm.ExecuteReader(); List wds = new List(); //开始读数据 while (DataReader.Read()) { byte[] blob; blob = (byte[])DataReader["wavedata"]; //Console.WriteLine(blob.Length); if (blob.Length == 0) break; wds.AddRange(blob); } string mseedPath = savePath + saveName; if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } int len = -1; using (FileStream fs = new FileStream(mseedPath, FileMode.OpenOrCreate)) { using (BinaryWriter binWriter = new BinaryWriter(fs)) { binWriter.Write(wds.ToArray()); len = (int)new FileInfo(mseedPath).Length; } } if (len == wds.Count) { return wds.Count; } return -1; } catch (Exception ex) { throw ex; } finally { if (DataReader != null) { DataReader.Dispose(); DataReader = null; } if (Comm != null) { Comm.Dispose(); Comm = null; } if (RemoteConn != null) { RemoteConn.Close(); RemoteConn.Dispose(); RemoteConn = null; } } } } }