public string BulkCopyToOracle(DataTable dt, Dictionary <string, string> config) { try { string connectOracle = ConfigurationManager.ConnectionStrings[config["connstr"]].ConnectionString; gnOracle gn = new gnOracle(); using (var connection = new OracleConnection(connectOracle)) { connection.Open(); using (var bulkCopy = new OracleBulkCopy(connection, OracleBulkCopyOptions.UseInternalTransaction)) { bulkCopy.DestinationTableName = config.ContainsKey("insertto")? config["insertto"]: dt.TableName; var table = gn.potentialFix(dt); bulkCopy.WriteToServer(table); } } return("{\"result\":\"OK\",\"data\":\"OK\"}"); } catch (Exception ex) { return("{\"result\":\"ERROR\",\"data\":[{\"status\":\"" + ex.Message.Replace("\n", "") + "\"}]}"); } }
public string InportXmlToOrcale() { try { general gn = new general(); string fileName = ""; HttpPostedFileBase file = Request.Files[0]; //Uploaded file //Use the following properties to get file's name, size and MIMEType int fileSize = file.ContentLength; fileName = file.FileName; string mimeType = file.ContentType; System.IO.Stream fileContent = file.InputStream; if (!Directory.Exists(Server.MapPath("~/UploadFile/"))) { Directory.CreateDirectory(Server.MapPath("~/UploadFile/")); } //To save file, use SaveAs method string filePath = Server.MapPath("~/UploadFile/") + fileName; file.SaveAs(filePath); //File will be saved in application root var itemp = this.Request.Form; var parr = new Dictionary <string, string>(); foreach (var key in itemp.AllKeys) { parr.Add(key.ToLower(), itemp[key]); } // CHUYEN SANG DATASET var ds = new DataSet(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(filePath); var xmlReader = new XmlNodeReader(xmlDoc); ds.ReadXml(xmlReader); var ds1 = gn.convertDatasetToLower(ds); if (ds1 == null || ds1.Tables.Count == 0) { return("{\"result\":\"OK\",\"data\":[{\"status\":\"Lỗi không đọc được file xml\"}]}"); } // add them 1 so cot DataColumn tenfile = new DataColumn("tenfile", typeof(System.String)); tenfile.DefaultValue = fileName; DataColumn ngayup = new DataColumn("ngayup", typeof(System.String)); ngayup.DefaultValue = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); DataColumn idrandom = new DataColumn("idrandom", typeof(System.String)); idrandom.DefaultValue = parr["idrandom"]; ds1.Tables[0].Columns.Add(tenfile); ds1.Tables[0].Columns.Add(ngayup); ds1.Tables[0].Columns.Add(idrandom); // check bang neu chua ton tai thi tao bang gnOracle or = new gnOracle(); general gns = new general(); var dicAppSet = gns.ReadAppseting(); var checkTonTai = "SELECT COUNT(1) TONTAI FROM USER_TABLES WHERE UPPER(TABLE_NAME) = UPPER('" + parr["insertto"] + "') "; var count = or.ExcuteReturnDataSet(checkTonTai, null, CommandType.Text, dicAppSet[parr["connstr"].ToLower()]); if (count != null && count.Tables.Count > 0 && count.Tables[0].Rows.Count > 0 && count.Tables[0].Rows[0]["TONTAI"].ToString() == "0") { // TAO COT CHINH var str = "CREATE TABLE " + parr["insertto"] + " ( "; foreach (var col in ds1.Tables[0].Columns) { str += " " + col.ToString().ToUpper() + " VARCHAR2(255),"; } str = str.Substring(0, str.Length - 1) + " ) "; or.ExcuteReturnDataSet(str, null, CommandType.Text, dicAppSet[parr["connstr"].ToLower()]); } // bulk copy to oracle gnOracle gnOr = new gnOracle(); string connectOracle = dicAppSet[parr["connstr"].ToLower()]; var dt2 = gnOr.forMartTable(ds1.Tables[0], parr["insertto"], connectOracle); using (var connection = new OracleConnection(connectOracle)) { connection.Open(); using (var bulkCopy = new OracleBulkCopy(connection, OracleBulkCopyOptions.UseInternalTransaction)) { bulkCopy.DestinationTableName = parr["insertto"]; var table = gnOr.potentialFix(dt2); bulkCopy.WriteToServer(table); } } return("{\"result\":\"OK\",\"data\":[{\"idrandom\":\"" + parr["idrandom"] + "\"}]}"); } catch (Exception ex) { return("{\"result\":\"ERROR\",\"data\":[{\"status\":\"" + ex.Message.Replace("\n", "") + "\"}]}"); } }
public string InportXmlToOrcale() { try { general gn = new general(); string fileName = ""; HttpPostedFileBase file = Request.Files[0]; //Uploaded file //Use the following properties to get file's name, size and MIMEType int fileSize = file.ContentLength; fileName = file.FileName; string mimeType = file.ContentType; System.IO.Stream fileContent = file.InputStream; if (!Directory.Exists(Server.MapPath("~/UploadFile/"))) { Directory.CreateDirectory(Server.MapPath("~/UploadFile/")); } //To save file, use SaveAs method string filePath = Server.MapPath("~/UploadFile/") + fileName; file.SaveAs(filePath); //File will be saved in application root var itemp = this.Request.Form; var parr = new Dictionary <string, string>(); foreach (var key in itemp.AllKeys) { parr.Add(key.ToLower(), itemp[key]); } var insertTo = parr["insertto"]; var connstr = parr["connstr"]; parr.Remove("insertto"); parr.Remove("connstr"); // CHUYEN SANG DATASET var ds = new DataSet(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(filePath); var xmlReader = new XmlNodeReader(xmlDoc); ds.ReadXml(xmlReader); var ds1 = gn.convertDatasetToLower(ds); var dt = new DataTable("dt"); foreach (var val in parr) { dt.Columns.Add(val.Key); } foreach (DataRow row in ds1.Tables[0].Rows) { var dr = dt.NewRow(); foreach (var val in parr) { dr[val.Key] = row[val.Key]; } dt.Rows.Add(dr); } // bulk copy to oracle gnOracle gnOr = new gnOracle(); string connectOracle = ConfigurationManager.ConnectionStrings[connstr].ConnectionString; using (var connection = new OracleConnection(connectOracle)) { connection.Open(); using (var bulkCopy = new OracleBulkCopy(connection, OracleBulkCopyOptions.UseInternalTransaction)) { bulkCopy.DestinationTableName = insertTo; var table = gnOr.potentialFix(dt); bulkCopy.WriteToServer(table); } } return("{\"result\":\"OK\",\"data\":[{\"status\":\"OK\"}]}"); } catch (Exception ex) { return("{\"result\":\"ERROR\",\"data\":[{\"status\":\"" + ex.Message.Replace("\n", "") + "\"}]}"); } }