Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Data.Common;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace fun12Test
- {
- static class Database
- {
- private const string provider = "System.Data.SqlClient";
- private const string connectionString = "Data Source=DESKTOP-UF0PRTR;Initial Catalog=funTest;Integrated Security=True";
- private static DbProviderFactory factory = DbProviderFactories.GetFactory(provider);
- private static DbConnection connection = factory.CreateConnection();
- public static bool Connect()
- {
- // DbProviderFactories generates an
- // instance of a DbProviderFactory
- // The DBConnection represents the DB connection
- // Check if a connection was made
- if (connection == null)
- {
- Console.WriteLine("Connection Error");
- Console.ReadLine();
- return false;
- }
- // The DB data needed to open the correct DB
- connection.ConnectionString = connectionString;
- // Open the DB connection
- connection.Open();
- return true;
- }
- public static DbDataReader sql(string sql)
- {
- // Allows you to pass queries to the DB
- DbCommand command = factory.CreateCommand();
- if (command == null)
- {
- Console.WriteLine("Command Error");
- if (!Connect())
- {
- throw new Exception("Something went terrebly wrong");
- }
- }
- // Set the DB connection for commands
- command.Connection = connection;
- // The query you want to issue
- command.CommandText = sql;
- // DbDataReader reads the row results
- // from the query
- DbDataReader dataReader = command.ExecuteReader();
- // Advance to the next results
- return dataReader;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement