示例#1
0
        public ActionResult Index()
        {
            DataTable      dt      = cm.HfindAll();
            List <hSlider> hSilder = DataToList <hSlider> .ConvertToList(dt);

            return(View(hSilder));
        }
示例#2
0
        public void CreateListTest()
        {
            DataCreator dataCreate = new DataCreator();
            var         lines      = new List <string> {
                "test1 10.10.2010 500 ", "test1 11.10.2010 600 ", "test2 12.10.2010 700 "
            };

            dataCreate.LinesIntoData(lines);

            DataToList dataList = new DataToList();
            var        result   = dataList.CreateList(dataCreate.AllData);

            Assert.AreEqual(result,
                            new List <string> {
                "test1 10.10.2010 500 ", "test1 11.10.2010 600 ", "test2 12.10.2010 700 "
            });
        }
示例#3
0
    public static object GetData(StageParams config)
    {
        if (settings == null)
        {
            settings = Util.GetByName(Engine.GetConfig(), "mysql");
        }

        using (MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection())
        {
            conn.ConnectionString = settings.Attributes["connectionString"].Value;
            conn.Open();

            using (MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand())
            {
                cmd.Connection = conn;

                object result = null;

                /*
                 *  mysql map details
                 *  [code/storeprocedure]:[table/scalar/nonquery]:[text/procedure name]
                 */

                string[] resultMap = config.map.Split(new char[] { ':' }, 3);

                string ct = resultMap[0]; // call type
                string rt = resultMap[1]; // return type
                string mp = resultMap[2]; // mapping ( sql text or store procedure name )

                if (!Validate(ct, validCodeTypes))
                {
                    throw new Exception("Mysql bad call type: " + ct);
                }
                if (!Validate(rt, validResultTypes))
                {
                    throw new Exception("Mysql bad result type: " + rt);
                }

                // command text
                cmd.CommandText = mp;

                // command type
                if (ct == "code")
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Prepare();
                }
                else
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                }

                List <string> output_params = new List <string>();

                // named parameters
                for (int i = 0; i < config.stage.ChildNodes.Count; ++i)
                {
                    XmlNode paramNode = config.stage.ChildNodes[i];
                    if (!Util.GetAttr(paramNode, "inplace", false))
                    {
                        if (Util.GetAttr(paramNode, "output", false))
                        {
                            cmd.Parameters.Add(paramNode.Name, Convert(paramNode.Attributes["type"].Value));
                            cmd.Parameters[paramNode.Name].Direction = ParameterDirection.Output;
                            output_params.Add(paramNode.Name);
                        }
                        else
                        {
                            Add(cmd, "@" + paramNode.Name, config.allParams[i]);
                        }
                    }
                }

                if (rt == "table")
                {
                    XmlDocument doc = new XmlDocument();
                    doc.AppendChild(doc.CreateNode(XmlNodeType.Element, "Root", ""));

                    MySqlDataReader reader = cmd.ExecuteReader();
                    DataToXML.Write(doc, reader);
                    reader.Close();

                    if (cmd.CommandText.StartsWith("select SQL_CALC_FOUND_ROWS"))
                    {
                        cmd.CommandText = "SELECT FOUND_ROWS()";
                        cmd.Parameters.Clear();
                        reader = cmd.ExecuteReader();
                        reader.Read();
                        doc.FirstChild.Attributes.Append(doc.CreateAttribute("found_rows")).Value = reader.GetInt32(0).ToString();
                        reader.Close();
                    }

                    result = doc;
                }
                else if (rt == "list")
                {
                    MySqlDataReader reader = cmd.ExecuteReader();
                    MemoryStream    output = new MemoryStream();
                    DataToList.Write(output, reader);
                    reader.Close();

                    result = output;
                }
                else if (rt == "scalar")
                {
                    XmlDocument doc = new XmlDocument();
                    doc.AppendChild(doc.CreateNode(XmlNodeType.Element, "Root", ""));

                    object callresult = cmd.ExecuteScalar();
                    doc.FirstChild.AppendChild(doc.CreateNode(XmlNodeType.Text, "", ""));
                    doc.FirstChild.FirstChild.Value = callresult.ToString();

                    result = doc;
                }
                else if (rt == "nonquery")
                {
                    XmlDocument doc = new XmlDocument();
                    doc.AppendChild(doc.CreateNode(XmlNodeType.Element, "Root", ""));

                    int affected = cmd.ExecuteNonQuery();
                    doc.FirstChild.Attributes.Append(doc.CreateAttribute("affected")).Value = affected.ToString();

                    result = doc;
                }

                foreach (string param in output_params)
                {
                    XmlDocument doc = Util.Validate <XmlDocument>(result, "Output params for this request not supported");
                    doc.FirstChild.Attributes.Append(
                        doc.CreateAttribute(param)).Value = cmd.Parameters[param].Value.ToString();
                }

                return(result);
            }
        }
    }