純粹的技術(shù)BLOG

          常用鏈接

          統(tǒng)計(jì)

          最新評(píng)論

          在C#中運(yùn)行SQL的SELECT、INSERT和UPDATE命令及存儲(chǔ)過(guò)程

          1、存儲(chǔ)過(guò)程

          C#的代碼如下:

          /// <summary>
            /// 存儲(chǔ)過(guò)程參數(shù)結(jié)構(gòu)
            /// </summary>
            public struct ProcParam
            {
             public string ParamName;
             public string ParamValue;
             public System.Data.SqlDbType ParamType ;
            }

          /// <summary>
            /// 執(zhí)行一個(gè)返回?cái)?shù)據(jù)集的存儲(chǔ)過(guò)程
            /// </summary>
            /// <param name="strProcName">存儲(chǔ)過(guò)程名稱</param>
            /// <param name="alParamName" >參數(shù)名稱</param>
            /// <param name="alParamValue" >參數(shù)值</param>
            /// <param name="constring">數(shù)據(jù)連接串</param>
            /// <returns></returns>
            public static System.Data.SqlClient.SqlDataReader  ExecProcedure(string strProcName,System.Collections.ArrayList alParamName,System.Collections.ArrayList alParamValue,string constring)
            {
             
             System.Data.SqlClient.SqlConnection cn=new System.Data.SqlClient.SqlConnection (constring);
             cn.Open ();
             System.Data.SqlClient.SqlCommand cmd=new System.Data.SqlClient. SqlCommand ();
             cmd.Connection =cn;
             cmd.CommandType =System.Data.CommandType.StoredProcedure ;
             cmd.CommandText =strProcName;
             for(int i=0;i<alParamName.Count ;i++)
             {
              System.Data.SqlClient.SqlParameter  param=new System.Data.SqlClient.SqlParameter (alParamName[i].ToString (),alParamValue[i].ToString ());
              cmd.Parameters.Add (param);
             }
             System.Data.SqlClient.SqlDataReader dr;
             dr=cmd.ExecuteReader ();
             return dr;
            }

          上述代碼可以執(zhí)行任意參數(shù)的存儲(chǔ)過(guò)程(返回一個(gè)數(shù)據(jù)集)

          2、直接SQL命令

            /// <summary>
            /// 執(zhí)行一個(gè)返回字符串的SQL SCRIPT
            /// </summary>
            /// <param name="constring" >連接串</param>
            /// <param name="SQL_COMMAND" >SQL命令</param>
            /// <returns>返回?cái)?shù)據(jù)集</returns>
            public static string  GetDataString(string SQL_COMMAND,string constring)
            { 
             System.Data.SqlClient.SqlConnection cn=new System.Data.SqlClient.SqlConnection (constring);
             cn.Open ();
             System.Data.SqlClient.SqlCommand cmd=new System.Data.SqlClient. SqlCommand ();
             cmd.Connection =cn;
             cmd.CommandType =System.Data.CommandType.Text  ;
             cmd.CommandText =SQL_COMMAND;
             string str="";
             try
             {
              str=cmd.ExecuteScalar().ToString ();
             }
             catch(System.Data.SqlClient.SqlException e)

          {
              System.Diagnostics.Debug.WriteLine (e.Message );
             }
             catch(Exception ex)
             {
              System.Diagnostics.Debug.WriteLine (ex.Message );
             }
             return str;
            }

          /// <summary>
            /// 執(zhí)行一個(gè)無(wú)返回的SQL
            /// </summary>
            /// <param name="SQL_COMMAND"></param>
            /// <param name="constring"></param>
            /// <returns></returns>
            public static void  Executesql(string SQL_COMMAND,string constring)
            { 
             System.Data.SqlClient.SqlConnection cn=new System.Data.SqlClient.SqlConnection (constring);
             cn.Open ();
             System.Data.SqlClient.SqlCommand cmd=new System.Data.SqlClient. SqlCommand ();
             cmd.Connection =cn;
             cmd.CommandType =System.Data.CommandType.Text  ;
             cmd.CommandText =SQL_COMMAND;
             try
             {
              cmd.ExecuteNonQuery ();
             }
             catch(System.Data.SqlClient.SqlException e)
             {
              System.Diagnostics.Debug.WriteLine (e.Message );
             }
             catch(Exception ex)
             {
              System.Diagnostics.Debug.WriteLine (ex.Message );
             }
            }

          posted on 2007-04-03 11:38 純粹 閱讀(4542) 評(píng)論(1)  編輯  收藏

          評(píng)論

          # re: 在C#中運(yùn)行SQL的SELECT、INSERT和UPDATE命令及存儲(chǔ)過(guò)程 2007-08-29 17:53 dreamstone

          只用過(guò)vb.net,還沒用過(guò)c#  回復(fù)  更多評(píng)論   


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 平原县| 新绛县| 修文县| 永宁县| 玉环县| 和田市| 克什克腾旗| 方正县| 湖南省| 红桥区| 广水市| 防城港市| 黔东| 金昌市| 鄂托克前旗| 黄陵县| 舞钢市| 灵川县| 龙州县| 仲巴县| 囊谦县| 灵武市| 万安县| 新巴尔虎右旗| 阳江市| 阳谷县| 基隆市| 梅州市| 灵丘县| 汶川县| 西盟| 抚宁县| 巫溪县| 芦溪县| 商水县| 易门县| 红原县| 成都市| 伊川县| 岳西县| 衢州市|