DevExpress後置代碼中初始化SQL資料來源的方法

來源:互聯網
上載者:User
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");

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.