Dit artikel is een spiegelartikel van machinevertaling, klik hier om naar het oorspronkelijke artikel te gaan.

Bekijken: 21081|Antwoord: 0

[Bron] [Pro-test] C# voert opgeslagen procedures uit en geeft de primaire GUID-sleutel terug

[Link kopiëren]
Geplaatst op 05-01-2016 15:09:07 | | |
  1. public class DBHelper
  2.     {
  3.         private static readonly string ConnStr = ConfigurationManager.AppSettings["Test"];
  4.         /// <summary>
  5.         /// 执行sql语句
  6.         /// </summary>
  7.         /// <param name="strSql"></param>
  8.         /// <returns>返回影响行数</returns>
  9.         public int ExecuteNonQuery(string strSql)
  10.         {
  11.             int result = 0;
  12.             using (SqlConnection con = new SqlConnection(ConnStr))
  13.             {
  14.                 if (con.State == ConnectionState.Closed)
  15.                     con.Open();
  16.                 using (SqlCommand cmd = new SqlCommand(strSql, con))
  17.                 {
  18.                     result = cmd.ExecuteNonQuery();
  19.                 }
  20.                 con.Close();

  21.                 return result;
  22.             }
  23.         }
  24.         /// <summary>
  25.         /// 执行存储过程
  26.         /// </summary>
  27.         /// <param name="strProcedure"></param>
  28.         /// <param name="param"></param>
  29.         /// <returns>返回影响行数</returns>
  30.         public int ExecuteNonQuery(string strProcedure, SqlParameter[] param)
  31.         {
  32.             int result = 0;
  33.             using (SqlConnection con = new SqlConnection(ConnStr))
  34.             {
  35.                 if (con.State == ConnectionState.Closed)
  36.                     con.Open();
  37.                 using (SqlCommand cmd = new SqlCommand())
  38.                 {
  39.                     cmd.CommandType = CommandType.StoredProcedure;
  40.                     cmd.CommandText = strProcedure;
  41.                     cmd.Connection = con;

  42.                     foreach (var item in param)
  43.                     {
  44.                         cmd.Parameters.Add(item);
  45.                     }

  46.                     result = cmd.ExecuteNonQuery();
  47.                 }

  48.                 con.Close();

  49.                 return result;
  50.             }
  51.         }
  52.         /// <summary>
  53.         /// 查询
  54.         /// </summary>
  55.         /// <param name="strSql"></param>
  56.         /// <returns>返回DataSet</returns>
  57.         public DataSet ExecuteDataSet(string strSql)
  58.         {
  59.             using (DataSet ds = new DataSet())
  60.             {
  61.                 using (SqlConnection con = new SqlConnection(ConnStr))
  62.                 {
  63.                     if (con.State == ConnectionState.Closed)
  64.                         con.Open();
  65.                     using (SqlCommand cmd = new SqlCommand(strSql, con))
  66.                     {
  67.                         using (SqlDataAdapter sda = new SqlDataAdapter())
  68.                         {
  69.                             sda.SelectCommand = cmd;
  70.                             sda.Fill(ds);
  71.                         }
  72.                     }
  73.                     con.Close();
  74.                 }

  75.                 return ds;
  76.             }
  77.         }

  78.         public DataSet ExecuteDataSet(string strProcedure, SqlParameter[] param)
  79.         {
  80.             using (DataSet ds = new DataSet())
  81.             {
  82.                 using (SqlConnection con = new SqlConnection(ConnStr))
  83.                 {
  84.                     if (con.State == ConnectionState.Closed)
  85.                         con.Open();
  86.                     using (SqlCommand cmd = new SqlCommand())
  87.                     {
  88.                         cmd.CommandType = CommandType.StoredProcedure;
  89.                         cmd.CommandText = strProcedure;
  90.                         cmd.Connection = con;

  91.                         foreach (var item in param)
  92.                         {
  93.                             cmd.Parameters.Add(item);
  94.                         }
  95.                         using (SqlDataAdapter sda = new SqlDataAdapter())
  96.                         {
  97.                             sda.SelectCommand = cmd;
  98.                             sda.Fill(ds);
  99.                         }
  100.                     }
  101.                     con.Close();
  102.                 }
  103.                 return ds;
  104.             }
  105.         }

  106.         public int ExecuteNonQueryReturnRecordID(string strProcedure, SqlParameter[] param)
  107.         {
  108.             int result = 0;
  109.             using (SqlConnection con = new SqlConnection(ConnStr))
  110.             {
  111.                 if (con.State == ConnectionState.Closed)
  112.                     con.Open();
  113.                 using (SqlCommand cmd = new SqlCommand())
  114.                 {
  115.                     cmd.CommandType = CommandType.StoredProcedure;
  116.                     cmd.CommandText = strProcedure;
  117.                     cmd.Connection = con;

  118.                     foreach (var item in param)
  119.                     {
  120.                         cmd.Parameters.Add(item);
  121.                     }

  122.                     cmd.ExecuteNonQuery();

  123.                     result = int.Parse(cmd.Parameters["ID"].Value.ToString());
  124.                 }

  125.                 con.Close();

  126.                 return result;
  127.             }
  128.         }

  129.         public SqlParameterCollection ExecuteNonQueryReturns(string strProcedure, SqlParameter[] param)
  130.         {
  131.             SqlParameterCollection spc = null;
  132.             using (SqlConnection con = new SqlConnection(ConnStr))
  133.             {
  134.                 if (con.State == ConnectionState.Closed)
  135.                     con.Open();
  136.                 using (SqlCommand cmd = new SqlCommand())
  137.                 {
  138.                     cmd.CommandType = CommandType.StoredProcedure;
  139.                     cmd.CommandText = strProcedure;
  140.                     cmd.Connection = con;

  141.                     foreach (var item in param)
  142.                     {
  143.                         cmd.Parameters.Add(item);
  144.                     }

  145.                     cmd.ExecuteNonQuery();

  146.                     spc = cmd.Parameters;
  147.                 }
  148.                 con.Close();

  149.                 return spc;
  150.             }
  151.         }
  152.     }
Code kopiëren



Allereerst is de methode van DBHelp gekoppeld
De opgeslagen procedure is als volgt:

  1. ALTER PROCEDURE [dbo].[pAddsp_UserInfoByEmail]
  2.         @ID VARCHAR(50),
  3.         @PASSLOGIN VARCHAR(32),
  4.         @GUID UNIQUEIDENTIFIER OUTPUT
  5. AS
  6. BEGIN
  7.         SELECT @GUID=NEWID()
  8.         INSERT INTO sp_UserInfo([GUID],[PasswordLogin],[Email],[IsEmailValidate],[CreateDate],[Statas]) VALUES (@GUID,@PASSLOGIN,@ID,1,GETDATE(),1)
  9. END
  10. GO
Code kopiëren


Code uitgevoerd:

  1. public class pAddsp_UserInfoByEmail:DBHelper
  2.     {
  3.         private string Email;
  4.         private string PassLogin;
  5.         public pAddsp_UserInfoByEmail(string mail, string passlogin)
  6.         {
  7.             this.Email = mail;
  8.             this.PassLogin = passlogin;
  9.         }
  10.         public string ExecutionProcedure()
  11.         {
  12.             try
  13.             {
  14.                 var param = new SqlParameter[3];
  15.                 param[0] = new SqlParameter("ID", this.Email);
  16.                 param[1] = new SqlParameter("PASSLOGIN", this.PassLogin);
  17.                 param[2] = new SqlParameter("GUID", SqlDbType.UniqueIdentifier);
  18.                 param[2].Direction = ParameterDirection.Output;
  19.                 var sp=base.ExecuteNonQueryReturns("pAddsp_UserInfoByEmail", param);
  20.                 return sp["GUID"].Value.ToString();
  21.             }
  22.             catch (Exception ex)
  23.             {
  24.                 return null;
  25.             }
  26.         }
Code kopiëren






Vorig:【iOS Development Series Tutorial uitgebracht in de zomer】IOS projectbroncode
Volgend:Vandaag wil ik je graag voorstellen aan een open-source .net obfuscator - ConfuserEx
Disclaimer:
Alle software, programmeermaterialen of artikelen die door Code Farmer Network worden gepubliceerd, zijn uitsluitend bedoeld voor leer- en onderzoeksdoeleinden; De bovenstaande inhoud mag niet worden gebruikt voor commerciële of illegale doeleinden, anders dragen gebruikers alle gevolgen. De informatie op deze site komt van het internet, en auteursrechtconflicten hebben niets met deze site te maken. Je moet bovenstaande inhoud volledig van je computer verwijderen binnen 24 uur na het downloaden. Als je het programma leuk vindt, steun dan de echte software, koop registratie en krijg betere echte diensten. Als er sprake is van een inbreuk, neem dan contact met ons op via e-mail.

Mail To:help@itsvse.com