Habsburg

test

Jul 22nd, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.Windows.Forms;
  9.  
  10. namespace StuDom {
  11.     public class Baza {
  12.  
  13.         private static Baza instance;        
  14.         private string connectionString;    
  15.         private SqlConnection connection;
  16.         public static Baza Instance{
  17.             get {
  18.                 if (instance == null) {
  19.                     instance = new Baza();
  20.                 } return instance;
  21.             }
  22.         }
  23.         public string ConnectionString {
  24.             get {
  25.                 return connectionString;
  26.             }
  27.             private set {
  28.                 connectionString = value;
  29.             }
  30.         }
  31.  
  32.         private SqlConnection Connection {
  33.             get {
  34.                 return connection;
  35.             }
  36.  
  37.             set {
  38.                 connection = value;
  39.             }
  40.         }
  41.  
  42.         private Baza(){
  43.             ConnectionString = "DATA.....;Password=PASSWORD;MultipleActiveResultSets=true";
  44.             Connection = new SqlConnection(ConnectionString);
  45.             Connection.Open();
  46.         }  
  47.         ~Baza(){
  48.             try {
  49.                 Connection.Close();
  50.                 Connection = null;
  51.             }
  52.             catch {
  53.             }
  54.         }
  55.         public int IzvrsiUpit(string sqlUpit) {
  56.             try {
  57.                 SqlCommand command = new SqlCommand(sqlUpit, Connection);
  58.                 return command.ExecuteNonQuery();
  59.             }
  60.             catch (Exception e) {
  61.                 MessageBox.Show(e.Message);
  62.                 return -1;
  63.             }
  64.         }
  65.  
  66.         public int IzvrsiParametarskiUpit(SqlCommand sqlNaredba) {
  67.             sqlNaredba.Connection = Connection;
  68.             return sqlNaredba.ExecuteNonQuery();
  69.         }
  70.  
  71.         public object DohvatiVrijednost(string sqlUpit) {
  72.             SqlCommand command = new SqlCommand(sqlUpit, Connection);
  73.             return command.ExecuteScalar();
  74.         }
  75.  
  76.         // metoda za spajanje na bazu
  77.         static public SqlConnection SpojiNaBazu() {
  78.             string connetionString = null;
  79.             SqlConnection conection2; connetionString = "DATA.....;Password=PASSWORD;MultipleActiveResultSets=true";
  80.             conection2 = new SqlConnection(connetionString);
  81.             try {
  82.                 conection2.Open();
  83.             }
  84.             catch (Exception ex) {
  85.                 MessageBox.Show("Can not open connection ! " + ex.Message);
  86.             }
  87.             return conection2;
  88.         }
  89.  
  90.         public SqlDataReader DohvatiPodatke(string upit) {
  91.             //SqlConnection connection = SpojiNaBazu();
  92.             SqlCommand command = new SqlCommand();
  93.             command.CommandText = upit;
  94.             command.CommandType = CommandType.Text;
  95.             command.Connection = Connection;
  96.             SqlDataReader podaci = command.ExecuteReader();
  97.  
  98.             //connection.Close();
  99.             return podaci;
  100.         }
  101.  
  102.        
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment