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 ......