Code
UsingSystem;
UsingSystem. Collections. Generic;
UsingSystem. LINQ;
UsingSystem. text;
UsingSystem. Data;
UsingSystem. Data. sqlclient;
Namespace Leleapplication1
{
Class Program
{
Static Void Main ( String [] ARGs)
{
Sqlconnection Conn = New Sqlconnection ( " Server = zhuobin; uid = sa; Pwd = zhuobin; database = northwind " );
String Qry = " Select * from employees where country = 'uk' " ;
String INS = @" Insert into employees (firstname, lastname, titleofcourtesy, city, country) values (@ firstname, @ lastname, @ titleofcourtesy, @ city, @ country) " ;
Try
{
// Create the adapter
Sqldataadapter da = New Sqldataadapter ();
Da. selectcommand = New Sqlcommand (qry, Conn );
// Create the dataset
Dataset DS = New Dataset ();
Da. Fill (DS, " Employees " );
// Get the table reference
Datatable dt = DS. Tables [ " Employees " ];
// Add a new row
Datarow newrow = DT. newrow ();
Newrow [ " Firstname " ] = " Zhuo " ;
Newrow [ " Lastname " ] = " Bin " ;
Newrow [ " Titleofcourtesy " ] = " Sir " ;
Newrow [ " City " ] = " Tengzhou " ;
Newrow [ " Country " ] = " China " ;
DT. Rows. Add (newrow );
// Display the data
Foreach (Datarow row In DT. Rows)
{
Console. writeline ( " {0} {1} {2} " , Row [ " Firstname " ]. Tostring (). padright ( 15 ), Row [ " Lastname " ]. Tostring (). padleft ( 25 ), Row [ " City " ]);
}
// Insert into employees
// CREATE Command
Sqlcommand cmd = New Sqlcommand (INS, Conn );
// Map Parameters
Cmd. Parameters. Add ( " @ Firstname " , Sqldbtype. nvarchar, 10 , " Firstname " );
Cmd. Parameters. Add ( " @ Lastname " , Sqldbtype. nvarchar, 10 , " Lastname " );
Cmd. Parameters. Add ( " @ Titleofcourtesy " , Sqldbtype. nvarchar, 10 , " Titleofcourtesy " );
Cmd. Parameters. Add ( " @ City " , Sqldbtype. nvarchar, 15 , " City " );
Cmd. Parameters. Add ( " @ Country " , Sqldbtype. nvarchar, 15 , " Country " );
// Insert into employees
Da. insertcommand = CMD;
Da. Update (DS, " Employees " );
}
Catch (Sqlexception ex)
{
Console. writeline ( " The error: {0} " , Ex. Message );
}
Finally
{
Conn. Close ();
}
Console. Readline ();
}
}
}