C # Parameterized query database, preventing injection of __ database

Source: Internet
Author: User
Tags readline

Simple landing program to demonstrate the parameterized query, involving database operation should not put the user input directly with the SQL statement splicing, user input information is always unsafe,



Namespace Democonsoleapplication 02 {
Class Program
04 {
05//Database connection string, based on actual modification
The private const string ConnectionString = @ "Data source=| Datadirectory|\database1.sdf ";
07
The static void Main (string [] args)
09 {
10//Get the user input content
One Console. WriteLine ("Please enter user name");
A string Passport = Console. ReadLine ();
The Console. WriteLine ("Please enter the password");
String Password = Console. ReadLine ();
15
using (SqlConnection Conn = new SqlConnection (ConnectionString))
17 {
Conn.  Open (); Open Database
The using (SqlCommand CMD = Conn. CreateCommand ())
20 {
Cmd. CommandText = "Select * from Tb_users where passport= @UN and password= @PWD";
Cmd. Parameters. ADD (New SqlParameter ("UN", Passport);
To CMD. Parameters. ADD (New SqlParameter ("PWD", Password));
24
if (1 = = Cmd. ExecuteNonQuery ())
Console. WriteLine ("Landing success". ");
/Else
Console. WriteLine ("Landing failed.") ");
29}
30}
31
Console. Readkey (); Prevents the console program from flashing and not seeing the output
33}
34}
35}

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.