Created
September 4, 2015 19:11
-
-
Save Suchiman/c73d2cfa0212b1575598 to your computer and use it in GitHub Desktop.
This file contains 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 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