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 ,,,