MagnusArias

SBO | Funkcje 2 C#

Nov 16th, 2019
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.SqlClient;
  4. using System.Data.SqlTypes;
  5. using Microsoft.SqlServer.Server;
  6.  
  7. public class Wynik
  8. {
  9.     [Microsoft.SqlServer.Server.SqlProcedure]
  10.     public static void wiersze(String tabela, out SqlInt32 ile)
  11.     {
  12.         try
  13.         {
  14.             ile = 0;
  15.             SqlConnection conn = new SqlConnection("context connection=true");
  16.             conn.Open();
  17.             SqlCommand zap = new SqlCommand("SELECT count(*) FROM " + tabela, conn);
  18.             SqlDataReader zapReader = zap.ExecuteReader();
  19.             if (zapReader.HasRows)
  20.             {
  21.                 zapReader.Read();
  22.                 ile = zapReader.GetInt32(0);
  23.             }
  24.         }
  25.         catch (SqlException)
  26.         {
  27.             ile = -1;
  28.         }
  29.     }
  30.  
  31.     [SqlFunction(DataAccess = DataAccessKind.Read)]
  32.     public static int? wiersze_f(SqlString tabela)
  33.     {
  34.         if (!String.IsNullOrEmpty(tabela.ToString()))
  35.         {
  36.             try
  37.             {
  38.                 int? ile = 0;
  39.                 SqlConnection conn = new SqlConnection("context connection=true");
  40.                 conn.Open();
  41.                 SqlCommand zap = new SqlCommand("SELECT count(*) FROM " + tabela.ToString(), conn);
  42.                 SqlDataReader zapReader = zap.ExecuteReader();
  43.                 if (zapReader.HasRows)
  44.                 {
  45.                     zapReader.Read();
  46.                     ile = zapReader.GetInt32(0);
  47.                 }
  48.                 return ile;
  49.             }
  50.             catch (SqlException)
  51.             {
  52.                 return -1;
  53.             }
  54.         }
  55.         else return null;
  56.     }
  57.  
  58.    
  59. }
Add Comment
Please, Sign In to add comment