ASP. NET call stored Procedure 2

Source: Internet
Author: User
Tags connectionstrings

Create a stored procedure with only input parameters

CREATE PROCEDURE Proc_user
@name varchar (20),
@Password varchar (100)
as
SELECT * FROM Loginuser
Where name like @name

---Create a stored procedure with input and output parameters

CREATE PROCEDURE Proc_usertext
@name varchar (20),
@blog varchar () output
as
Select @blog = blog from loginuser where name = @name

---Create a stored procedure with input and return parameters

Create PROCEDURE ReturnVal
@name varchar (100),
@blog varchar () output
As
Begin
Select @blog = blog from loginuser where name = @name
if (@blog is null)
Set @blog = ' You haven't applied for a blog '
Return execute (@blog)--Conversion of data number type
End

Calls in ASP.

protected void Page_Load (object sender, EventArgs e)
{
DataBind ();
}

private void DataBind ()
{
The notation for stored procedures with input parameters only.
SqlConnection con = new SqlConnection (configurationmanager.connectionstrings["strconnection"]. ConnectionString);
SqlCommand cmd = new SqlCommand ("Proc_user", con);
Cmd.commandtype = CommandType.StoredProcedure;
Cmd. Parameters.addwithvalue ("@name", "l%");
Cmd. Parameters.addwithvalue ("@Password", "lcs9702");
Con. Open ();
SqlDataReader dr = cmd. ExecuteReader ();
Dropdownlist1.datasource = Dr;
Dropdownlist1.datatextfield = "name";
Dropdownlist1.datavaluefield = "use_id";
Dropdownlist1.databind ();
}

private void Textdatabind ()
{
The notation of the stored procedure with input and output parameters.
SqlConnection con = new SqlConnection (configurationmanager.connectionstrings["strconnection"]. ConnectionString);
SqlCommand cmd = new SqlCommand ("Proc_usertext", con);
Cmd.commandtype = CommandType.StoredProcedure;
Cmd. Parameters.Add ("@name", "Love");
Cmd. Parameters.Add ("@blog", sqldbtype.varchar,100);
Cmd. parameters["@blog"]. Direction = ParameterDirection.Output;
Cmd. Parameters.Add (New SqlParameter ("@blog", sqldbtype.varchar,100,parameterdirection.output,false,0,0,string. Empty,datarowversion.default,null));
Cmd.commandtype = CommandType.StoredProcedure;
Con. Open ();
Cmd. ExecuteNonQuery ();
TextBox1.Text = cmd. parameters["@blog"]. Value.tostring ();
Con. Close ();

}

private void Returndatabind ()
{
The notation of the stored procedure with input, which returns parameters.
SqlConnection con = new SqlConnection (configurationmanager.connectionstrings["strconnection"]. ConnectionString);
SqlCommand cmd = new SqlCommand ("ReturnVal", con);
Cmd.commandtype = CommandType.StoredProcedure;
Cmd. Parameters.Add ("@name", "lcs9702");
Cmd. Parameters.Add (New SqlParameter ("@blog", SqlDbType.VarChar, ParameterDirection.Output, False, 0, 0, string. Empty, Datarowversion.default, null));
Cmd.commandtype = CommandType.StoredProcedure;
Con. Open ();
Cmd. ExecuteNonQuery ();
TextBox2.Text = cmd. parameters["@blog"]. Value.tostring ();
Con. Close ();
}

protected void Button1_Click (object sender, EventArgs e)
{
Textdatabind ();
}

protected void button2_click (object sender, EventArgs e)
{
Returndatabind ();
}

ASP. NET call stored Procedure 2

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.