Insert basic dB operations for C]

Source: Internet
Author: User

JingFrequently usedRecordDon't look for troubleAnnoyingBytes(-_-There are already too many other

 

Compiled a database and a table. It can be compiled anyway ~

I will not discuss it here. The same person in the Tao is also clear.
For how to store dB links in the config file, refer to here
Http://www.cnblogs.com/Kenr/archive/2009/04/27/1444610.html

/// <Summary>

/// Sample generationCode(WriteAmoeba ~)

/// </Summary>

/// <Param name = "studentid">Student ID</Param>

/// <Param name = "studentname">Student name</Param>

Private VoidDbinsert (StringStudentid,StringStudentname)

{

SqlconnectionConnection =Null;

null ;

progress process, when insertion fails failed hour can return Roll

SqltransactionTransaction =Null;

Try

{

string . empty;

// tune config dB in the file chain connected string

Connection =New Sqlconnection(Configurationmanager. Deleetask ["DB. connectionstring"]);

Connection. open ();

Command = connection. createcommand ();

Transaction = connection. begintransaction ();

Command. Transaction = transaction;

Commtext + ="Insert into <dbname>. <schema>. <tablename>";

Commtext + ="(Student_id, student_name, last_update )";

Commtext + ="Select @ studentid, @ studentname, @ lastupdate";

Command. commandtext = commtext;

Command. Parameters. Add (New Sqlparameter("@ Studentid","<Field 1>"));

Command. Parameters. Add (New Sqlparameter("@ Studentname","<Field 2>"));

Command. Parameters. Add (New Sqlparameter("@ Lastupdate", System.Datetime. Now. tostring ()));

//Returns the number of inserted rows.

IntInsertcount = command. executenonquery ();

Transaction. Commit ();

}

Catch(ExceptionE)

{

//SendingRawErrorBackRoll

If(Transaction! =Null)

{

Transaction. rollback ();

}

//SetErrorThrowing to the previousLayer

ThrowE;

}

//Never forgetNote,DisconnectConnect

Finally

{

If(Connection! =Null)

{

Connection. Close ();

}

}

}

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.