ASP.NET技巧:預存程序的分析

來源:互聯網
上載者:User
asp.net|預存程序|技巧 1 // 對預存程序的分析:執行個體
 2         // string G_name ,string G_password為傳遞給此預存程序的參數,string Loging表示方法名和類型
 3    
 4          public   string  Login( string  G_Name , string  G_Passord)
 5          {
 6
 7             SqlConnection Conn  =   new  SqlConnection(ConfigurationSettings.AppeSettings[ " ConnectionString " ]);
 8          // 此為串連語句
 9          /**/ /*
10          ConfigurationSettings.AppeSettings["ConnectionString"]
11          表示從Configurantion載入了一條串連語句
12          Config裡的語句為
13          <appSettings>
14          <add key="ConnectionString" value="server=Localhost;uid=sa;pwd=sa; dataBase= Global" />
15         </appSettings>
16          <system.web>
17          注意此標是放在<system.web>的上面
18           */ 
19             SqlCommand Comm  =   new  SqlCommand( " Login " ,Conn);
20          // 建立一個SqlCommand的執行個體Comm並把它標記為儲蓄過程名為Login。
21        
22             Comm.CommandType  = CommandType.StoredProcedure;
23          // 將Comm標記為儲蓄過程
24
25
26
27          // 下面為預存程序添加參數
28             SqlParameter parameterG_name  =   new  SqlParameter( " @G_name " ,SqlDbType.NVarChar, 20 );
29          //  建立了一個SqlParameter的儲蓄過程參數執行個體:執行個體名為pranmeterG_name
30          // 並定義了一個名為"@G_name"的參數名,定義類型SqlDbType為NvarChar 位元組數為20;
31        
32             parameterG_name.Value = G_Name;
33          // 為參數執行個體parameterG_name賦值 為這個值是從方法Login中傳遞進來的值
34          // 注意parameterG_name.Value=G_name 中的G_name和語句new SqlParameter("@G_name",SqlDbType.NVarChar,20);
35          // 中的@G_name是不同,@G_name是為一個SqlParameter的儲蓄流程定義的一個參數名
36 
37             Comm.Parameters.Add(prarameterG_name);
38          //  為Comm為添加參數paratemterG_name
39
40
41
42
43             SqlParameter parameterG_password  =   new  SqlParameter( " @G_password " ,SqlDbType.NVarChar, 20 );
44             parameterG_password.Value = G_password;
45             Comm.Parameters.Add(parameterG_password);
46
47             SqlParameter parameterG_Id  =   new  SqlParameter( " @G_Id " ,SqlDbType.Int, 4 );
48             parameterG_Id.Direction  =  ParameterDirection.Output;
49              //  parameterG_Id.dDirection 擷取或者設定一個值,該值指示指示參數是只可
50             // 只可以輸入,只可以輸出,雙向 還是預存程序傳回值參數
51              // ParameterDirection.Output;定義了此參數為輸出參數
52             Comm.Parameters.Add(paramerG_Id);
53            
54              // 開啟串連並執行Command命令
55             Conn.Open();
56             Comm.ExecuteNonQuery();
57             Conn.Close();
58            
59              // 對獲得parameterG_Id.Value的值進行處理
60              int  G_id  = ( int )(parameterG_Id.Value);
61
62              if (G_id == 0 )
63              {
64                  return   null ;
65                  // 返回空
66             }
67              else
68              {
69                 G_id.ToString();
70                  // 將此資訊轉換為等效字串的表現形勢
71             }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.