C#
using System.Configuration;
using System.Data;
using System.Data.OleDb;
informazioni
del nostro server DB, qui di seguito un esempio di app.config.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="DB2" connectionString="Provider=DB2OLEDB;
Persist Security Info = False; User ID = NomeUtente; Password = Password;
Initial Catalog = Database; Data Source = Database; APPC Mode Name = IBMRDB;
Network Transport Library = TCPIP; Network Address = NomeServer; Network Port =
5025; Package Collection = Nome; Default Schema = NomeSchema; DEFER PREPARE =
true;"/>
</connectionStrings>
</configuration>
private void Form1_Load(object sender, EventArgs e)
{
using (var connOleDB2 = new
OleDbConnection(ConfigurationManager.ConnectionStrings["DB2"].ConnectionString))
{
using (var cmdOleDB2 = new
OleDbCommand())
{
connOleDB2.Open();
cmdOleDB2.CommandType = CommandType.Text;
cmdOleDB2.CommandText = "SELECT Nome, Cognome FROM
Schema.Anagrafica";
cmdOleDB2.Connection = connOleDB2;
try
{
var
risultatoReader = cmdOleDB2.ExecuteReader();
DataTable dati = new DataTable();
dati.Load(risultatoReader);
DtgDati.DataSource = dati;
}
catch (Exception ex)
{
MessageBox.Show("Errore: " + ex.Message);
}
}
}
}
C#
private void BtnUpdate_Click(object sender,
EventArgs e)
{
using (var connOleDB2 = new
OleDbConnection(ConfigurationManager.ConnectionStrings["DB2"].ConnectionString))
{
using (var cmdOleDB2 = new
OleDbCommand())
{
connOleDB2.Open();
cmdOleDB2.CommandType = CommandType.Text;
cmdOleDB2.CommandText = "update Schema.Anagrafica set Cognome =
'test', Nome = 'test2' where ID = 5";
cmdOleDB2.Connection = connOleDB2;
try
{
var
risultatoReader = cmdOleDB2.ExecuteNonQuery();
MessageBox.Show("Record aggiornati: " + risultatoReader);
}
catch (Exception ex)
{
MessageBox.Show("Errore: " + ex.Message);
}
}
}
}
C#
private void BtnCancella_Click(object sender, EventArgs e)
{
using (var connOleDB2 = new
OleDbConnection(ConfigurationManager.ConnectionStrings["DB2"].ConnectionString))
{
using (var cmdOleDB2 = new OleDbCommand())
{
connOleDB2.Open();
cmdOleDB2.CommandType = CommandType.Text;
cmdOleDB2.CommandText = "DELETE Schema.Anagrafica where ID =
5";
cmdOleDB2.Connection = connOleDB2;
try
{
var
risultatoReader = cmdOleDB2.ExecuteNonQuery();
MessageBox.Show("Record Cancellati: " + risultatoReader);
}
catch (Exception ex)
{
MessageBox.Show("Errore: " + ex.Message);
}
}
}
}
private void BtnInsert_Click(object sender, EventArgs e)
{
using (var connOleDB2 = new
OleDbConnection(ConfigurationManager.ConnectionStrings["DB2"].ConnectionString))
{
using (var cmdOleDB2 = new
OleDbCommand())
{
connOleDB2.Open();
cmdOleDB2.CommandType = CommandType.Text;
cmdOleDB2.CommandText = "INSERT INTO Schema.Anagrafica (Nome, Cognome) VALUES(‘Emanuele’,
‘Mattei’)";
cmdOleDB2.Connection = connOleDB2;
try
{
var
risultatoReader = cmdOleDB2.ExecuteNonQuery();
MessageBox.Show("Record Inseriti: " + risultatoReader);
}
catch (Exception ex)
{
MessageBox.Show("Errore: " + ex.Message);
}
}
}
}
Nessun commento:
Posta un commento