Asp.net data operations three steps (1 ):)

Source: Internet
Author: User

I. Create a database connection in the class

It is best to make namespace
Using system. Data. sqlclient;
/// <Summary>
/// Summary description for DB
/// </Summary>
///
Namespace vote
{
Public class DB

{
Public static sqlconnection createconnection ()
{
Sqlconnection con = new sqlconnection ("Server = gyykf; database = vote; uid = sa; Pwd = sa ;");
Return con;
}
}
}

Ii. Bind a database

First open the database:
Sqlconnection con = class1.createconnection ();
Con. open ();
Then use the command to extract the data:
For example:
Sqlcommand itemcmd = new sqlcommand ("select votedetailsid, voteitem from votedetails where voteid =" + this. voteid, con );
Sqldatareader SDR = itemcmd. executereader ();
This. rbtn. datasource = SDR; // set the data source
This. rbtn. datatextfield = "voteitem ";
This. rbtn. datavaluefield = "votedetailsid"; // bind the property
This. rbtn. databind (); // binds data to the control.
SDR. Close ();
Con. Close (); // close the connection

Iii. update the database
First open the database:
Sqlconnection con = class1.createconnection ();
Con. open ();
Then run the following command:
Sqlcommand cmd = new sqlcommand ();
Cmd. Connection = con;
Cmd. commandtext = "Update votedetails set votenum = votenum + 1 where id = 1;
Cmd. executenonquery (); // update
Con. Close ();

I wrote so much for the first time. I will continue tomorrow ......

Related Article

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.