Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Data;
- using System.Data.SqlClient;
- using System.Windows.Forms;
- namespace StuDom {
- public class Baza {
- private static Baza instance;
- private string connectionString;
- private SqlConnection connection;
- public static Baza Instance{
- get {
- if (instance == null) {
- instance = new Baza();
- } return instance;
- }
- }
- public string ConnectionString {
- get {
- return connectionString;
- }
- private set {
- connectionString = value;
- }
- }
- private SqlConnection Connection {
- get {
- return connection;
- }
- set {
- connection = value;
- }
- }
- private Baza(){
- ConnectionString = "DATA.....;Password=PASSWORD;MultipleActiveResultSets=true";
- Connection = new SqlConnection(ConnectionString);
- Connection.Open();
- }
- ~Baza(){
- try {
- Connection.Close();
- Connection = null;
- }
- catch {
- }
- }
- public int IzvrsiUpit(string sqlUpit) {
- try {
- SqlCommand command = new SqlCommand(sqlUpit, Connection);
- return command.ExecuteNonQuery();
- }
- catch (Exception e) {
- MessageBox.Show(e.Message);
- return -1;
- }
- }
- public int IzvrsiParametarskiUpit(SqlCommand sqlNaredba) {
- sqlNaredba.Connection = Connection;
- return sqlNaredba.ExecuteNonQuery();
- }
- public object DohvatiVrijednost(string sqlUpit) {
- SqlCommand command = new SqlCommand(sqlUpit, Connection);
- return command.ExecuteScalar();
- }
- // metoda za spajanje na bazu
- static public SqlConnection SpojiNaBazu() {
- string connetionString = null;
- SqlConnection conection2; connetionString = "DATA.....;Password=PASSWORD;MultipleActiveResultSets=true";
- conection2 = new SqlConnection(connetionString);
- try {
- conection2.Open();
- }
- catch (Exception ex) {
- MessageBox.Show("Can not open connection ! " + ex.Message);
- }
- return conection2;
- }
- public SqlDataReader DohvatiPodatke(string upit) {
- //SqlConnection connection = SpojiNaBazu();
- SqlCommand command = new SqlCommand();
- command.CommandText = upit;
- command.CommandType = CommandType.Text;
- command.Connection = Connection;
- SqlDataReader podaci = command.ExecuteReader();
- //connection.Close();
- return podaci;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment