一言で言うと、SQL Serverとの繋ぎはほぼ似ている。
ただし使用するメソッド/オブジェクトを
SqlXxx
からSQLiteXxx
あるいはMySqlXxx
に変わるだけ。
さて、本題に入る。先ずは基本のSQL serverに行こう。
することは全部同じ。
- データベースに繋ぎ(連結する)。
- CarというTableを作成。
- テーブルCarにデータを入れる。
- データを出力し、連結をクローズする。
using System; using System.Data.SqlClient; class SQLTest01 { static void Main() { /* Set the connection string * With the setting below: * user id : the userid for SQL server * password or pwd : the password of user * database : the database you want to connect */ string cs = "user id=testuser;" + "password=testpwd;server=localhost;" + "Trusted_Connection=yes;" + "database=dbTest; " + "connection timeout=30"; // Connection to Database // with the new "using" garbage collection using (SqlConnection con = new SqlConnection(cs)) { // Open connection; con.Open(); // Create the Table using (SqlCommand cmd = new SqlCommand()) { cmd.Connection = con; cmd.CommandText = "DROP TABLE IF EXISTS Cars"; cmd.ExecuteNonQuery(); cmd.CommandText = @"CREATE TABLE Cars(Id INTEGER PRIMARY KEY, Name TEXT, Price INT)"; cmd.ExecuteNonQuery(); } // Put Datas into Table using(SqlCommand cmd = new SqlCommand()) { cmd.Connection = con; // do with Prepare(); cmd.CommandText = "INSERT INTO Cars(Id, Name, Price) VALUES(@Id, @Name, @Price)"; cmd.Prepare(); cmd.Parameters.AddWithValue("@Id", 1); cmd.Parameters.AddWithValue("@Name", "Audi"); cmd.Parameters.AddWithValue("@Price", 52642); cmd.ExecuteNonQuery(); cmd.Parameters.AddWithValue("@Id", 2); cmd.Parameters.AddWithValue("@Name", "Mercedes"); cmd.Parameters.AddWithValue("@Price", 57127); cmd.ExecuteNonQuery(); // do with Standard SQL command cmd.CommandText = "INSERT INTO Cars VALUES(3,'Skoda',9000)"; cmd.ExecuteNonQuery(); cmd.CommandText = "INSERT INTO Cars VALUES(4,'Volvo',29000)"; cmd.ExecuteNonQuery(); cmd.CommandText = "INSERT INTO Cars VALUES(5,'Bentley',350000)"; cmd.ExecuteNonQuery(); cmd.CommandText = "INSERT INTO Cars VALUES(6,'Citroen',21000)"; cmd.ExecuteNonQuery(); cmd.CommandText = "INSERT INTO Cars VALUES(7,'Hummer',41400)"; cmd.ExecuteNonQuery(); cmd.CommandText = "INSERT INTO Cars VALUES(8,'Volkswagen',21600)"; cmd.ExecuteNonQuery(); } // Read Datas with DataReader string stm = "SELECT * FROM Cars LIMIT 5"; using (SqlCommand cmd = new SqlCommand(stm,con)) { using (SqlDataReader rdr = cmd.ExecuteReader()) { while (rdr.Read()) { Console.WriteLine(rdr.GetInt32(0) + " " + rdr.GetString(1) + " " + rdr.GetInt32(2)); } } } // Close connection; con.Close(); } } }
続き、似ている内容で、SQLiteでしましょう。
注意してほしい点は
- 事前にSystem.Data.SQLiteのDLLファイルをプロジェクトのフォルダ下のExternalsにコピーしておくこと。また、プロジェクト設定でそれに参照するように設定して下さい。
- DLLファイルを持っていない場合、SQLite.NET公式サイトあるいはSQLite公式サイトより探して下さい。無償で手に入れるはず。
using System; using System.Data.SQLite; class SQLTest01 { static void Main() { /* Set the connection string * With the setting below: * user id : the userid for SQL server * password or pwd : the password of user * database : the database you want to connect */ string cs = "URI=file:test.db"; // Connection to Database // with the new "using" garbage collection using (SQLiteConnection con = new SQLiteConnection(cs)) { // Open connection; con.Open(); // Create the Table using (SQLiteCommand cmd = new SQLiteCommand()) { cmd.Connection = con; cmd.CommandText = "DROP TABLE IF EXISTS Cars"; cmd.ExecuteNonQuery(); cmd.CommandText = @"CREATE TABLE Cars(Id INTEGER PRIMARY KEY, Name TEXT, Price INT)"; cmd.ExecuteNonQuery(); } // Put Datas into Table using (SQLiteCommand cmd = new SQLiteCommand()) { cmd.Connection = con; // do with Prepare(); cmd.CommandText = "INSERT INTO Cars(Id, Name, Price) VALUES(@Id, @Name, @Price)"; cmd.Prepare(); cmd.Parameters.AddWithValue("@Id", 1); cmd.Parameters.AddWithValue("@Name", "Audi"); cmd.Parameters.AddWithValue("@Price", 52642); cmd.ExecuteNonQuery(); cmd.Parameters.AddWithValue("@Id", 2); cmd.Parameters.AddWithValue("@Name", "Mercedes"); cmd.Parameters.AddWithValue("@Price", 57127); cmd.ExecuteNonQuery(); // do with Standard SQL command cmd.CommandText = "INSERT INTO Cars VALUES(3,'Skoda',9000)"; cmd.ExecuteNonQuery(); cmd.CommandText = "INSERT INTO Cars VALUES(4,'Volvo',29000)"; cmd.ExecuteNonQuery(); cmd.CommandText = "INSERT INTO Cars VALUES(5,'Bentley',350000)"; cmd.ExecuteNonQuery(); cmd.CommandText = "INSERT INTO Cars VALUES(6,'Citroen',21000)"; cmd.ExecuteNonQuery(); cmd.CommandText = "INSERT INTO Cars VALUES(7,'Hummer',41400)"; cmd.ExecuteNonQuery(); cmd.CommandText = "INSERT INTO Cars VALUES(8,'Volkswagen',21600)"; cmd.ExecuteNonQuery(); } // Read Datas with DataReader string stm = "SELECT * FROM Cars LIMIT 5"; using (SQLiteCommand cmd = new SQLiteCommand(stm, con)) { using (SQLiteDataReader rdr = cmd.ExecuteReader()) { while (rdr.Read()) { Console.WriteLine(rdr.GetInt32(0) + " " + rdr.GetString(1) + " " + rdr.GetInt32(2)); } } } // Close connection; con.Close(); } Console.WriteLine("Press ENTER to continue..."); Console.ReadLine(); } }
最後、MySQL(今回はローカルを例にする)でしましょう。
SQLiteの時と同じ、System.Data.MySQLのDLLを取得して下さい。SQLiteの時と同じ、MySQLの公式サイトより無償で手に入れるはず。
using System; using MySql.Data.MySqlClient; class SQLTest01 { static void Main() { /* Set the connection string * With the setting below: * user id : the userid for SQL server * password or pwd : the password of user * database : the database you want to connect */ string cs = "user id=testuser;" + "password=testpwd;server=localhost;" + "database=testDB; " + "connection timeout=30"; // Connection to Database // with the new "using" garbage collection using (MySqlConnection con = new MySqlConnection(cs)) { // Open connection; con.Open(); // Create the Table using (MySqlCommand cmd = new MySqlCommand()) { cmd.Connection = con; cmd.CommandText = "DROP TABLE IF EXISTS Cars"; cmd.ExecuteNonQuery(); cmd.CommandText = @"CREATE TABLE Cars(Id INTEGER PRIMARY KEY, Name TEXT, Price INT)"; cmd.ExecuteNonQuery(); } // Put Datas into Table using (MySqlCommand cmd = new MySqlCommand()) { cmd.Connection = con; // do with Prepare(); cmd.CommandText = "INSERT INTO Cars(Id, Name, Price) VALUES(1, 'Audi', 52642)"; cmd.ExecuteNonQuery(); cmd.CommandText = "INSERT INTO Cars(Id, Name, Price) VALUES(2, 'Mercedes', 57127)"; cmd.ExecuteNonQuery(); cmd.CommandText = "INSERT INTO Cars VALUES(3,'Skoda',9000)"; cmd.ExecuteNonQuery(); cmd.CommandText = "INSERT INTO Cars VALUES(4,'Volvo',29000)"; cmd.ExecuteNonQuery(); cmd.CommandText = "INSERT INTO Cars VALUES(5,'Bentley',350000)"; cmd.ExecuteNonQuery(); cmd.CommandText = "INSERT INTO Cars VALUES(6,'Citroen',21000)"; cmd.ExecuteNonQuery(); cmd.CommandText = "INSERT INTO Cars VALUES(7,'Hummer',41400)"; cmd.ExecuteNonQuery(); cmd.CommandText = "INSERT INTO Cars VALUES(8,'Volkswagen',21600)"; cmd.ExecuteNonQuery(); } // Read Datas with DataReader string stm = "SELECT * FROM Cars LIMIT 5"; using (MySqlCommand cmd = new MySqlCommand(stm, con)) { using (MySqlDataReader rdr = cmd.ExecuteReader()) { while (rdr.Read()) { Console.WriteLine(rdr.GetInt32(0) + " " + rdr.GetString(1) + " " + rdr.GetInt32(2)); } } } // Close connection; con.Close(); } } }
0 件のコメント:
コメントを投稿