Transaction----1

Source: Internet
Author: User

1. Use the command parameters:
Cmd.commandtext = "[email protected] ...";
Cmd. Parameters.clear ();
Cmd. Parameters.addwithvalue ("@uid", "value")
Cmd. Executexxxxx ();

2.ExecuteScalar () Executes the SQL statement, returning the value in the first row column.
It is typically used to execute queries for aggregate functions.

3. Transaction transaction
Acid-a Atomic nature. C consistency. I-isolation. D persistence.
Two types of transactions:
(i) Intra-link transactions: Use transactions to control the execution of multiple statements after a link is opened.
1. Create a transaction object.
Sqltransanction trans = conn. BeginTransaction (); Note must be written after the link is opened
2. Hang the transaction object on the Command object, and let the command perform the function with the transaction.
Cmd. Transaction = trans;

3. Follow the previous command to operate the database
A. After success, call the Commit () method of the transaction object to submit the result of the execution.
B. The rollback () method of the transaction object is called after failure to roll back the state before the execution.

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Data;
Using System.Data.SqlClient;

Namespace Lian_commandbehavior
{
class Lian_shiwu
{
Public const string AB = "server=.; Database=wo0505;uid=sa;pwd=123 ";
public static void Main11 (string[] args)
{
Show ();
Shanchu ();
Show ();
Console.ReadLine ();
}
public static void Shanchu ()//delete the corresponding information with the foreign key
{
Console.Write ("Enter the number of the person to be deleted");
String co = Console.re Adline ();
String ok = "";
OK = Yancode (CO);
if (ok = = "Cun")
{
SqlConnection con = new SqlConnection (AB);
Try
{
Con. Open ();
SqlCommand cmd = con. CreateCommand ();
SqlTransaction tran = con. BeginTransaction ();
Cmd. Transaction = Tran;
Try
{
Cmd.commandtext = "Delete from Family where [email protected]";
Cmd. Parameters.clear ();
Cmd. Parameters.addwithvalue ("@c", CO);
Cmd. ExecuteNonQuery ();


Cmd.commandtext = "Delete from work where [email protected]";
Cmd. Parameters.clear ();
Cmd. Parameters.addwithvalue ("@c", CO);
Cmd. ExecuteNonQuery ();


Cmd.commandtext = "Delete from Info where [email protected]";
Cmd. Parameters.clear ();
Cmd. Parameters.addwithvalue ("@c", CO);
Cmd. ExecuteNonQuery ();
Tran.commit ();
}
Catch
{
Console.WriteLine ("Delete is unsuccessful, return the data before execution!") ");
Tran. Rollback ();
}

}
Catch
{
Console.WriteLine ("Not deleted! ");
}
Finally
{
con. Close ();
}
}
Else {Console.WriteLine ("The number you entered does not exist!) "); }
}
public static string Yancode (string code)//Verify the existence of the appropriate information
{
String ok = "Cun";
SqlConnection con = new SqlConnection (AB);
Try
{
Con. Open ();
SqlCommand cmd = con. CreateCommand ();
Cmd.commandtext = "SELECT * from Info";
SqlDataReader dr = cmd. ExecuteReader (); The
while (Dr. Read ())
{
if (dr["Code"). ToString () ==code)
{
ok= "Cun";
}
Else {ok= "bu";}
}

}
Catch
{
Console.WriteLine ("File Error! ");
}
Finally
{
Con. Close ();
}
return OK;
}
public static void Show ()//display all information for this table
{
SqlConnection con = new SqlConnection (AB);
Try
{
Con. Open ();
SqlCommand cmd = con. CreateCommand ();
Cmd.commandtext = "SELECT * from Info";
SqlDataReader dr = cmd. ExecuteReader ();
while (Dr. Read ())
{
String sex= ((bool) dr["Sex"])? " Male ":" Female ";
String date = ((DateTime) dr["Birthday"]). ToString ("yyyy mm month DD day");
Console.WriteLine (dr["Code"). ToString () + "\ T" + dr["Name"]. ToString () + "\ T" + sex + "\ T" + dr["Nation"]. ToString () + "\ T" + date);
}

}
catch (Exception)
{
Console.WriteLine ("File Error! ");
}
Finally
{
Con. Close ();
}
}
}
}

Transaction----1

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.