C#
Attention
This page documents an earlier version. Go to the latest (v2.3) version.Prerequisites
This tutorial assumes that you have:
- installed YugabyteDB, created a universe and are able to interact with it using the CQL shell. If not, please follow these steps in the quick start guide.
- installed Visual Studio
Writing a HelloWorld C# app
In your Visual Studio create a new Project and choose Console Application as template. Follow the instructions to save the project.
Install Cassandra C# driver
To install the driver in your Visual Studio project
- Open your Project Solution View.
- Right-click on Packages and click Add Packages.
- Search for CassandraCSharpDriver and click Add Package.
Copy the contents below to your Program.cs
file.
using System;
using System.Linq;
using Cassandra;
namespace Yugabyte_CSharp_Demo
{
class Program
{
static void Main(string[] args)
{
try
{
var cluster = Cluster.Builder()
.AddContactPoints("127.0.0.1")
.WithPort(9042)
.Build();
var session = cluster.Connect();
session.Execute("CREATE KEYSPACE IF NOT EXISTS ybdemo");
Console.WriteLine("Created keyspace ybdemo");
<span style="color:#b00040">var</span> createStmt = <span style="color:#ba2121">"CREATE TABLE IF NOT EXISTS ybdemo.employee("</span> +
<span style="color:#ba2121">"id int PRIMARY KEY, name varchar, age int, language varchar)"</span>;
session.Execute(createStmt);
Console.WriteLine(<span style="color:#ba2121">"Created keyspace employee"</span>);
<span style="color:#b00040">var</span> insertStmt = <span style="color:#ba2121">"INSERT INTO ybdemo.employee(id, name, age, language) "</span> +
<span style="color:#ba2121">"VALUES (1, 'John', 35, 'C#')"</span>;
session.Execute(insertStmt);
Console.WriteLine(<span style="color:#ba2121">"Inserted data: {0}"</span>, insertStmt);
<span style="color:#b00040">var</span> preparedStmt = session.Prepare(<span style="color:#ba2121">"SELECT name, age, language "</span> +
<span style="color:#ba2121">"FROM ybdemo.employee WHERE id = ?"</span>);
<span style="color:#b00040">var</span> selectStmt = preparedStmt.Bind(<span style="color:#666">1</span>);
<span style="color:#b00040">var</span> result = session.Execute(selectStmt);
<span style="color:#b00040">var</span> rows = result.GetRows().ToList();
Console.WriteLine(<span style="color:#ba2121">"Select query returned {0} rows"</span>, rows.Count());
Console.WriteLine(<span style="color:#ba2121">"Name\tAge\tLanguage"</span>);
<span style="color:#008000;font-weight:bold">foreach</span> (Row row <span style="color:#008000;font-weight:bold">in</span> rows)
Console.WriteLine(<span style="color:#ba2121">"{0}\t{1}\t{2}"</span>, row[<span style="color:#ba2121">"name"</span>], row[<span style="color:#ba2121">"age"</span>], row[<span style="color:#ba2121">"language"</span>]);
session.Dispose();
cluster.Dispose();
}
<span style="color:#008000;font-weight:bold">catch</span> (Cassandra.NoHostAvailableException)
{
Console.WriteLine(<span style="color:#ba2121">"Make sure YugabyteDB is running locally!."</span>);
}
<span style="color:#008000;font-weight:bold">catch</span> (Cassandra.InvalidQueryException ie)
{
Console.WriteLine(<span style="color:#ba2121">"Invalid Query: "</span> + ie.Message);
}
}
}
}
Running the C# app
Run the C# app from menu select Run -> Start Without Debugging
You should see the following as the output.
Created keyspace ybdemo
Created keyspace employee
Inserted data: INSERT INTO ybdemo.employee(id, name, age, language) VALUES (1, 'John', 35, 'C#')
Select query returned 1 rows
Name Age Language
John 35 C#
Prerequisites
This tutorial assumes that you have:
- installed YugabyteDB, created a universe and are able to interact with it using the YSQL shell. If not, please follow these steps in the ysql guide.
- installed Visual Studio
Writing a HelloWorld C# application
In your Visual Studio create a new Project and choose Console Application as template. Follow the instructions to save the project.
Install YSQL C# driver
To install the driver in your Visual Studio project
- Open your Project Solution View.
- Right-click on Packages and click Add Packages.
- Search for Npgsql and click Add Package.
Copy the contents below to your Program.cs
file
using System;
using Npgsql;
namespace Yugabyte_CSharp_Demo
{
class Program
{
static void Main(string[] args)
{
NpgsqlConnection conn = new NpgsqlConnection("host=localhost;port=5433;database=yb_demo;user id=postgres;password=");
<span style="color:#008000;font-weight:bold">try</span>
{
conn.Open();
NpgsqlCommand empCreateCmd = <span style="color:#008000;font-weight:bold">new</span> NpgsqlCommand(<span style="color:#ba2121">"CREATE TABLE employee (id int PRIMARY KEY, name varchar, age int, language varchar);"</span>, conn);
empCreateCmd.ExecuteNonQuery();
Console.WriteLine(<span style="color:#ba2121">"Created table Employee"</span>);
NpgsqlCommand empInsertCmd = <span style="color:#008000;font-weight:bold">new</span> NpgsqlCommand(<span style="color:#ba2121">"INSERT INTO employee (id, name, age, language) VALUES (1, 'John', 35, 'CSharp');"</span>, conn);
<span style="color:#b00040">int</span> numRows = empInsertCmd.ExecuteNonQuery();
Console.WriteLine(<span style="color:#ba2121">"Inserted data (1, 'John', 35, 'CSharp')"</span>);
NpgsqlCommand empPrepCmd = <span style="color:#008000;font-weight:bold">new</span> NpgsqlCommand(<span style="color:#ba2121">"SELECT name, age, language FROM employee WHERE id = @EmployeeId"</span>, conn);
empPrepCmd.Parameters.Add(<span style="color:#ba2121">"@EmployeeId"</span>, NpgsqlTypes.NpgsqlDbType.Integer);
empPrepCmd.Parameters[<span style="color:#ba2121">"@EmployeeId"</span>].Value = <span style="color:#666">1</span>;
NpgsqlDataReader reader = empPrepCmd.ExecuteReader();
Console.WriteLine(<span style="color:#ba2121">"Query returned:\nName\tAge\tLanguage"</span>);
<span style="color:#008000;font-weight:bold">while</span> (reader.Read())
{
Console.WriteLine(<span style="color:#ba2121">"{0}\t{1}\t{2}"</span>, reader.GetString(<span style="color:#666">0</span>), reader.GetInt32(<span style="color:#666">1</span>), reader.GetString(<span style="color:#666">2</span>));
}
}
<span style="color:#008000;font-weight:bold">catch</span> (Exception ex)
{
Console.WriteLine(<span style="color:#ba2121">"Failure: "</span> + ex.Message);
}
<span style="color:#008000;font-weight:bold">finally</span>
{
<span style="color:#008000;font-weight:bold">if</span> (conn.State != System.Data.ConnectionState.Closed)
{
conn.Close();
}
}
}
}
}
Running the C# application
Run the C# app from menu select Run -> Start Without Debugging
You should see the following as the output.
Created table Employee
Inserted data (1, 'John', 35, 'CSharp')
Query returned:
Name Age Language
John 35 CSharp