Αυτό το άρθρο είναι ένα άρθρο καθρέφτη της αυτόματης μετάφρασης, κάντε κλικ εδώ για να μεταβείτε στο αρχικό άρθρο.

Άποψη: 21081|Απάντηση: 0

[Πηγή] [Pro-test] Η C# εκτελεί αποθηκευμένες διαδικασίες και επιστρέφει το πρωτεύον κλειδί GUID

[Αντιγραφή συνδέσμου]
Δημοσιεύτηκε στις 5/1/2016 3: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.     }
Αντιγραφή κώδικα



Πρώτα απ 'όλα, επισυνάπτεται η μέθοδος του DBHelp
Η αποθηκευμένη διαδικασία είναι η εξής:

  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
Αντιγραφή κώδικα


Κώδικας που εκτελέστηκε:

  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.         }
Αντιγραφή κώδικα






Προηγούμενος:【Το σεμινάριο της σειράς ανάπτυξης iOS κυκλοφόρησε το καλοκαίρι】Πηγαίος κώδικας έργου IOS
Επόμενος:Σήμερα θα ήθελα να σας παρουσιάσω έναν συσκοτιστή ανοιχτού κώδικα .net - ConfuserEx
Αποκήρυξη:
Όλο το λογισμικό, το υλικό προγραμματισμού ή τα άρθρα που δημοσιεύονται από το Code Farmer Network προορίζονται μόνο για μαθησιακούς και ερευνητικούς σκοπούς. Το παραπάνω περιεχόμενο δεν θα χρησιμοποιηθεί για εμπορικούς ή παράνομους σκοπούς, άλλως οι χρήστες θα υποστούν όλες τις συνέπειες. Οι πληροφορίες σε αυτόν τον ιστότοπο προέρχονται από το Διαδίκτυο και οι διαφορές πνευματικών δικαιωμάτων δεν έχουν καμία σχέση με αυτόν τον ιστότοπο. Πρέπει να διαγράψετε εντελώς το παραπάνω περιεχόμενο από τον υπολογιστή σας εντός 24 ωρών από τη λήψη. Εάν σας αρέσει το πρόγραμμα, υποστηρίξτε γνήσιο λογισμικό, αγοράστε εγγραφή και λάβετε καλύτερες γνήσιες υπηρεσίες. Εάν υπάρχει οποιαδήποτε παραβίαση, επικοινωνήστε μαζί μας μέσω email.

Mail To:help@itsvse.com