Acest articol este un articol oglindă al traducerii automate, vă rugăm să faceți clic aici pentru a sări la articolul original.

Vedere: 21081|Răspunde: 0

[Sursă] [Pro-test] C# execută proceduri stocate și returnează cheia primară GUID

[Copiază linkul]
Postat pe 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.     }
Cod de copiere



În primul rând, metoda DBHelp este atașată
Procedura stocată este următoarea:

  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
Cod de copiere


Cod executat:

  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.         }
Cod de copiere






Precedent:【Tutorial iOS Development Series Lansat în vară】Cod sursă al proiectului iOS
Următor:Astăzi aș dori să vă prezint un obfuscator .net open-source - ConfuserEx
Disclaimer:
Tot software-ul, materialele de programare sau articolele publicate de Code Farmer Network sunt destinate exclusiv scopurilor de învățare și cercetare; Conținutul de mai sus nu va fi folosit în scopuri comerciale sau ilegale, altfel utilizatorii vor suporta toate consecințele. Informațiile de pe acest site provin de pe Internet, iar disputele privind drepturile de autor nu au legătură cu acest site. Trebuie să ștergi complet conținutul de mai sus de pe calculatorul tău în termen de 24 de ore de la descărcare. Dacă îți place programul, te rugăm să susții software-ul autentic, să cumperi înregistrarea și să primești servicii autentice mai bune. Dacă există vreo încălcare, vă rugăm să ne contactați prin e-mail.

Mail To:help@itsvse.com