Skip to content

Instantly share code, notes, and snippets.

@Suchiman
Created September 4, 2015 19:11
Show Gist options
  • Save Suchiman/c73d2cfa0212b1575598 to your computer and use it in GitHub Desktop.
Save Suchiman/c73d2cfa0212b1575598 to your computer and use it in GitHub Desktop.
using System;
using System.Data.SQLite;
namespace StringInterpolationSQL
{
class Program
{
private static SQLiteConnection connection;
static void Main(string[] args)
{
connection = new SQLiteConnection("Data Source=db.sqlite");
connection.Open();
CreateSchema();
Console.WriteLine("Enter Name");
string name = Console.ReadLine();
Insert($"INSERT INTO Students (Name) VALUES ({name})");
}
static void Insert(FormattableString query)
{
SQLiteCommand cmd = connection.CreateCommand();
object[] args = query.GetArguments();
object[] stringArgs = new string[args.Length];
for (int i = 0; i < args.Length; i++)
{
string param = "@p" + i.ToString();
stringArgs[i] = param;
cmd.Parameters.AddWithValue(param, args[i]);
}
cmd.CommandText = String.Format(query.Format, stringArgs);
cmd.ExecuteNonQuery();
}
static void CreateSchema()
{
SQLiteCommand create = connection.CreateCommand();
create.CommandText = "CREATE TABLE Students(Name TEXT)";
create.ExecuteNonQuery();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment