Applicazione "ConnectToDB2"

screenshot applicazione

Funzione "Connect2DB2".
La funzione copia in locale, nella cartella c:\Temp, un file di database da AS400 e il descrittore del file.
La libreria e il nome tabella vengono letti da due text box dell'interfaccia utente.


private void Connect2DB2()
{
	string strLib = txtLib.Text;
	string strFile = txtFile.Text;
	string as400ip = "X.Y.Z.W";

	//
	cwbx.AS400System AS400 = new AS400System();

	AS400.Define(as400ip);

	AS400.UserID = "USER";
	AS400.Password = "PWD";

	if (AS400.IsConnected(cwbx.cwbcoServiceEnum.cwbcoServiceRemoteCmd) == 0)
	{
		RTBFeedback.AppendText("\n" + "AS400 IS CONNECTED!!\n");
	}

	try
	{
		// Copia di un file di database (in un file di testo + file .FDF, descrittore).
		cwbx.DatabaseTransfer DT = new DatabaseTransfer();
		DT.Download(as400ip, strLib + "/" + strFile, "C:\\Temp\\" + strFile + "(Lib " + strLib + ")" + ".txt");
		
		RTBFeedback.AppendText("Copia effettuata sotto c:\\Temp del file\n" + strFile + "(Lib " + strLib + ")" + ".txt\n");
	}
	catch (SystemException eX)
	{
		RTBFeedback.AppendText("Errore: " + eX.Message + "\n");
	}
	finally 
	{
		RTBFeedback.AppendText("FINE PROCEDURA - Connect2DB2 -\n");
	}

	// Disconnessione.
	AS400.Disconnect(cwbx.cwbcoServiceEnum.cwbcoServiceAll);
}

Funzione "Query1".
La funzione esegue una istruzione SQL, ricavandola da un oggetto Text Box.
Nel caso di SELECT viene mostrato il risultato dell'interrogazione in un oggetto Data Grid View.
Nel caso di INSERT, UPDATE, DELETE viene riportato il numero di record processati.
Viene riportato il tempo trascorso per l'elaborazione.


private void Query1()
{
	int nRows = 0;

	Cursor.Current = Cursors.WaitCursor;
	Application.DoEvents();

	if(dataGridView1.DataSource != null)
	{
		dataGridView1.DataSource = null;
	}    
	else
	{
		dataGridView1.Rows.Clear();
	}

	dataGridView1.Refresh();

	try
	{
		String Sql = RTBQuery.Text; 
		DataTable dt1 = new DataTable();
		DataSet ds1 = new DataSet();
		OleDbConnection ConAS400 = new OleDbConnection();
		ConAS400.ConnectionString = "Provider=IBMDA400;Data Source=indirizzoIP; User ID=ID;Password=PWD";
		OleDbCommand CmdAS400 = new OleDbCommand(Sql, ConAS400);
		OleDbDataAdapter sqlAS400 = new OleDbDataAdapter();

		sqlAS400.SelectCommand = CmdAS400;

		System.Diagnostics.Stopwatch stopWatch1 = new System.Diagnostics.Stopwatch();
		stopWatch1.Start();

		if (RTBQuery.Text.ToUpper().IndexOf("SELECT") > -1)
		{
			// La stringa SQL contiene la stringa "SELECT".
			RTBFeedback.AppendText("Operazione in corso..." + "\n");
			
			ConAS400.Open();

			sqlAS400.Fill(dt1);

			nRows = dt1.Rows.Count;
			
			if (dt1.Rows.Count > 0)
			{
				dataGridView1.DataSource = dt1;
				dataGridView1.DataMember = dt1.TableName;
			}

			RTBFeedback.AppendText("Operazione conclusa.\n" + "Numero record processati: " + nRows + "\n");
		}
		else
		{ 
			// La stringa SQL non contiene "SELECT",
			// quindi dovrebbe contenere "INSERT" o "UPDATE" o DELETE.
			//
			RTBFeedback.AppendText("Operazione in corso..." + "\n");
			
			OleDbCommand cmdUpdate = new OleDbCommand();
			ConAS400.Open();
			cmdUpdate = ConAS400.CreateCommand();
			cmdUpdate.CommandText = Sql;
			nRows = cmdUpdate.ExecuteNonQuery();
			
			RTBFeedback.AppendText("Operazione conclusa.\n" + "Numero record processati: " + nRows + "\n");
		}
		//

		// Feedback tempo di esecuzione.
		stopWatch1.Stop();
		// Tempo trascorso.
		TimeSpan ts = stopWatch1.Elapsed;

		// Formattazione.
		string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
			ts.Hours, ts.Minutes, ts.Seconds,
			ts.Milliseconds / 10);
		RTBFeedback.AppendText("\nRunTime (H:M:S.millisec) " + elapsedTime + "\n");
		//

		RTBFeedback.AppendText("FATTO.\n");
	}
	catch(System.Exception ex)
	{
		MessageBox.Show("Errore " + ex.Message);
	}
	finally
	{
		RTBFeedback.AppendText("FINE PROCEDURA - QUERY1 -\n");
	}
	Cursor.Current = Cursors.Default;
}