码迷,mamicode.com
首页 > 数据库 > 详细

DevExpress后置代码中初始化SQL数据源的方法

时间:2015-06-12 18:45:43      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

//初始化SQL数据源的提供者和连接字符串 函数 OK
        public virtual void InitSqlDataSource_ConStr(SqlDataSource sql_ds)
        {            
            ConnectionStringSettings cs_set = ConfigurationManager.ConnectionStrings["connString"];
            sql_ds.ProviderName = cs_set.ProviderName;
            sql_ds.ConnectionString = cs_set.ConnectionString;            
        }
        //初始化SQL数据源的查询插入修改语句 针对一个主关键字段名称
        public virtual void InitSqlDataSource_SelectInsertUpdate(SqlDataSource p_ds, string gridview_ControlID, string tableName, string where_str, string KeyFieldName)
        {
            string x = "";
            //
            ControlParameter ctrl_param = new ControlParameter();
            ctrl_param.ControlID = gridview_ControlID;
            ctrl_param.PropertyName = "KeyFieldName";
            ctrl_param.Name = KeyFieldName;
            //
            string t_fd_name = "";
            x = "select * from " + tableName + " where 1<>1";
            DataTable dt_null = this.GetDataTable(x);
            //
            string fds_str = "";
            string fds_val = "";
            string update_fds_str = "";
            //获取字段分析后字符串
            this.get_SqlDataSource_sql_str(tableName, ref fds_str, ref fds_val, ref update_fds_str);
            //
            string insert_sql = "";
            insert_sql = "insert " + tableName + "(" + fds_str + ") values(" + fds_val + ")";
            string update_sql = "";
            update_sql = "update " + tableName + " set " + update_fds_str + " where " + KeyFieldName + "=@" + KeyFieldName + " ";
            //查询语句
            p_ds.SelectCommandType = SqlDataSourceCommandType.Text;
            p_ds.SelectCommand = "select * from " + tableName + " " + where_str;
            //
            ////删除语句
            p_ds.DeleteCommandType = SqlDataSourceCommandType.Text;
            p_ds.DeleteCommand = "delete from " + tableName + " where " + KeyFieldName + "=@" + KeyFieldName + "";
            p_ds.DeleteParameters.Clear();
            p_ds.DeleteParameters.Add(ctrl_param);
            //
            //插入语句
            p_ds.InsertCommandType = SqlDataSourceCommandType.Text;
            p_ds.InsertCommand = insert_sql;
            p_ds.InsertParameters.Clear();
            p_ds.InsertParameters.Add(ctrl_param);
            //--
            for (int i = 0; i < dt_null.Columns.Count; i++)
            {
                t_fd_name = dt_null.Columns[i].ColumnName;
                if (t_fd_name.ToUpper() != KeyFieldName)
                {
                    p_ds.InsertParameters.Add(new Parameter(t_fd_name));
                }
            }
            //
            //修改语句
            p_ds.UpdateCommandType = SqlDataSourceCommandType.Text;
            p_ds.UpdateCommand = update_sql;
            //--
            p_ds.UpdateParameters.Clear();
            p_ds.UpdateParameters.Add(ctrl_param);
            for (int i = 0; i < dt_null.Columns.Count; i++)
            {
                t_fd_name = dt_null.Columns[i].ColumnName;
                if (t_fd_name.ToUpper() != KeyFieldName)
                {
                    p_ds.UpdateParameters.Add(new Parameter(t_fd_name));
                }
            }
            //==
        }
        public virtual void InitSqlDataSource_Select(SqlDataSource p_ds,string tableName, string where_str)
        {            
            //查询语句
            p_ds.SelectCommandType = SqlDataSourceCommandType.Text;
            p_ds.SelectCommand = "select * from " + tableName + " " + where_str;
            //
            //==
        }
//获取SQL数据源的update和insert语句的需要的组合字符串 函数 OK
        public void get_SqlDataSource_sql_str(string tablename, ref string fds_str, ref string fds_val, ref string update_fds_str)
        {
            string x = "select * from "+tablename+" where 1<>1";
            DataTable dt_null = this.GetDataTable(x);
            string t_fd_name = "";
            fds_str = "";
            fds_val = "";
            //
            update_fds_str = "";
            //
            for (int i = 0; i < dt_null.Columns.Count; i++)
            {
                t_fd_name = dt_null.Columns[i].ColumnName;
                //#region//插入语句字符串处理
                if (fds_str == "")
                {
                    fds_str = t_fd_name;
                    fds_val = "@" + t_fd_name;
                }
                else
                {
                    fds_str += "," + t_fd_name;
                    fds_val += ",@" + t_fd_name;
                }
                //#endregion
                //#region //修改语句字符串处理
                if (update_fds_str == "")
                {
                    update_fds_str = t_fd_name + "=@" + t_fd_name;
                }
                else
                {
                    update_fds_str += "," + t_fd_name + "=@" + t_fd_name;
                }
                //#endregion
            }
        }
        //

调用的方法:

//初始化 需求信息登记的数据源
            this.InitSqlDataSource_ConStr(this.sql_ds_xq);
            string xq_ctrlID = this.dx_gv_xq.ID;   //或xq_ctrlID =this.pagecontrol.ID+"$"+ this.dx_gv_xq.ID;  
            this.InitSqlDataSource_SelectInsertUpdate(this.sql_ds_xq, xq_ctrlID, "TableName", whereStr, "MainFieldName");

DevExpress后置代码中初始化SQL数据源的方法

标签:

原文地址:http://www.cnblogs.com/wordgao/p/4572178.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!