使用ASP.NET选择SQL Server存储过程 [英] Select SQL server stored procedures using ASP.NET

查看:63
本文介绍了使用ASP.NET选择SQL Server存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的程序



  CREATE   PROCEDURE  [dbo]。[搜索客户] 

@ cusCode varchar 10 ),
@ Status int

AS
BEGIN

IF @ Status = 0

选择 * tblCustomer 其中 cusCode = @ cusCode


END







Asp Code



 字符串 procedureName; 
CommandType cmdType;
列表< SqlParameter> parameters = new List< SqlParameter>();

// 设置程序属性
procedureName = 搜索客户;
cmdType = CommandType.StoredProcedure;


parameters.Add( new SqlParameter( @ cusCode,SqlDbType.VarChar,( 30 ))); // 0
parameters.Add( new SqlParameter( @ Status,SqlDbType.Int)); // 1

参数[ 0 ]。Value = txtcusCode.Text; < span class =code-comment> // 0;
参数[ 1 ]。值= 0 ;

Boolean status1 = Common.executeProcedure(procedureName,cmdType,parameters);

String CusCode = txtcusCode.Text;
if (status1 == true
{
if (CusCode == 001
{
DropDown_Customer.SelectedValue = cusDataSet.Rows [ 0 ] [ 0 ]。的ToString();
}

else
{

DropDown_Customer.SelectedValue = cusDataSet.Rows [< span class =code-digit> 0 ] [ 0 ]。ToString();
dropdowncutomer.SelectedItem.Text = cusDataSet.Rows [ 0 ] [ 1 ]。ToString( );
txtcustomerName.Text = cusDataSet.Rows [ 0 ] [ 1 ]。ToString();



}
}







我的asp代码没有工作???谁能帮助我解决这个问题?



我尝试过的事情:



Quote:

String strConnString = ConfigurationManager.ConnectionStrings [conString]。ConnectionString;

SqlConnection con = new SqlConnection(strConnString);

SqlCommand cmd = new SqlCommand();

cmd.CommandType = CommandType.StoredProcedure;

cmd.CommandText = 搜索客户;

cmd.Connection = con;

尝试

{

con.Open() ;

DropDown_Customer.DataSource = cmd.ExecuteReader();

DropDown_Customer.DataTextField =cusName;

DropDown_Customer.DataValueField =cusCode ;

DropDown_Customer.DataBind();

DropDown_Customer.Items.Insert(0,new ListItem(Select));

}

catch(例外情况)

{

抛出ex;

}

终于

{

con.Close();

con.Dispose();

}

解决方案

Hi Guys



检查这个有用的资源,使用ASP.NET选择SQL Server存储过程。我认为它对你有帮助。



选择使用ASP.NET MVC4中的存储过程插入更新和删除 [ ^ ]



ASP.NET存储过程 [ ^ ]


< pre lang =C#> 字符串 strConnString = ConfigurationManager.ConnectionStrings [ AntronERP_DBConnectionString2]的ConnectionString。
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = 搜索客户;
cmd.Parameters.Add( @ cusCode,SqlDbType.NChar).Value = txtcusCode.Text.Trim();
cmd.Parameters.Add( @ Status,SqlDbType.Int).Value = 0 ;
cmd.Connection = con;
尝试
{
con.Open();
DropDown_Customer.DataSource = cmd.ExecuteReader();
DropDown_Customer.DataTextField = cusName;
DropDown_Customer.DataValueField = cusCode;
DropDown_Customer.DataBind();
// DropDown_Customer.Items.Insert(0,new ListItem(Select));
}
catch (例外情况)
{
throw ex;
}
最后
{
con.Close();
con.Dispose();
}


This my Procedure

CREATE PROCEDURE [dbo].[Search Customer]       
(
 @cusCode varchar(10),
 @Status int
 )
AS
BEGIN

	IF  (@Status = 0)
	
	Select * from tblCustomer where cusCode=@cusCode
	

END




Asp Code

String procedureName;
           CommandType cmdType;
           List<SqlParameter> parameters = new List<SqlParameter>();

           //setting procedure properties
           procedureName = "Search Customer";
           cmdType = CommandType.StoredProcedure;


           parameters.Add(new SqlParameter("@cusCode", SqlDbType.VarChar, (30)));//0
           parameters.Add(new SqlParameter("@Status", SqlDbType.Int));//1

           parameters[0].Value = txtcusCode.Text;//0;
           parameters[1].Value = 0;

           Boolean status1 = Common.executeProcedure(procedureName, cmdType, parameters);

           String CusCode =txtcusCode.Text;
           if (status1 == true)
           {
               if (CusCode == "001")
                  {
                  DropDown_Customer.SelectedValue = cusDataSet.Rows[0][0].ToString();
                  }

 else
        {

        DropDown_Customer.SelectedValue = cusDataSet.Rows[0][0].ToString();
        dropdowncutomer.SelectedItem.Text = cusDataSet.Rows[0][1].ToString();
        txtcustomerName.Text = cusDataSet.Rows[0][1].ToString();
         


          }
           }




my asp code not working ??? anyone can help me to slove this??

What I have tried:

Quote:

String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Search Customer";
cmd.Connection = con;
try
{
con.Open();
DropDown_Customer.DataSource = cmd.ExecuteReader();
DropDown_Customer.DataTextField = "cusName";
DropDown_Customer.DataValueField = "cusCode";
DropDown_Customer.DataBind();
DropDown_Customer.Items.Insert(0, new ListItem("Select"));
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}

解决方案

Hi Guys

Check this useful resources to Select SQL server stored procedures using ASP.NET.I think it will helpful for you.

Select Insert Update and Delete using Stored Procedure in ASP.NET MVC4[^]

ASP.NET Stored Procedures[^]


String strConnString = ConfigurationManager.ConnectionStrings["AntronERP_DBConnectionString2"].ConnectionString;
            SqlConnection con = new SqlConnection(strConnString);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Search Customer";
            cmd.Parameters.Add("@cusCode", SqlDbType.NChar).Value = txtcusCode.Text.Trim();
            cmd.Parameters.Add("@Status", SqlDbType.Int).Value = 0;
            cmd.Connection = con;
            try
            {
                con.Open();
                DropDown_Customer.DataSource = cmd.ExecuteReader();
                DropDown_Customer.DataTextField = "cusName";
                DropDown_Customer.DataValueField = "cusCode";
                DropDown_Customer.DataBind();
                //DropDown_Customer.Items.Insert(0, new ListItem("Select"));
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
                con.Dispose();
            }


这篇关于使用ASP.NET选择SQL Server存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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