Névtér:
A következő osztályokat használjuk:
Hozzunk létre egy konzolos projektet:
mkdir app01 cd app01 dotnet new console
Telepítsük a projektbe a MySqlConnector csomagot:
dotnet add package Microsoft.Data.Sqlite
using Microsoft.Data.Sqlite; var conn = new SqliteConnection ("Data Source=hello.db"); conn.Open();
using Microsoft.Data.Sqlite; var conn = new SqliteConnection ("Data Source=hello.db"); conn.Open(); SqliteCommand cmd = conn.CreateCommand(); string sql = @" create table sampletable( col1 varchar(20), col2 int ) "; cmd.CommandText = sql; cmd.ExecuteNonQuery();
var conn = new SqliteConnection ("Data Source=hello.db"); conn.Open(); SqliteCommand cmd = conn.CreateCommand(); string sql = @" insert into sampletable values (@col1, @col2 ) "; cmd.CommandText = sql; cmd.Parameters.AddWithValue("@col1", "egy"); cmd.Parameters.AddWithValue("@col2", 33); cmd.Prepare(); cmd.ExecuteNonQuery();
using Microsoft.Data.Sqlite; var conn = new SqliteConnection ("Data Source=hello.db"); conn.Open(); SqliteCommand cmd = conn.CreateCommand(); string sql = "select * from sampletable"; cmd.CommandText = sql; SqliteDataReader reader = cmd.ExecuteReader(); while(reader.Read()) { Console.WriteLine($"{reader.GetString(0)} {reader.GetInt32(1)}"); }
using Microsoft.Data.Sqlite; var conn = new SqliteConnection ("Data Source=hello.db"); conn.Open(); string sql = "select * from sampletable"; using var cmd = new SqliteCommand (sql, conn); SqliteDataReader reader = cmd.ExecuteReader(); while(reader.Read()) { Console.WriteLine($"{reader.GetString(0)} {reader.GetInt32(1)}"); }
Használhatunk több Write utasítást:
while(reader.Read()) { Console.Write(reader.GetString(0)); Console.Write(" "); Console.WriteLine(reader.GetInt32(1)); }
using Microsoft.Data.Sqlite; var conn = new SqliteConnection ("Data Source=database.db"); try { conn.Open(); Console.WriteLine("Ok"); } catch (SqliteException e) { Console.Error.WriteLine("Hiba! A megnyitás sikertelen!"); Console.Error.WriteLine(e.Message); }
Példa konzolos menüvel: