// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Data; using MySql.Data.MySqlClient; namespace TextSearch.Rdbms.MySQL.Search { public class Program { private static void Dump0(IDbCommand cmd, string searchtype) { Console.WriteLine("{0}", searchtype); using(IDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { Console.WriteLine(" {0} - {1}", (string)dr["names"], (string)dr["moves"]); } } } private static void Dump1(IDbCommand cmd, string searchtype, string searchterm ) { Console.WriteLine("{0} search for : {1}", searchtype, searchterm); ((IDbDataParameter)cmd.Parameters["@st"]).Value = searchterm; using (IDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { Console.WriteLine(" {0} - {1}", (string)dr["names"], (string)dr["moves"]); } } } public static void Main(string[] args) { using(IDbConnection con = new MySqlConnection("Server=localhost;Database=test;User Id=arne;Password=hemmeligt")) { con.Open(); // all using (IDbCommand cmd = con.CreateCommand()) { cmd.CommandText = "SELECT names,moves FROM chessopening"; Dump0(cmd, "all"); } // one using (IDbCommand cmd = con.CreateCommand()) { cmd.CommandText = "SELECT names,moves FROM chessopening WHERE MATCH(names) AGAINST (@st)"; IDbDataParameter st = cmd.CreateParameter(); st.ParameterName = "@st"; st.DbType = DbType.String; cmd.Parameters.Add(st); Dump1(cmd, "one", "defense"); } using (IDbCommand cmd = con.CreateCommand()) { cmd.CommandText = "SELECT names, moves FROM chessopening WHERE MATCH(moves) AGAINST (@st)"; IDbDataParameter st = cmd.CreateParameter(); st.ParameterName = "@st"; st.DbType = DbType.String; cmd.Parameters.Add(st); Dump1(cmd, "one natural", "d4"); } // dual using (IDbCommand cmd = con.CreateCommand()) { cmd.CommandText = "SELECT names, moves FROM chessopening WHERE MATCH(names,moves) AGAINST (@st)"; IDbDataParameter st = cmd.CreateParameter(); st.ParameterName = "@st"; st.DbType = DbType.String; cmd.Parameters.Add(st); Dump1(cmd, "dual natural", "defense"); Dump1(cmd, "dual natural", "d4"); Dump1(cmd, "dual natural", "defense d4"); // implicit or } // dual boolean using (IDbCommand cmd = con.CreateCommand()) { cmd.CommandText = "SELECT names, moves FROM chessopening WHERE MATCH(names,moves) AGAINST (@st IN BOOLEAN MODE)"; IDbDataParameter st = cmd.CreateParameter(); st.ParameterName = "@st"; st.DbType = DbType.String; cmd.Parameters.Add(st); Dump1(cmd, "dual boolean", "defense"); Dump1(cmd, "dual boolean", "d4"); Dump1(cmd, "dual boolean", "defense d4"); // implicit or Dump1(cmd, "dual boolean", "+defense+d4"); // and Dump1(cmd, "dual boolean", "+defense-d4"); // not Dump1(cmd, "dual boolean", "def*"); // wildcard Dump1(cmd, "dual boolean", "N*"); // wildcard } } } } }