熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ASP.NET項目開發指南:房間類型的添加(2)[2]

2022-06-13   來源: .NET編程 

  程序  ST_RCatgAddModuleascxcs

    private void SubmitButton_Click(object sender SystemEventArgs e)

    {

        if(PageIsValid)

        {

            //從文件WebConfig中讀取連接字符串

            string ST_sqldb=

            ConfigurationSettingsAppSettings[ConnectionString];

            //連接ST_GinShopManage數據庫

            SqlConnection ST_Conn= new SqlConnection (ST_sqldb)

            ST_ConnOpen ()

            //利用Command對象調用存儲過程

            SqlCommand ST_mycommand=new SqlCommand

                    (ST_InsertRoomCategoryST_Conn)

            //將命令類型轉換為存儲類型

            ST_mycommandCommandType =CommandTypeStoredProcedure ;

            //向存儲過程中添加參數

            ST_mycommandParameters Add(@NameSqlDbTypeVarChar)

            ST_mycommandParameters Add(@AreaSqlDbTypeFloat)

            ST_mycommandParameters Add(@BedNumSqlDbTypeInt)

            ST_mycommandParameters Add(@PriceSqlDbTypeMoney)

            ST_mycommandParameters Add(@AirConditionSqlDbTypeInt)

            ST_mycommandParameters Add(@TVSqlDbTypeInt)

            //給存儲過程的參數賦值

            ST_mycommandParameters [@Name]Value

                =RCatgNameTextBoxTextTrim()

            ST_mycommandParameters [@Area]Value

                =ConvertToDouble(AreaTextBoxTextTrim())

            ST_mycommandParameters [@BedNum]Value

                =intParse(BedNumTextBoxTextTrim())

            ST_mycommandParameters [@Price]Value

                =ConvertToDouble(PriceTextBoxTextTrim())

            ST_mycommandParameters [@AirCondition]Value

                =AirConditionListSelectedIndex;

            ST_mycommandParameters [@TV]Value =TvListSelectedIndex;

            try

            {

                //執行SQL語句

                ST_mycommandExecuteNonQuery()

                ShowMsgText=新房間類型添加成功;

                ShowMsgStyle[color]=green;}

            catch(SqlException error)

            {

                ShowMsgText=添加未成功請稍後再試原因+errorMessage;

                ShowMsgStyle[color]=red;

            }

            //關閉連接

            ST_ConnClose()

        }

    }

  

    private void ReturnButton_Click(object sender SystemEventArgs e)

    {

        ResponseRedirect(PathPrefix+/ST_RCategoryManaspx

    }

  【代碼說明】代碼第~行是向存儲過程中添加參數代碼第行中的StoredProcedure屬性表明要執行的是一個存儲過程而不是SQL語句代碼第~行是為這些參數賦值代碼第行是執行存儲過程這裡與執行SQL語句沒有什麼區別

  注意SqlDbTypeMoney是SQL Server中專門用來表示人民幣價格的數據類型

       返回目錄ASPNET項目開發指南

       編輯推薦

       ASPNET MVC 框架揭秘

       ASPNET開發寶典

       ASP NET開發培訓視頻教程

[]  []  


From:http://tw.wingwit.com/Article/program/net/201311/15980.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.