You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Programación<br />
bien, ahora ya tenemos una tabla para trabajar y probar los ejemplos para el objeto Command:<br />
using System;<br />
using System.Data;<br />
using Npgsql;<br />
namespace godel.examples{<br />
public class ejemplo2{<br />
static NpgsqlConnection conn = null;<br />
static NpgsqlCommand cmd = null;<br />
public static int Main(string[] args){<br />
Console.WriteLine("Probando un comando a la base de datos...");<br />
try{<br />
int rowsAffected = 0;<br />
int opcion = 0;<br />
conn = new NpgsqlConnection();<br />
conn.ConnectionString = @"Server=127.0.0.1;Port=5432;User<br />
Id=postgres;Password=chikome;Database=pubs";<br />
conn.Open();<br />
cmd = new NpgsqlCommand();<br />
cmd.Connection = conn;<br />
Console.WriteLine("Eliga el tipo de comando:");<br />
Console.WriteLine("[1] INSERT into authors VALUES('Carson','Cheryl','415<br />
548-7223','589 Darwin Ln.','Berkeley','CA','94705','1')");<br />
Console.WriteLine("[2] UPDATE authors set au_fname = 'Martin' WHERE au_id = 1");<br />
Console.WriteLine("[3] DELETE from authors WHERE au_fname = 'Martin'");<br />
Console.Write("Elija opcion ");<br />
opcion = Console.Read();<br />
switch(opcion){<br />
case 1:<br />
cmd.CommandText = "INSERT into<br />
authors(au_lname,au_fname,phone,address,city,state,zip,contract)VALUES('Carson','Cheryl','415<br />
548-7223','589 Darwin Ln.','Berkeley','CA','94705','1')";<br />
break;<br />
case 2:<br />
cmd.CommandText = "UPDATE authors set au_fname = 'Martin' WHERE<br />
au_fname = 'Johnson'";<br />
break;<br />
case 3:<br />
cmd.CommandText = "DELETE from authors WHERE au_fname = 'Martin'";<br />
break;<br />
default:<br />
cmd.CommandText = "INSERT into<br />
authors(au_lname,au_fname,phone,address,city,state,zip,contract)VALUES('Carson','Cheryl','415<br />
548-7223','589 Darwin Ln.','Berkeley','CA','94705','1')";<br />
break;<br />
}<br />
rowsAffected = cmd.Execute<strong>No</strong>nQuery();<br />
Console.WriteLine("(" + rowsAffected + ") registro(s) afectados");<br />
conn.Close();<br />
return 0;<br />
}catch(Exception e){<br />
Console.WriteLine("Sin conexion por {0}",e.ToString());<br />
return -1;<br />
}finally{<br />
conn.Close();<br />
}<br />
PÁGINA 43<br />
BEGINS <strong>11</strong> I OCTUBRE 2007