|
| 1 | +## Prerequisites |
| 2 | + |
| 3 | +This tutorial assumes that you have: |
| 4 | + |
| 5 | +- installed YugaByte DB, created a universe and are able to interact with it using the YSQL shell. If not, please follow these steps in the [ysql guide](../../../api/ysql/). |
| 6 | +- installed Visual Studio |
| 7 | + |
| 8 | +## Writing a HelloWorld C# app |
| 9 | + |
| 10 | +In your Visual Studio create a new Project and choose Console Application as template. Follow the instructions to save the project. |
| 11 | + |
| 12 | +### Install YSQL C# driver |
| 13 | +To install the driver in your Visual Studio project |
| 14 | +<ol> |
| 15 | + <li>Open your Project Solution View.</li> |
| 16 | + <li>Right-click on Packages and click Add Packages.</li> |
| 17 | +  |
| 18 | + <li>Search for Npgsql and click Add Package.</li> |
| 19 | +  |
| 20 | +</ol> |
| 21 | + |
| 22 | +### Copy the contents below to your `Program.cs` file. |
| 23 | + |
| 24 | +```cs |
| 25 | +using System; |
| 26 | +using Npgsql; |
| 27 | + |
| 28 | +namespace YugaByte_CSharp_Demo |
| 29 | +{ |
| 30 | + class Program |
| 31 | + { |
| 32 | + static void Main(string[] args) |
| 33 | + { |
| 34 | + NpgsqlConnection conn = new NpgsqlConnection("host=localhost;port=5433;database=yb_demo;user id=postgres;password="); |
| 35 | + |
| 36 | + try |
| 37 | + { |
| 38 | + conn.Open(); |
| 39 | + |
| 40 | + NpgsqlCommand empCreateCmd = new NpgsqlCommand("CREATE TABLE employee (id int PRIMARY KEY, name varchar, age int, language varchar);", conn); |
| 41 | + empCreateCmd.ExecuteNonQuery(); |
| 42 | + Console.WriteLine("Created table Employee"); |
| 43 | + |
| 44 | + NpgsqlCommand empInsertCmd = new NpgsqlCommand("INSERT INTO employee (id, name, age, language) VALUES (1, 'John', 35, 'CSharp');", conn); |
| 45 | + int numRows = empInsertCmd.ExecuteNonQuery(); |
| 46 | + Console.WriteLine("Inserted data (1, 'John', 35, 'CSharp')"); |
| 47 | + |
| 48 | + NpgsqlCommand empPrepCmd = new NpgsqlCommand("SELECT name, age, language FROM employee WHERE id = @EmployeeId", conn); |
| 49 | + empPrepCmd.Parameters.Add("@EmployeeId", NpgsqlTypes.NpgsqlDbType.Integer); |
| 50 | + |
| 51 | + empPrepCmd.Parameters["@EmployeeId"].Value = 1; |
| 52 | + NpgsqlDataReader reader = empPrepCmd.ExecuteReader(); |
| 53 | + |
| 54 | + Console.WriteLine("Query returned:\nName\tAge\tLanguage"); |
| 55 | + while (reader.Read()) |
| 56 | + { |
| 57 | + Console.WriteLine("{0}\t{1}\t{2}", reader.GetString(0), reader.GetInt32(1), reader.GetString(2)); |
| 58 | + } |
| 59 | + } |
| 60 | + catch (Exception ex) |
| 61 | + { |
| 62 | + Console.WriteLine("Failure: " + ex.Message); |
| 63 | + } |
| 64 | + finally |
| 65 | + { |
| 66 | + if (conn.State != System.Data.ConnectionState.Closed) |
| 67 | + { |
| 68 | + conn.Close(); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +### Running the C# app |
| 77 | +Run the C# app from menu select `Run -> Start Without Debugging` |
| 78 | + |
| 79 | +You should see the following as the output. |
| 80 | + |
| 81 | +``` |
| 82 | +Created table Employee |
| 83 | +Inserted data (1, 'John', 35, 'CSharp') |
| 84 | +Query returned: |
| 85 | +Name Age Language |
| 86 | +John 35 CSharp |
| 87 | +``` |
0 commit comments