来自 电脑系统 2019-09-21 04:06 的文章
当前位置: 金沙澳门官网网址 > 电脑系统 > 正文

存储过程的创建及在项目中的调用方法总结,p

在代码进程中中,将代码进程中比较好的代码段珍藏起来,如下的代码是关于python群集使用轨范的代码,希望能对大伙有用。

  这里总括一下ASP.Net程序中寄存进程的行使方式,以及它怎样与ADO.NET中的SqlDataAdapter对象,DataSet对象等组合使用.

# sets are unordered collections of unique hashable elements# Python23 tested vegaseat 09mar2005# Python v2.4 has sets built inimport setsprint "List the functions within module 'sets':"for funk in dir:print funk# create an empty setset1 = set# now load the setfor k in range:set1.addprint "nLoaded a set with 0 to 9:"print set1set1.addprint "Tried to add another 7, but it was already there:"print set1# make a list of fruits as you put them into a basketbasket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']print "nThe original list of fruits:"print basket# create a set from the list, removes the duplicatesfruits = sets.Setprint "nThe set is unique, but the order has changed:"print fruits# let's get rid of some duplicate wordsstr1 = "Senator Strom Thurmond dressed as as Tarzan"print "nOriginal string:"print str1print "A list of the words in the string:"wrdList1 = str1.split()print wrdList1# now create a set of unique wordsstrSet = sets.Setprint "The set of the words in the string:"print strSetprint "Convert set back to string (order has changed!):"print " ".joinprint# comparing two sets, bear with me ...colorSet1 = sets.Set(['red','green','blue','black','orange','white'])colorSet2 = sets.Set(['black','maroon','grey','blue'])print "colorSet1 =", colorSet1print "colorSet2 =", colorSet2# same as (colorSet1 - colorSet2)colorSet3 = colorSet1.difference(colorSet2)print "nThese are the colors in colorSet1 that are not in colorSet2:"print colorSet3# same as (colorSet1 | colorSet2)colorSet4 = colorSet1.union(colorSet2)print "nThese are the colors appearing in both sets:"print colorSet4# same as (colorSet1 ^ colorSet2)colorSet5 = colorSet1.symmetric_difference(colorSet2)print "nThese are the colors in colorSet1 or in colorSet2, but not both:"print colorSet5# same as (colorSet1 & colorSet2)colorSet6 = colorSet1.intersection(colorSet2)print "nThese are the colors common to colorSet1 and colorSet2:"print colorSet6

  一,不带参数查询的积存进程:

  

  1,创造存款和储蓄进程语句:

Create proc [dbo].[sp_SelectUsers]
    as
    begin
    select * from tb_Users;
    end

 

  2,ASP.Net中对存款和储蓄进度的调用代码:

 public partial class SP_Select : System.Web.UI.Page
      {
          DBClass dbc = new DBClass();
          protected void Page_Load(object sender, EventArgs e)
          {
              if (!IsPostBack)
              {
                  GridView1BindData();
              }
          }
          public void GridView1BindData()
          {
              SqlCommand cmd = dbc.GetCommandProc("sp_SelectUsers");
              DataSet ds = dbc.GetDS(cmd);
              GridView1.DataSource = ds;
              GridView1.DataBind();
          }
      }

金沙澳门官网网址 ,  注:DBClass公共类的代码为:

  

金沙澳门官网网址 1金沙澳门官网网址 2View Code

public class DBClass
    {
        public DBClass()
        { 
            //todo:構造函數.
        }
        #region 創建與數據庫SP的連接
        /// <summary>
        /// 創建與數據庫SP的連接
        /// </summary>
        /// <returns></returns>
        public SqlConnection GetConnection()
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SP"].ConnectionString);
            return con;
        }
        #endregion

        #region 執行存儲過程,返回SqlCommand對象
        /// <summary>
        /// 執行存儲過程,返回SqlCommand對象
        /// </summary>
        /// <param name="strProcName"></param>
        /// <returns></returns>
        public SqlCommand GetCommandProc(string strProcName)
        {
            //DBClass dbc = new DBClass();
            SqlConnection con = GetConnection();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = strProcName;
            return cmd;
        }
        #endregion

        #region 執行执行SQL语句命令的SqlCommand对象,用于更新數據庫
        /// <summary>
        /// 執行执行SQL语句命令的SqlCommand对象,用于更新數據庫
        /// </summary>
        /// <param name="cmd"></param>
        public void ExecNonQuery(SqlCommand cmd)
        {
            try
            {
                if (cmd.Connection.State != ConnectionState.Open)
                {
                    cmd.Connection.Open();
                }
                cmd.ExecuteNonQuery();
                //StoredProcedure(); //刷新數據,加這個會報錯.
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
            finally
            {
                if (cmd.Connection.State == ConnectionState.Open)
                {
                    cmd.Connection.Close();
                }
            }
        }

        private void StoredProcedure()
        {
            throw new NotImplementedException();
        }
        #endregion

        #region 執行SQL語句的SqlCommand對象,返回DataSet
        /// <summary>
        /// 執行SQL語句的SqlCommand對象,返回DataSet
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public DataSet GetDS(SqlCommand cmd)
        {
            try
            {
                if (cmd.Connection.State != ConnectionState.Open)
                {
                    cmd.Connection.Open();
                }
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);

            }
            finally
            {
                if (cmd.Connection.State == ConnectionState.Open)
                {
                    cmd.Connection.Close();

                }
            }
        }
        #endregion
    }

  二,带参数查询的存款和储蓄进度:

    1,创设存款和储蓄进度代码:

Create proc [dbo].[sp_SelectParUsers]
@Id int
as
begin
select * from tb_Users where UserId=@Id;
end

    2,项目中存放进程的调用代码:

金沙澳门官网网址 3金沙澳门官网网址 4View Code

public partial class SP_SelectWithPar : System.Web.UI.Page
    {
        DBClass dbc = new DBClass();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GridView1BindData(2);
            }
        }
        public void GridView1BindData(int Id)
        {
            SqlCommand cmd = dbc.GetCommandProc("sp_SelectParUsers");
            cmd.Parameters.AddWithValue("@Id",Id);
            DataSet ds = dbc.GetDS(cmd);
            GridView1.DataSource = ds.Tables[0].DefaultView;
            GridView1.DataBind();

        }
    }

  三,新添的存款和储蓄进度:

本文由金沙澳门官网网址发布于电脑系统,转载请注明出处:存储过程的创建及在项目中的调用方法总结,p

关键词: