C # example of using Winform to prevent SQL Injection for user logon,

Source: Internet
Author: User

C # example of using Winform to prevent SQL Injection for user logon,

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using System. data. sqlClient;

Namespace OmyGod {public partial class Form1: Form {private static string connectionString = "Data Source = .; initial Catalog = Omy; Integrated Security = True "; public Form1 () {InitializeComponent ();}

Enum message {

Username or password input error = 1, logon successful = 2,

}

Public bool check (string name, string pass) {using (SqlConnection conn = new SqlConnection (connectionString) {conn. open (); SqlCommand cmd = new SqlCommand (); cmd. connection = conn; cmd. commandText = "select * from auser where name = @ name and pass = @ pass"; cmd. parameters. addRange (new SqlParameter [] {new SqlParameter ("@ name", SqlDbType. varChar) {Value = this. name. text}, new SqlParameter ("@ pass", SqlDbType. varChar) {Value = this. pass. text },}); cmd. executeNonQuery (); SqlDataAdapter ada = new SqlDataAdapter (cmd); DataSet ds = new DataSet (); ada. fill (ds); // return ds; DataSet data = ds; if (data. tables [0]. rows. count = 0) {MessageBox. show (message. incorrect username or password ). toString ();} else {

Index mm = new index (); mm. show (); this. close (); // MessageBox. show (message. login successful ). toString ();} return false;

}

}

// Log on to private void button#click (object sender, EventArgs e) {string name = this. name. text; string pass = this. pass. text; check (name, pass );}

Private void button2_Click (object sender, EventArgs e) {this. Close ();}

}}

 

 

This is just a simple anti-SQL injection method, but it is not comprehensive anti-SQL injection ,,,

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.