|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Sep 2004
Messaggi: 3967
|
[C#/MySql]Array di MySqlParameter
Ciao a tutti
![]() Sto cercando di crearmi una classe generalizzata per le comuni operazioni sui db. Al momento sono giunto a questo risultato (parziale) Codice:
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Text; using System.Windows.Forms; using MySql.Data.MySqlClient; namespace ClassUtil { public class DataBaseClass { private string connectionString = ConfigurationManager.ConnectionStrings["ClassUtil.Properties.Settings.dbConnectionString"].ToString(); private MySqlConnection cn; private MySqlDataAdapter mda; private MySqlCommand cmd; private MySqlParameter[] p; private int numberOfParameters; private string commandString; private int recordReturned; private bool opState; private string serverIp; private int serverPort; private string userId; private string userPasswd; private string databaseName; public DataBaseClass() { } #region connectionParameters public string ServerIP { get { return serverIp; } set { serverIp = value; } } public int ServerPort { get { return serverPort; } set { serverPort = value; } } public string UserID { get { return userId; } set { userId = value; } } public string UserPassw { get { return userPasswd; } set { userPasswd = value; } } public string DatabaseName { get { return databaseName; } set { databaseName = value; } } #endregion #region DbCommandsProperties public int NumberOfParameters { get { return numberOfParameters; } set { numberOfParameters = value; } } public string CommandString { get { return commandString; } set { commandString = value; } } public int GeneralQueryAction() { try { cmd = new MySqlCommand(commandString, cn); recordReturned = cmd.ExecuteNonQuery(); cn.Close(); return recordReturned; } catch (MySqlException ex) { MessageBox.Show(ex.Message.ToString()); } } public void BindMyData(DataGridView d, BindingNavigator b) { try { mda = new MySqlDataAdapter(commandString, cn); DataTable dt = new DataTable(); mda.Fill(dt); BindingSource bs = new BindingSource(); bs.DataSource = dt; b.BindingSource = bs; d.DataSource = bs; cn.Close(); } catch (MySqlException ex) { MessageBox.Show(ex.Message.ToString()); } } #endregion public void OpenConnection() { if (cn != null) { cn.Close(); } try { using (cn = new MySqlConnection(connectionString)) { cn.Open(); } } catch (MySqlException ex) { switch (ex.Number) { case 0: break; default: break; } } } } } Dato che spesso la cosa + comune alla quale si va incontro è di avere a che fare con dei forms sui quali sono presenti textbox, combobox etc.. Volevo astrarre questo concetto senza dovermi scrivere ogni volta una query per ogni operazione nel form, per questo ho pensato ad una proprietà "commandString" e ad un metodo "GeneralQueryAction". Ora, volevo creare anche un metodo che: 1)Mediante una proprietà "NumeroDiParametri" mi contasse quanti oggetti di tipo textbox (per esempio) ci siano sul form; 2)Passare questo numero come lunghezza di un array di MySqlParameter. Fatto questo, credo poi di saper utilizzare un metodo che inizializzi i parametri. Grazie mille ![]() RaouL.
__________________
Dai wafer di silicio nasce: LoHacker... il primo biscotto Geek ![]() |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Sep 2004
Messaggi: 3967
|
mmm... rileggendo il messaggio credo di essermi espresso molto male..
Allora, quello che vorrei fare è: Codice:
//codice che risulterebbe se usassi di volta in volta un form MySqlParameter[] p = new MySqlParameter[numeroditextboxPerEsempio]; p[0] = new MySqlParameter("?ID", MySqlDbType.Int, 2); p[0].Value = textBoxId.Text; //etcetera.... //quello che invece vorrei fare è un metodo pubblico //tipo: //ottengo il numero di controlli che ho sul form, per esempio in un pannello private int _numberOfControls; public int NumberOfControls { get { return _numberOfContols; } set { _numberOfControls = value; } } //nel form ad esempio conto i controlli presenti in un pannello myClass.NumberOfControls = this.myPanel.Controls.Count; //qui non so come fare per creare il metodo pubblico che mi inizializzi i mysqlparameter....
__________________
Dai wafer di silicio nasce: LoHacker... il primo biscotto Geek ![]() |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 16:03.