Skip to content
This repository was archived by the owner on Jul 21, 2019. It is now read-only.

Commit c7dc178

Browse files
sibtayshahrkarthik007
authored andcommitted
Added hello world program for C# YSQL (#136)
1 parent 6dbf96d commit c7dc178

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

content/latest/develop/build-apps/csharp.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,20 @@ menu:
1919
YCQL
2020
</a>
2121
</li>
22+
<li>
23+
<a href="#ysql" class="nav-link" id="ysql-tab" data-toggle="tab" role="tab" aria-controls="ysql" aria-selected="true">
24+
<i class="icon-postgres" aria-hidden="true"></i>
25+
YSQL
26+
</a>
27+
</li>
2228
</ul>
2329

2430
<div class="tab-content">
2531
<div id="ycql" class="tab-pane fade show active" role="tabpanel" aria-labelledby="ycql-tab">
2632
{{% includeMarkdown "ycql/csharp.md" /%}}
2733
</div>
34+
<div id="ysql" class="tab-pane fade show active" role="tabpanel" aria-labelledby="ysql-tab">
35+
{{% includeMarkdown "ysql/csharp.md" /%}}
36+
</div>
37+
2838
</div>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
![Add Package](/images/develop/client-drivers/csharp/visual-studio-add-package.png)
18+
<li>Search for Npgsql and click Add Package.</li>
19+
![Search Package](/images/develop/client-drivers/csharp/visual-studio-search-ngpsql-package.png)
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+
```
Loading

0 commit comments

Comments
 (0)