Rumah >pangkalan data >tutorial mysql >Melaksanakan Pembantu Pangkalan Data dalam C# Menggunakan Panduan Langkah demi Langkah Corak Reka Bentuk Strategi
Langkah 1: Tentukan Antara Muka Strategi
Pertama, buat antara muka ISQLStrategy yang mengisytiharkan kaedah GetDataTable yang akan dilaksanakan oleh strategi pangkalan data yang berbeza.
using System.Data; namespace MyProject.Util { public interface ISQLStrategy { DataTable GetDataTable(string sql); } }
Langkah 2: Laksanakan Strategi Konkrit
Seterusnya, laksanakan antara muka ISQLStrategy untuk setiap jenis pangkalan data (MySQL, PostgreSQL dan SQL Server).
Strategi MySQL:
using MySql.Data.MySqlClient; using System; using System.Configuration; using System.Data; namespace MyProject.Util { public class MySQLQuery : ISQLStrategy { public DataTable GetDataTable(string sql) { try { using (MySqlConnection conn = new MySqlConnection(ConfigurationManager.AppSettings["MySqlConnectionString"])) { DataTable dt = new DataTable(); conn.Open(); MySqlCommand command = new MySqlCommand(sql, conn); dt.Load(command.ExecuteReader()); conn.Close(); return dt; } } catch (Exception e) { Console.WriteLine(e.ToString()); return null; } } } }
Strategi PostgreSQL:
using Npgsql; using System; using System.Configuration; using System.Data; namespace MyProject.Util { public class NpgSqlQuery : ISQLStrategy { public DataTable GetDataTable(string sql) { try { using (NpgsqlConnection conn = new NpgsqlConnection(ConfigurationManager.AppSettings["NpgSqlConnectionString"])) { DataTable dt = new DataTable(); conn.Open(); NpgsqlCommand command = new NpgsqlCommand(sql, conn); NpgsqlDataAdapter _dap = new NpgsqlDataAdapter(command); _dap.Fill(dt); conn.Close(); return dt; } } catch (Exception e) { Console.WriteLine(e.ToString()); return null; } } } }
Strategi Pelayan SQL:
using System; using System.Configuration; using System.Data; using System.Data.SqlClient; namespace MyProject.Util { public class TSqlQuery : ISQLStrategy { public DataTable GetDataTable(string sql) { try { using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["TSqlConnectionString"])) { DataTable dt = new DataTable(); conn.Open(); SqlCommand command = new SqlCommand(sql, conn); SqlDataAdapter da = new SqlDataAdapter(command); da.Fill(dt); conn.Close(); return dt; } } catch (Exception e) { Console.WriteLine(e.ToString()); return null; } } } }
Langkah 3: Cipta Kelas Konteks
Kelas SQLStrategy akan menggunakan contoh ISQLStrategy untuk melaksanakan operasi pangkalan data.
using System.Data; namespace MyProject.Util { public class SQLStrategy { private readonly ISQLStrategy _sqlStrategy; public SQLStrategy(ISQLStrategy sqlStrategy) { _sqlStrategy = sqlStrategy; } public DataTable GetDataTable(string sql) { return _sqlStrategy.GetDataTable(sql); } } }
Langkah 4: Laksanakan Kod Pelanggan
Akhir sekali, tulis kod pelanggan untuk menguji pelaksanaan corak strategi.
using System; using System.Data; namespace MyProject.Util { public class Client { public static void Main() { SQLStrategy sqlHelper = new(new TSqlQuery()); DataTable result = sqlHelper.GetDataTable("SELECT TOP (10) * FROM [Product]"); foreach (DataRow row in result.Rows) { foreach (DataColumn column in result.Columns) { Console.Write($"{column.ColumnName}: {row[column]} \t"); } Console.WriteLine(); } } } }
Ringkasan
Pengekodan Penuh
using System; using System.Configuration; using System.Data; using System.Data.SqlClient; using Npgsql; using MySql.Data.MySqlClient; namespace MyProject.Util { //Strategy (Interface) public interface ISQLStrategy { DataTable GetDataTable(string sql); //You could add more methods for "Create", "Update" as well } //Concrete Strategies public class MySQLQuery : ISQLStrategy { public DataTable GetDataTable(string sql) { try { using (MySqlConnection conn = new MySqlConnection(ConfigurationManager.AppSettings["MySqlConnectionString"])) { DataTable dt = new DataTable(); conn.Open(); MySqlCommand command = new MySqlCommand(sql, conn); dt.Load(command.ExecuteReader()); conn.Close(); return dt; } } catch (Exception e) { Console.WriteLine(e.ToString()); return null; } } } public class NpgSqlQuery : ISQLStrategy { public DataTable GetDataTable(string sql) { try { using (NpgsqlConnection conn = new NpgsqlConnection(ConfigurationManager.AppSettings["NpgSqlConnectionString"])) { DataTable dt = new DataTable(); conn.Open(); NpgsqlCommand command = new NpgsqlCommand(sql, conn); NpgsqlDataAdapter _dap = new NpgsqlDataAdapter(command); _dap.Fill(dt); conn.Close(); return dt; } } catch (Exception e) { Console.WriteLine(e.ToString()); return null; } } } public class TSqlQuery : ISQLStrategy { public DataTable GetDataTable(string sql) { try { using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["TSqlConnectionString"])) { DataTable dt = new DataTable(); conn.Open(); SqlCommand command = new SqlCommand(sql, conn); SqlDataAdapter da = new SqlDataAdapter(command); da.Fill(dt); conn.Close(); return dt; } } catch (Exception e) { Console.WriteLine(e.ToString()); return null; } } } //Context public class SQLStrategy { public ISQLStrategy _sqlStrategy; public SQLStrategy(ISQLStrategy sqlStrategy) { _sqlStrategy = sqlStrategy; } public DataTable GetDataTable(string sql) { return _sqlStrategy.GetDataTable(sql); } } // Testing the Strategy Design Pattern // Client Code public class Client { public static void Main() { SQLStrategy sqlHelper = new(new TSqlQuery()); DataTable result = sqlHelper.GetDataTable("SELECT TOP (10) * FROM [Product]"); foreach (DataRow row in result.Rows) { foreach (DataColumn column in result.Columns) { Console.Write($"{column.ColumnName}: {row[column]} \t"); } Console.WriteLine(); } } } }
Cinta C#!
Atas ialah kandungan terperinci Melaksanakan Pembantu Pangkalan Data dalam C# Menggunakan Panduan Langkah demi Langkah Corak Reka Bentuk Strategi. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!