Update database with Ado.net for distributed program Design (learning notes)

Source: Internet
Author: User
Tags insert
Provides a data service class that invokes GetDataSet to get the required dataset, then modifies it in the user interface, and finally calls Savedate to save the pending changes to the database.
Using System;
Using System.Data;
Using System.Data.SqlClient;

Namespace Asterdnet.databind
{
<summary>
Summary description for GetData.
</summary>
public class TestData
{
Private SqlDataAdapter da;
Public TestData ()
{
//
Todo:add constructor Logic here
//
}
Public DataSet GetDataSet ()
{

String strconn= "Data source=asterdnet;initial catalog=test;integrated security=sspi;persist security Info=False;user Id=sa;workstation id=asterdnet;packet size=4096 ";
SqlConnection conn=new SqlConnection (strconn);
Conn. Open ();
DataSet ds=new DataSet ();

SqlParameter workparam=new SqlParameter ();
Da=new SqlDataAdapter ();
Build the Select Command
Da. SelectCommand =new SqlCommand ("SELECT * FROM Education", Conn);
Da. Fill (ds, "Education");


Build the Insert Command
Da. InsertCommand = new SqlCommand ("Insert into education (ID, education) VALUES (@ID, @Education)", conn);

Workparam = da. INSERTCOMMAND.PARAMETERS.ADD ("@ID", SqlDbType.Int);
Workparam.sourcecolumn = "ID";
Workparam.sourceversion = DataRowVersion.Current;

Workparam = da. INSERTCOMMAND.PARAMETERS.ADD ("@Education", SqlDbType.NChar, 50);
Workparam.sourceversion = DataRowVersion.Current;
Workparam.sourcecolumn = "Education";

Build the update command
Da. UpdateCommand = new SqlCommand ("Update education Set education = @Education WHERE ID = @ID", conn);

Workparam = da. UPDATECOMMAND.PARAMETERS.ADD ("@ID", SqlDbType.Int);
Workparam.sourcecolumn = "ID";
Workparam.sourceversion = datarowversion.original;

Workparam = da. UPDATECOMMAND.PARAMETERS.ADD ("@Education", SqlDbType.NChar, 50);
Workparam.sourceversion = DataRowVersion.Current;
Workparam.sourcecolumn = "Education";

Build the Delete command
Da. DeleteCommand =new SqlCommand ("Delete from education where id= @ID", conn);
Workparam=da. DELETECOMMAND.PARAMETERS.ADD ("@ID", SqlDbType.Int);
Workparam.sourcecolumn = "ID";
Workparam.sourceversion =datarowversion.original;

return DS;
}
Save the Change dataset
public void Savedate (DataSet dataset,string tablename)
{
Da. Update (Dataset,tablename);
}

}
}

In the example, I created a database of test, in which a education table was created, with two field IDs (int), education (char), passed in RC3.

The feeling is not vb6.0 with ado2.5 so convenient (just set the ActiveConnection, and then update on everything, now to write their own how to update the SqlCommand), but the programmer's ability to control a bit stronger.




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.