Database Operations (anti-injection attacks)

Source: Internet
Author: User

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

namespace ConsoleApplication1
{
Class Program
{
static void Main (string[] args)
{
Console.WriteLine ("Please enter the vehicle code to be queried:");
String code = Console.ReadLine ();
SqlConnection conn = new SqlConnection ("server=.; database=mydb;user=sa;pwd=1023823348 ");
SqlCommand cmd = conn. CreateCommand ();
cmd.commandtext = "SELECT * from car where [email protected]";
cmd. Parameters.clear ();
cmd. Parameters.addwithvalue ("@code", code);
Conn. Open ();
SqlDataReader dr = cmd. ExecuteReader ();
if (Dr. HasRows)
{
While (Dr. Read ())
{
Console.WriteLine (Dr[0] + "--" + dr[1]);
Console.WriteLine ("Input 1 Delete this data input 2 modify data input 3 Add data");
}
int a = Int. Parse (Console.ReadLine ());
Dr. Close ();
if (a = = 1)
{
cmd.commandtext = "Delete from car where [email protected]";
cmd. ExecuteNonQuery ();
Console.WriteLine ("Delete succeeded! ");
}
else if (a = = 2)
{
Console.WriteLine ("Please enter Content");
String name = Console.ReadLine ();
cmd.commandtext = "Update car set [email protected] where [email protected]";
cmd. Parameters.clear ();
cmd. Parameters.addwithvalue ("@name", name);
cmd. Parameters.addwithvalue ("@code", code);
cmd. ExecuteNonQuery ();
Console.WriteLine ("Modified successfully! ");
}
else if (a = = 3)
{
Console.WriteLine ("Please enter");
string code2 = Console.ReadLine ();
Console.WriteLine ("Please enter the content name");
String name = Console.ReadLine ();
Console.WriteLine ("Please enter serial number");
string brand = Console.ReadLine ();
Console.WriteLine ("Please enter the content date");
string time = Console.ReadLine ();
Console.WriteLine ("Please enter content fuel consumption *.**");
double oil =double. Parse (Console.ReadLine ());
Console.WriteLine ("Please enter the content horsepower");
int powers = Int. Parse (Console.ReadLine ());
Console.WriteLine ("Please enter Content");
int exhaust = Int. Parse (Console.ReadLine ());
Console.WriteLine ("Please enter Content");
double Price =double. Parse (Console.ReadLine ());
Console.WriteLine ("Please enter Content");
string pic = Console.ReadLine ();

Cmd.commandtext = "INSERT into car values (@code, @name, @brand, @time, @oil, @powers, @exhaust , @price, @pic)";
cmd. Parameters.clear ();
cmd. Parameters.addwithvalue ("@code", Code2);
cmd. Parameters.addwithvalue ("@name", name);
cmd. Parameters.addwithvalue ("@brand", brand);
cmd. Parameters.addwithvalue ("@time", time);
cmd. Parameters.addwithvalue ("@oil", oil);
cmd. Parameters.addwithvalue ("@powers", Powers);
cmd. Parameters.addwithvalue ("@exhaust", exhaust);
cmd. Parameters.addwithvalue ("@price", price);
cmd. Parameters.addwithvalue ("@pic", pic);
cmd. ExecuteNonQuery ();
Console.WriteLine ("added successfully! ");
}
Else
{
Console.WriteLine ("Error");
}
}
Else
{
Console.WriteLine ("No corresponding data found");
}

Conn. Close ();
console.readline ();

}
}

}

Database Operations (anti-injection attacks)

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.