C # simple instance for calling the SQL Server Stored Procedure

Source: Internet
Author: User
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 storetest {public partial class form1: FORM {public form1 () {initializecomponent ();} private void btn_storeinvoke_click (Object sender, eventargs E) {string conn = "Server = .; database = testsd; uid = sa; Pwd = j6394126 "; sqlconnection = new sqlconnection (conn); sqlcommand cmd = new sqlcommand (); cmd. connection = sqlconnection; // set the SQL connection cmd. commandtext = "getalldata"; // set the name of the calling Stored Procedure cmd. commandtype = commandtype. storedprocedure; // specify the execution statement as the Stored Procedure sqldataadapter da = new sqldataadapter (CMD); dataset DS = new dataset (); DA. fill (DS); // fill dataset This. dgv_storeview.datasource = Ds. tables [0]; // dgv_storeview.databind (); this sentence is required only for Asp.net. After winform is set to the data source, no need to bind it.} private void btn_storenoinoutparam_click (Object sender, eventargs e) {string conn = "Server = .; database = testsd; uid = sa; Pwd = j6394126 "; sqlconnection sqlconn = new sqlconnection (conn); sqlcommand cmd = new sqlcommand (); cmd. connection = sqlconn; cmd. commandtext = "insertinfo"; cmd. commandtype = commandtype. storedprocedure; sqlconn. open (); this.txt _ storenoinout. TEXT = "the stored procedure has an impact:" + cmd. executenonquery (). tostring () + "row"; sqlconn. close ();} private void btn_test3_click (Object sender, eventargs e) {string conn = "Server = .; database = testsd; uid = sa; Pwd = j6394126 "; sqlconnection sqlconn = new sqlconnection (conn); sqlcommand cmd = new sqlcommand (); cmd. connection = sqlconn; cmd. commandtext = "inouttest"; cmd. commandtype = commandtype. storedprocedure; // set the Stored Procedure cmd. parameters. add ("@ studentid", sqldbtype. varchar); // set the default input parameter value cmd. parameters. add ("@ studentname", sqldbtype. varchar, 50); // set the default output parameter; otherwise, the default size is 0 cmd. parameters. add ("@ studentclass", sqldbtype. varchar, 50); // set the default output parameter; otherwise, the default size is 0 cmd. parameters. add ("@ studentscores", sqldbtype. INT); // set the default output parameter. Otherwise, the default size is 0 cmd. parameters. add ("@ rowcount", sqldbtype. INT); // set the default output parameter. Otherwise, the default size is 0 cmd. parameters ["@ studentid"]. value = 2; // The default parameter value is cmd. parameters ["@ studentname"]. direction = parameterdirection. output; // set the parameter to the output parameter. Otherwise, the input parameter CMD is used by default. parameters ["@ studentclass"]. direction = parameterdirection. output; // set the parameter to the output parameter. Otherwise, the input parameter CMD is used by default. parameters ["@ studentscores"]. direction = parameterdirection. output; // set the parameter to the output parameter. Otherwise, the input parameter CMD is used by default. parameters ["@ rowcount"]. direction = parameterdirection. returnvalue; // the operation that is set to return value // either of the following methods is commented out. // idataparameter [] parameters = {// new sqlparameter ("@ studentid ", sqldbtype. varchar), // new sqlparameter ("@ studentname", sqldbtype. varchar, 50), // new sqlparameter ("@ studentclass", sqldbtype. varchar, 50), // new sqlparameter ("@ studentscores", sqldbtype. INT), // new sqlparameter ("@ rowcount", sqldbtype. INT) //}; // cmd. parameters. add (parameters [0]); // cmd. parameters. add (parameters [1]); // cmd. parameters. add (parameters [2]); // cmd. parameters. add (parameters [3]); // cmd. parameters. add (parameters [4]); // parameters [0]. value = 2; // parameters [1]. direction = parameterdirection. output; // set the parameter to the output parameter. Otherwise, the default value is the input parameter // parameters [2]. direction = parameterdirection. output; // set the parameter to the output parameter. Otherwise, the default value is the input parameter // parameters [3]. direction = parameterdirection. output; // set the parameter to the output parameter. Otherwise, the default value is the input parameter // parameters [4]. direction = parameterdirection. returnvalue; // set to return value to operate sqlconn. open (); cmd. executenonquery (); this.txt _ name. TEXT = cmd. parameters ["@ studentname"]. value. tostring (); // obtain the name this.txt _ class with the ID of 3. TEXT = cmd. parameters ["@ studentclass"]. value. tostring (); // obtain this.txt _ socres of the class with the ID of 3. TEXT = cmd. parameters ["@ studentscores"]. value. tostring () + "-" + "affected:" + cmd. parameters ["@ rowcount"]. value. tostring () + "row"; // obtain the score of ID 3 // this.txt _ name. TEXT = parameters [1]. value. tostring (); // get the name of ID 3 // this.txt _ class. TEXT = parameters [2]. value. tostring (); // obtain the class ID 3 // this.txt _ socres. TEXT = parameters [3]. value. tostring () + "-" + "affected:" + // parameters [4]. value. tostring () + "row"; // obtain the score sqlconn with the ID of 3. close ();}}}

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.