Created
June 18, 2016 19:00
-
-
Save natemcmaster/db14848ed53ae520e8a2b8a6c4410d52 to your computer and use it in GitHub Desktop.
Saving/reading guid as string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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