This C# Tutorial is going to cover a lot in a short video. I’ll set up the newest SQL Server and Visual Studio 2017. I’ll then explore Creating Tables, Using SSMS, App.config, Connecting to SQL Server with C# and a whole lot more.
For best results take notes on the cheat sheet provided below as you watch and leave any questions you have.
[googleplusone]
If you value videos like this consider donating a $1 on Patreon.
Code & Cheat Sheet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
----- App.config ----- <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="provider" value="System.Data.SqlClient" /> <add key="connectionString" value="Data Source=DEREKBANAS936E;Initial Catalog=StoreDB;Integrated Security=True;Pooling=False"/> </appSettings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> </startup> </configuration> ----- Program.cs ----- using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.Common; using System.Configuration; namespace DBTest { class Program { static void Main(string[] args) { // App.config stores configuration data // System.Data.SqlClient provides classes // for accessing a SQL Server DB // connectionString defines the DB name, and // other parameters for connecting to the DB // Configurationmanager provides access to // config data in App.config string provider = ConfigurationManager.AppSettings["provider"]; string connectionString = ConfigurationManager.AppSettings["connectionString"]; // DbProviderFactories generates an // instance of a DbProviderFactory DbProviderFactory factory = DbProviderFactories.GetFactory(provider); // The DBConnection represents the DB connection using (DbConnection connection = factory.CreateConnection()) { // Check if a connection was made if(connection == null) { Console.WriteLine("Connection Error"); Console.ReadLine(); return; } // The DB data needed to open the correct DB connection.ConnectionString = connectionString; // Open the DB connection connection.Open(); // Allows you to pass queries to the DB DbCommand command = factory.CreateCommand(); if(command == null) { Console.WriteLine("Command Error"); Console.ReadLine(); return; } // Set the DB connection for commands command.Connection = connection; // The query you want to issue command.CommandText = "Select * From Products"; // DbDataReader reads the row results // from the query using (DbDataReader dataReader = command.ExecuteReader()) { // Advance to the next results while (dataReader.Read()) { // Output results using row names Console.WriteLine($"{dataReader["ProdId"]} " + $"{dataReader["Product"]}"); } } Console.ReadLine(); } } } } |
Leave a Reply