// returns a Open connection public override void GetConnection () { string connectionString = null; try { connectionString = ConfigClass.GetElement (configDoc, "database", "connectionString"); } catch (XPathException e) { Console.WriteLine ("Error reading the config file !!"); Console.WriteLine (e.Message); con = null; return; } con = new MySqlConnection (connectionString); try { con.Open (); } catch (MySqlException e) { Console.WriteLine ("Cannot establish connection with the database"); Console.WriteLine ("Probably the Database is down "); con = null; } catch (InvalidOperationException e) { Console.WriteLine ("Cannot open connection "); Console.WriteLine ("Probably the connection is already open"); con = null; } catch (Exception e) { Console.WriteLine ("Cannot open connection "); con = null; } }
public void MySqlStore() { MySqlConnection connection = new MySqlConnection( "Server=localhost;" + "User ID=root;" + "Password=;"); connection.Open(); MySqlCommand cmd = connection.CreateCommand(); string sql = "DROP DATABASE openid_test;"; cmd.CommandText = sql; try { cmd.ExecuteNonQuery(); } catch (MySqlException e) { } cmd.Dispose(); cmd = connection.CreateCommand(); cmd.CommandText = "CREATE DATABASE openid_test;"; cmd.ExecuteNonQuery(); cmd.Dispose(); cmd = connection.CreateCommand(); cmd.CommandText = "USE openid_test;"; cmd.ExecuteNonQuery(); cmd.Dispose(); MySqlStore store = new MySqlStore(connection); store.CreateTables(); StoreTester tester = new StoreTester(store); tester.Test(); }
/// <summary>l um valor</summary> internal static int executeNonQuery( string query ) { string connectionString = OrionGlobals.getConnectionString("connectiostring-mysql"); MySqlConnection conn = new MySqlConnection(connectionString); try { conn.Open(); MySqlCommand cmd = conn.CreateCommand(); cmd.CommandText = query; cmd.CommandType = CommandType.Text; object obj = cmd.ExecuteScalar(); if( obj == null ) { return 0; } return int.Parse(obj.ToString()); } catch( Exception e ) { Chronos.Utils.Log.log("Connection String: {0}", connectionString); Chronos.Utils.Log.log("Error: " + e.Message); throw; } finally { conn.Close(); } }
/// <summary> /// Executes a single command against a MySQL database. A new <see cref="MySqlConnection"/> is created /// using the <see cref="MySqlConnection.ConnectionString"/> given. /// </summary> /// <param name="connectionString"><see cref="MySqlConnection.ConnectionString"/> to use</param> /// <param name="commandText">SQL command to be executed</param> /// <param name="parms">Array of <see cref="MySqlParameter"/> objects to use with the command.</param> /// <returns></returns> public static int ExecuteNonQuery( string connectionString, string commandText, params MySqlParameter[] parms ) { //create & open a SqlConnection, and dispose of it after we are done. using (MySqlConnection cn = new MySqlConnection(connectionString)) { cn.Open(); //call the overload that takes a connection in place of the connection string return ExecuteNonQuery(cn, commandText, parms ); } }
public CatLists(MySqlConnection conn) { _conn = conn; conn.Open(); ArrayList listNames = new ArrayList(); MySqlCommand cmd = conn.CreateCommand(); cmd.CommandText ="SELECT DISTINCT ListName FROM " + _table; MySqlDataReader dr = cmd.ExecuteReader(); while(dr.Read()) { listNames.Add(Convert.ToString(dr["ListName"])); } dr.Close(); conn.Close(); foreach(string listName in listNames){ List.Add(new CatList(listName, false, conn)); } }
/// <summary> /// Executes a single command against a MySQL database. /// </summary> /// <param name="connectionString">Settings to use for this command</param> /// <param name="commandText">Command text to use</param> /// <param name="commandParameters">Array of <see cref="MySqlParameter"/> objects to use with the command</param> /// <returns><see cref="MySqlDataReader"/> object ready to read the results of the command</returns> public static MySqlDataReader ExecuteReader(string connectionString, string commandText, params MySqlParameter[] commandParameters) { //create & open a SqlConnection MySqlConnection cn = new MySqlConnection(connectionString); cn.Open(); try { //call the private overload that takes an internally owned connection in place of the connection string return ExecuteReader(cn, null, commandText, commandParameters, false ); } catch { //if we fail to return the SqlDatReader, we need to close the connection ourselves cn.Close(); throw; } }
/// <summary> /// Updates the given table with data from the given <see cref="DataSet"/> /// </summary> /// <param name="connectionString">Settings to use for the update</param> /// <param name="commandText">Command text to use for the update</param> /// <param name="ds"><see cref="DataSet"/> containing the new data to use in the update</param> /// <param name="tablename">Tablename in the dataset to update</param> public static void UpdateDataSet( string connectionString, string commandText, DataSet ds, string tablename ) { MySqlConnection cn = new MySqlConnection( connectionString ); cn.Open(); MySqlDataAdapter da = new MySqlDataAdapter( commandText, cn ); MySqlCommandBuilder cb = new MySqlCommandBuilder( da ); da.Update( ds, tablename ); cn.Close(); }
public void Connect(string server, string user, string password, string database) { string myConn = "Data Source=" + server + ";User ID=" + user + ";Database=" + database + ";Password=" + password; mysql = new MySqlConnection(myConn); mysql.Open(); }
ArrayList TypeArray = new ArrayList(); //C# data types #endregion Fields #region Constructors //********************************* //constructor: open mysql database //********************************* public DataBaseReader(string connect_string) { dbcon = new MySqlConnection(connect_string); dbcon.Open(); }
public static MySqlDataReader Load(MySqlConnection conn, string table, string pk, int value) { conn.Open(); MySqlCommand cmd = new MySqlCommand("SELECT * FROM " + table + " WHERE " + pk + " = @PK", conn); cmd.Parameters.Add("@PK", value); return cmd.ExecuteReader(); }
public static MySqlDataReader LoadWhereColumnIs(MySqlConnection conn, string table, string key, string value) { conn.Open(); MySqlCommand cmd = new MySqlCommand("SELECT * FROM " + table + " WHERE " + key + " = @Id " + "ORDER BY 1", conn); cmd.Parameters.Add("@Id", value); MySqlDataReader dr = cmd.ExecuteReader(); return dr; }
/*public MySqlDataReader Load() { MySqlConnection conn = (MySqlConnection) _bb.Conn; conn.Open(); MySqlCommand cmd = new MySqlCommand("SELECT * FROM " + _bb.Table + " WHERE ID = @ID", conn); cmd.Parameters.Add("@Id", _bb.Id); return cmd.ExecuteReader(); }*/ public static MySqlDataReader LoadAll(MySqlConnection conn, string table) { conn.Open(); MySqlCommand cmd = new MySqlCommand("SELECT * FROM " + table, conn); MySqlDataReader dr = cmd.ExecuteReader(); return dr; }