コード例 #1
0
ファイル: SqLiteConnector.cs プロジェクト: NueGy/NgLib
        public SQLiteCommand InitCommand(string sql, Dictionary <string, object> parameters)
        {
            if (conn == null)
            {
                this.Open();
            }
            try
            {
                SQLiteCommand cmd = new SQLiteCommand(this.conn);
                cmd.CommandTimeout = this.DefaultTimeOut;

                if (DataConnectorTools.IsSQLQuery(sql))
                {
                    cmd.CommandType = System.Data.CommandType.Text;
                }
                else
                {
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                }
                cmd.CommandText = sql;
                List <string> paramneed = new List <string>();
                foreach (System.Text.RegularExpressions.Match match in System.Text.RegularExpressions.Regex.Matches(sql.ToLower() + " ", "(\\@\\w+)"))
                {
                    paramneed.Add(match.Groups[1].Value);
                }

                if (parameters != null)
                {
                    foreach (string fieldKey in parameters.Keys)
                    {
                        if (cmd.CommandType == System.Data.CommandType.Text) // envoyer que les params demandés
                        {
                            if (!paramneed.Contains("@" + fieldKey.ToLower()))
                            {
                                continue;                                                // innutile
                            }
                        }
                        object obj = parameters[fieldKey];
                        //if(obj is bool) // convertion de type
                        //{
                        //    int iobj = 0;
                        //    if((bool)obj)iobj=1;
                        //    obj= iobj;
                        //}
                        // if (itemd.IsDynamic && !itemd.IsTrue("notransform") && itemd.NameMinimal.ToLower() != "fluxxml") itemd.value = itemd.Transformation();
                        cmd.Parameters.AddWithValue("@" + fieldKey, obj);
                    }
                }


                return(cmd);
            }
            catch (Exception ex)
            {
                throw new Exception("Initialisation SQL", ex);
            }
        }
コード例 #2
0
ファイル: MsSQLConnector.cs プロジェクト: NueGy/NgLib
        public SqlCommand InitCommand(QueryContext query)
        {
            if (conn == null)
            {
                this.Open();
            }
            try
            {
                SqlCommand cmd = conn.CreateCommand();
                if (transac != null)
                {
                    cmd.Transaction = transac;
                }
                cmd.CommandTimeout = this.DefaultTimeOut;

                if (DataConnectorTools.IsSQLQuery(query.sqlQuery))
                {
                    cmd.CommandType = CommandType.Text;
                }
                else
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                }
                cmd.CommandText = query.sqlQuery;

                if (query.parameters != null)
                {
                    foreach (string fieldKey in query.parameters.Keys)
                    {
                        // if (itemd.IsDynamic && !itemd.IsTrue("notransform") && itemd.NameMinimal.ToLower() != "fluxxml") itemd.value = itemd.Transformation();
                        cmd.Parameters.AddWithValue("@" + fieldKey, query.parameters[fieldKey]);
                    }
                }
                return(cmd);
            }
            catch (Exception ex)
            {
                throw new Exception("Initialisation SQL", ex);
            }
        }