Ejemplo n.º 1
0
        public string BulkCopyToSqlNoLower(DataTable dt, Dictionary <string, string> dicConfig, Dictionary <string, string> dicParr)
        {
            try
            {
                var prDt = new SqlParameter("@dt", SqlDbType.Structured);
                prDt.Value = dt;

                List <SqlParameter> lstParr = new List <SqlParameter>();
                lstParr.Add(prDt);
                if (dicParr != null)
                {
                    foreach (var item in dicParr)
                    {
                        lstParr.Add(new SqlParameter("@" + item.Key, item.Value));
                    }
                    // trường hợp đặc biệt thêm mã hóa đăng nhập
                    // encode: true, passold: "PP01000111917", passnew: "PasswordEncrypt"
                    if (dicConfig.ContainsKey("encode") && Convert.ToBoolean(dicConfig["encode"]))
                    {
                        string passEncrypt = SLCommon.Security.EncryptString(dicConfig["passold"]);
                        lstParr.Add(new SqlParameter("@" + dicConfig["passnew"], passEncrypt));
                    }
                }
                string connect = ConfigurationManager.ConnectionStrings[dicConfig["connstr"]].ConnectionString;
                var    ds      = DynamicSelectDataset(dicConfig["namesql"], lstParr.ToArray(), CommandType.StoredProcedure, connect);

                var data = gn.ConvertDatasetToListJsonNoLower(ds);
                if (data == null)
                {
                    return("{\"result\":\"ERROR\",\"data\":[{\"status\":\"Lỗi khi ConvertDatasetToListJson\"}]}");
                }
                var json      = gn.ConvertListToJson(data);
                var strResult = "{\"result\":\"OK\",\"data\":" + json + "}";
                return(strResult);
            }
            catch (Exception ex)
            {
                return("{\"result\":\"ERROR\",\"data\":[{\"status\":\"" + ex.Message.Replace("\n", "") + "\"}]}");
            }
        }
Ejemplo n.º 2
0
        public string ExecuteSqlDatasetNoLower(Dictionary <string, string> dicConfig, Dictionary <string, string> dicParr)
        {
            SqlConnection  Conn    = null;
            SqlCommand     command = null;
            SqlDataAdapter adapter = null;

            try
            {
                //  SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings[_dicConfig["connstr"]].ConnectionString);
                //   var conStr = Connection != null ? Connection.ConnectionString : "";
                general gn         = new general();
                var     dicApp     = gn.ReadAppseting();
                string  Connection = dicApp[dicConfig["connstr"].ToString().ToLower()];// dicApp["connectsql"];
                var     conStr     = Connection != null ? Connection : "";
                if (conStr == "")
                {
                    return("{\"result\":\"ERROR\",\"data\":[{\"status\":\"Lỗi khi gọi ConnectionString\"}]}");
                }
                Conn    = new SqlConnection(conStr);
                command = new SqlCommand {
                    Connection = Conn
                };
                command.CommandText = dicConfig["namesql"];
                command.Parameters.Clear();
                command.CommandType = CommandType.StoredProcedure;
                if (dicParr != null)
                {
                    List <SqlParameter> lstParr = new List <SqlParameter>();
                    foreach (var item in dicParr)
                    {
                        lstParr.Add(new SqlParameter("@" + item.Key, item.Value));
                    }
                    // trường hợp đặc biệt thêm mã hóa đăng nhập
                    // encode: true, passold: "PP01000111917", passnew: "PasswordEncrypt"
                    if (dicConfig.ContainsKey("encode") && Convert.ToBoolean(dicConfig["encode"]))
                    {
                        string passEncrypt = SLCommon.Security.EncryptString(dicConfig["passold"]);
                        lstParr.Add(new SqlParameter("@" + dicConfig["passnew"], passEncrypt));
                    }
                    command.Parameters.AddRange(lstParr.ToArray());
                }
                if (Conn.State != ConnectionState.Open)
                {
                    Conn.Open();
                }
                adapter = new SqlDataAdapter(command);
                var ds = new DataSet();
                adapter.Fill(ds);

                var data = gn.ConvertDatasetToListJsonNoLower(ds);
                if (data == null)
                {
                    return("{\"result\":\"ERROR\",\"data\":[{\"status\":\"Lỗi khi ConvertDatasetToListJson\"}]}");
                }
                var json      = gn.ConvertListToJson(data);
                var strResult = "{\"result\":\"OK\",\"data\":" + json + "}";
                return(strResult);
            }
            catch (Exception ex)
            {
                return("{\"result\":\"ERROR\",\"data\":[{\"status\":\"" + ex.Message.Replace("\n", "") + "\"}]}");
            }
            finally
            {
                if (command != null)
                {
                    command.Dispose();
                    command = null;
                }
                if (adapter != null)
                {
                    //if(adapter.is)
                    adapter.Dispose();
                    adapter = null;
                }
                if (Conn.State != ConnectionState.Closed)
                {
                    Conn.Close();
                }
            }
        }
Ejemplo n.º 3
0
        public JsonResult ReadFileXml()
        {
            try
            {
                Dictionary <string, string> dicAppSet = null;
                if (dicAppSet == null || dicAppSet.Count == 0)
                {
                    general gn = new general();
                    dicAppSet = gn.ReadAppseting();
                    if (dicAppSet == null || dicAppSet.Count == 0)
                    {
                        Json("{\"result\":\"OK\",\"data\":\"Đọc appsetting lỗi\"}");
                    }
                }

                string fileName = "";
                for (int i = 0; i < Request.Files.Count; i++)
                {
                    HttpPostedFileBase file = Request.Files[i]; //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;


                    //To save file, use SaveAs method
                    string filePath = dicAppSet["path_saveonserver"] + 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]);
                    }

                    XmlDocument doc = new XmlDocument();
                    doc.Load(filePath);

                    var     dsHead = new DataSet();
                    DataSet ds     = new DataSet();
                    ds.ReadXml(new XmlNodeReader(doc));

                    var dt = new DataTable("dt");
                    dt.Columns.Add("Header");
                    dt.Rows.Add(ds.GetXmlSchema());
                    ds.Tables.Add(dt);

                    general gn   = new general();
                    var     data = gn.ConvertDatasetToListJsonNoLower(ds);
                    if (data == null)
                    {
                        return(Json("{\"result\":\"ERROR\",\"data\":\"Lỗi khi ConvertDatasetToListJson\"}"));
                    }
                    var json      = gn.ConvertListToJson(data);
                    var strResult = Json("{\"result\":\"OK\",\"data\":" + json + "}");

                    System.IO.File.Delete(filePath);
                    return(strResult);
                }
                return(Json("{\"result\":\"OK\",\"data\":\"Không nhận được file\"}"));
            }
            catch (Exception ex)
            {
                return(Json("{\"result\":\"ERROR\",\"data\":\"" + ex.Message + "\"}"));
            }
        }