C # ADO. NET parameter query

Source: Internet
Author: User

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Data.SqlClient;6 usingSystem.Drawing;7 usingSystem.Text;8 usingSystem.Windows.Forms;9 Ten namespaceAdotwo One { A      Public Partial classFlogin:form -     { -          PublicFlogin () the         { - InitializeComponent (); -         } -  +         Private voidBtnok_click (Objectsender, EventArgs e) -         { +             stringTname = TxtName.Text.Trim ();//a A             stringTpwd = TxtPwd.Text.Trim ();//b at             //determine whether the user name password is correct is actually to go to the database query there is no data, -             //This data is user name equal to user input username, -             //and -             //a password equals a single piece of data entered by the user -SqlConnection conn =NewSqlConnection ("server=.; Database=heimablog;uid=sa;pwd=suncoder"); -             stringSql=string. Format ("select COUNT (id) from Student where Name= ' {0} ' and pwd= ' {1} '", tname,tpwd); inSqlCommand cmd =NewSqlCommand (SQL, conn); -             //cmd. Connection = conn; to             //cmd.commandtext = ""; + Conn. Open (); -  the             //SqlDataReader dr = cmd. ExecuteReader (); *             //if (Dr. HasRows) $             //{Panax Notoginseng             //MessageBox.Show ("Landing Success"); -             //} the             //While (Dr. Read ()) +             //{ A             //Console.WriteLine (Dr[1]. ToString ());//+dr["Name"],DR. GetString (1)); the             //} +  -             ObjectOBG =cmd. ExecuteScalar (); $             intres =Convert.ToInt32 (OBG); $             if(Res >0) {MessageBox.Show ("Landing Success"); } -             Else{MessageBox.Show ("Login Failed"); } -         } the  -         Private voidBtnloginpara_click (Objectsender, EventArgs e)Wuyi         { theSqlConnection conn =NewSqlConnection ("server=.; Database=heimablog;uid=sa;pwd=suncoder"); -SqlCommand cmd =NewSqlCommand ("select COUNT (id) from Student where [email protected] and [email protected]", conn); Wu  -             //The first form of a notation About             //SqlParameter sp = new SqlParameter ("@ass", TxtName.Text.Trim ()); $             //SqlParameter SP2 = new SqlParameter ("@add", TxtPwd.Text.Trim ()); -  -             //cmd. Parameters.Add (SP); -             //cmd. Parameters.Add (SP2); A  +             //the second kind of writing, the special case error the             //cmd. Parameters.Add (New SqlParameter ("@ass", TxtName.Text.Trim ())); -             //cmd. Parameters.Add (New SqlParameter ("@add", TxtPwd.Text.Trim ())); $  the             //The Third kind of wording the             //sqlparameter[] Spa = new sqlparameter[]{ the             //New SqlParameter ("@ass", TxtName.Text.Trim ()), the             //New SqlParameter ("@add", TxtPwd.Text.Trim ()) -             //}; in             //cmd. Parameters.addrange (SPA); the  the             //The fourth kind of notation About             //cmd. Parameters.addwithvalue ("@ass", TxtName.Text.Trim ()); the             //cmd. Parameters.addwithvalue ("@add", TxtPwd.Text.Trim ()); the  the             //the fifth way, parameters can be output parameters +             //SqlParameter sp = new SqlParameter ("@ass", SqlDbType.NVarChar); -             //SqlParameter SP2 = new SqlParameter ("@add", SqlDbType.Char); the             //sp. Value = TxtName.Text.Trim ();Bayi             //SP2. Value = TxtPwd.Text.Trim (); the             //cmd. Parameters.Add (SP); the             //cmd. Parameters.Add (SP2); -  -             //The sixth kind of notation theSqlParameter sp =NewSqlParameter ("@ass", SqlDbType.NVarChar) {Value =TxtName.Text.Trim ()}; theSqlParameter SP2 =NewSqlParameter ("@add", SqlDbType.Char) {Value =TxtPwd.Text.Trim ()}; the cmd. Parameters.Add (SP); the cmd. Parameters.Add (SP2); -  the Conn. Open (); the  the MessageBox.Show (cmd. ExecuteScalar (). ToString ());94  the         } the     } the}

C # ADO. NET parameter query

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.