如何在asp.net中的代码bihand文件中将参数传递给SP [英] how to pass a parameter to a SP in code bihand file in asp.net

查看:105
本文介绍了如何在asp.net中的代码bihand文件中将参数传递给SP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码隐藏文件中使用此代码。

I am using this code in code behind file.

Imports System.Data
Imports System.Data.Sqlclient

Partial Class ProductDetails
  Inherits System.Web.UI.Page

  Public strProductCode As String
  Public ConnectionString As String

  Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    strProductCode = Request.QueryString("type")
    ConnectionString = System.Configuration.ConfigurationManager.AppSettings("StagingConnectionString")

    Me.SqlDataSource1.ConnectionString = ConnectionString
    'Me.SqlDataSource1.SelectCommand = "SELECT * FROM tblFinishedProd WHERE (strProductCode = " & strProductCode & ")"

'this worked fine.
'now I want run that code by using SP

Me.SqlDataSource1.SelectCommand = "spGetProductDetails"
Me.SqlDataSource1.SelectCommandType = SqlDataSourceCommandType.StoredProcedure

'SqlDataSource1: stored proc paramter = strProductCode
   
'How to set the paramter for the SqlDataSource ????

    Me.SqlDataSource1.DataBind()
End Sub




--** Stored Proc (piece) **
alter PROCEDURE spGetProductDetails (@ProductCode as nvarchar(50))





Thx for你帮忙!



Thx for you help!

推荐答案

像这样使用



Use like this

sqlDataSource1.SelectCommand = "spGetProductDetails";
sqlDataSource1.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
 
sqlDataSource1.SelectParameters.Add("code",TypeCode.String, "1");
sqlDataSource1.SelectParameters[0].Direction =ParameterDirection.Input; 
 
GridView1.DataSource = sqlDataSource1;
GridView1.DataBind();









Check这是获取更多信息...

http://forums.asp.net/t /1115044.aspx/1 [ ^ ]


cmd.Parameters.AddWithValue("@parameterName",parameterValue);


hi,





SqlCommand sqlCmd = new SqlCommand("[usp_PBCustomWorkFlowJobsList_GroupBy]", sqlcon);
       sqlCmd.CommandType = CommandType.StoredProcedure;
       SqlDataAdapter dataAdapter = new SqlDataAdapter();
       SqlParameter[] sqlparam = new SqlParameter[1];
       sqlparam[0] = new SqlParameter("UserId", SqlDbType.Int);
       sqlparam[0].Value = 148291;
       sqlCmd.Parameters.Add(sqlparam[0]);






or

con.Open();
           SqlCommand cmd = new SqlCommand("sp_questions", con);
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.Add(new SqlParameter("@question", "sai"));
           cmd.Parameters.Add(new SqlParameter("@answer", 2));
           cmd.Parameters.Add(new SqlParameter("@option1", "saddf"));
           cmd.Parameters.Add(new SqlParameter("@option2", "sagdf"));
           SqlDataReader rdr = cmd.ExecuteReader();
           con.Close();


这篇关于如何在asp.net中的代码bihand文件中将参数传递给SP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆