Skip to content

Instantly share code, notes, and snippets.

@natemcmaster
Created June 18, 2016 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natemcmaster/db14848ed53ae520e8a2b8a6c4410d52 to your computer and use it in GitHub Desktop.
Save natemcmaster/db14848ed53ae520e8a2b8a6c4410d52 to your computer and use it in GitHub Desktop.
Saving/reading guid as string
using System;
using Microsoft.Data.Sqlite;
using static System.Console;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
using (var conn = new SqliteConnection("Data Source=:memory:"))
{
conn.Open();
var cmd = conn.CreateCommand();
cmd.CommandText = @"CREATE TABLE tbl ( col TEXT );";
cmd.ExecuteNonQuery();
var guid = Guid.NewGuid();
var save = conn.CreateCommand();
save.CommandText = "INSERT INTO tbl VALUES (@p1)";
save.Parameters.AddWithValue("@p1", guid.ToString());
save.ExecuteNonQuery();
WriteLine($"Saved new guid {guid}");
var query = conn.CreateCommand();
query.CommandText = "SELECT * FROM tbl";
using (var reader = query.ExecuteReader())
{
while (reader.Read())
{
WriteLine($"Sqlite type: {reader.GetDataTypeName(0)}");
WriteLine($"CLR Type: {reader.GetFieldType(0).FullName}");
WriteLine($"Value: '{reader.GetValue(0)}'");
var g = new Guid(reader.GetFieldValue<string>(0));
WriteLine($"var g is: {g}, Type: {g.GetType().FullName}");
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment