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

//初始化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)