Insertcommand attribute of sqldataadapter

Source: Internet
Author: User

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 ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.