This article is a mirror article of machine translation, please click here to jump to the original article.

View: 21081|Reply: 0

[Source] [Pro-test] C# executes stored procedures and returns the GUID primary key

[Copy link]
Posted on 1/5/2016 3:09:07 PM | | |
  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.     }
Copy code



First of all, the method of DBHelp is attached
The stored procedure is as follows:

  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
Copy code


Code executed:

  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.         }
Copy code






Previous:【iOS Development Series Tutorial Released in the Summer】IOS project source code
Next:Today I would like to introduce you to an open-source .net obfuscator - ConfuserEx
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com