Advertisement
Guest User

Untitled

a guest
Jul 4th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.47 KB | None | 0 0
  1. using Magazin_ColOovidiu.Other.DataMaps;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace Magazin_ColOovidiu.Other
  11. {
  12.     public class DbConnections
  13.     {
  14.         public static string msConString = String.Format("Server={0};Database={1};Integrated Security={2}",
  15.             Properties.ProjectSettings.Default.MsSqlServer,
  16.             Properties.ProjectSettings.Default.MsSqlDbName,
  17.             Properties.ProjectSettings.Default.MsSecurity);
  18.  
  19.         public static System.Data.DataTable GetDataTable(string query, string tableName) //this is the generic method that gets the table, can be used everywhere
  20.         {
  21.             System.Data.DataTable tbl;
  22.             using (SqlConnection conn = new SqlConnection(msConString))
  23.             {
  24.                 SqlDataAdapter sda = new SqlDataAdapter(query, conn);
  25.                 DataSet ds = new DataSet(tableName);
  26.                 sda.Fill(ds, tableName);
  27.                 tbl = ds.Tables[tableName];
  28.             }
  29.             return tbl;
  30.         }
  31.  
  32.         public static IEnumerable<LoginDataDB> GetDbCredentials() //these are specific, needed to get specific data from db table
  33.         {
  34.             string query = "SELECT * FROM [testDb].[dbo].[Creds]"; //this is the table from the db
  35.             System.Data.DataTable tbl = GetDataTable(query, "Creds"); //must have the same name as the table, in this case Creds
  36.             foreach (DataRow rw in tbl.Rows)
  37.             {
  38.                 string username = rw["username"].ToString(); //select the row name
  39.                 string password = rw["password"].ToString();
  40.                 string cash = rw["cash"].ToString();
  41.                 yield return new LoginDataDB(username, password, cash);
  42.             }
  43.  
  44.  
  45.         }
  46.  
  47.         public static IEnumerable<LoginDataDB> GetDbCredentials2() //used to get only the first row ***
  48.         {
  49.             string query = "SELECT * FROM [testDb].[dbo].[Creds]"; //this is the table from the db
  50.             System.Data.DataTable tbl = GetDataTable(query, "Creds");
  51.             DataRow rw = tbl.Rows[0]; //*** that is indicated here
  52.  
  53.             string username = rw["username"].ToString(); //select the row name
  54.             string password = rw["password"].ToString();
  55.             string cash = rw["cash"].ToString();
  56.             yield return new LoginDataDB(username, password, cash);
  57.         }
  58.     }
  59.  
  60.  
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement