如何使用c#在asp.net中调用存储过程 [英] how to call store procedure in asp.net using c#

查看:48
本文介绍了如何使用c#在asp.net中调用存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

USE [安全]

GO

/ ******对象:StoredProcedure [dbo]。[考试]剧本日期:10/15/2013 23 :15:03 ****** /

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

ALTER程序[dbo]。[考试]

@type varchar(50)= null,

@user_name varchar(50)= null,

@email_id varchar(50)= null,

@password varchar(50)= null,

@exp varchar(50) = null

as

开始

if(@ type ='insert')

begin



插入user_info值(@ user_name,@ email_id,@ password,@ exp)

end

if(@type ='select')

开始



如果存在(从user_info中选择@ email_id,@密码,其中email_id = @ email_id和密码= @密码)

从user_info选择@ email_id,@密码,其中email_id = @ email_id和密码= @密码

结束

end

解决方案

这个创建,使用和删除存储过程:

  string  r; 
string s;
使用(SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
使用(SqlCommand create = new SqlCommand( CREATE PROC [dbo] .Sample @Name varchar(100)OUTPUT AS BEGIN SELECT @ Name = Username FROM myTable WHERE iD = 1 END,con ))
{
create.ExecuteNonQuery();
}
使用(SqlCommand com = new SqlCommand( 示例,con))
{
com.CommandType = CommandType.StoredProcedure;
SqlParameter name = new SqlParameter( @名称,SqlDbType.VarChar, 100 );
name.Direction = ParameterDirection.Output;
com.Parameters.Add(name);
com.ExecuteNonQuery();
r =( string )name.Value;
s =( string )com。参数[ @Name]值。
}
使用(SqlCommand drop = new SqlCommand( DROP PROCEDURE [dbo] .Sample,con))
{
drop.ExecuteNonQuery() ;
}
con.Close();
}
Console.WriteLine( 名称:{0} \ nParam:{1} ,r,s);


这是为了班级。

 使用 Microsoft.ApplicationBlocks.Data; 
使用 Microsoft.ApplicationBlocks.ExceptionManagement;

public string connection()
{
return ConfigurationSettings.AppSettings [ connection ]。
}

public SqlDataReader GetData( string 类型, string username, string email, string 密码,< span class =code-keyword> string exp)
{
SqlDataReader sqlReader = SqlHelper.ExecuteReader(connection(),CommandType.StoredProcedure, 考试
new SqlParameter( @ type,type),
new SqlParameter( @ username,username),
SqlParameter( @ email,email),
SqlParameter( @ password,密码),
new SqlParameter( @ exp,exp));
return sqlReader;
}





这是最简单的方法。希望这有所帮助。


在SQL中创建存储过程并在代码中调用它。



public void methodName(string parameter1 ,string parameter2)

{

SqlConnection con = new SqlConnection(Data Source = server_name; Initial Catalog = Database_name; User ID = username; Password = password);

con.Open();

SqlConnection.ClearAllPools();



SqlCommand cmd = new SqlCommand(存储过程名称,con);

cmd.CommandType = CommandType.StoredProcedure;



cmd.Parameters.Add(new SqlParameter( @ param1,SqlDbType.VarChar,50));

cmd.Parameters [0] .Value = parameter1;



cmd.Parameters .Add(new SqlParameter(@ param2,SqlDbType.VarChar,50));

cmd.Parameters [1] .Value = parameter2;



SqlDataReader rdr = cmd.ExecuteReader();





返回rdr.Read();



con.Close();

}

USE [security]
GO
/****** Object: StoredProcedure [dbo].[exam] Script Date: 10/15/2013 23:15:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[exam]
@type varchar(50)=null,
@user_name varchar(50)=null,
@email_id varchar(50)=null,
@password varchar(50)=null,
@exp varchar(50)=null
as
begin
if(@type='insert')
begin

insert into user_info values(@user_name,@email_id,@password,@exp)
end
if(@type='select')
begin

if exists(select @email_id,@password from user_info where email_id=@email_id and password=@password)
select @email_id,@password from user_info where email_id=@email_id and password=@password
end
end

解决方案

This creates, uses and removes a stored procedure:

string r;
string s;
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand create = new SqlCommand("CREATE PROC [dbo].Sample @Name varchar(100) OUTPUT AS BEGIN SELECT @Name=Username FROM myTable WHERE iD=1 END", con))
        {
        create.ExecuteNonQuery();
        }
    using (SqlCommand com = new SqlCommand("Sample", con))
        {
        com.CommandType = CommandType.StoredProcedure;
        SqlParameter name = new SqlParameter("@Name", SqlDbType.VarChar, 100);
        name.Direction = ParameterDirection.Output;
        com.Parameters.Add(name);
        com.ExecuteNonQuery();
        r = (string) name.Value;
        s = (string) com.Parameters["@Name"].Value;
        }
    using (SqlCommand drop = new SqlCommand("DROP PROCEDURE [dbo].Sample", con))
        {
        drop.ExecuteNonQuery();
        }
    con.Close();
    }
Console.WriteLine("Name : {0}\nParam: {1}", r, s);


this is for class.

using Microsoft.ApplicationBlocks.Data;
using Microsoft.ApplicationBlocks.ExceptionManagement;

public string connection()
{
    return ConfigurationSettings.AppSettings["connection"];
}

public SqlDataReader GetData(string type, string username, string email, string password, string exp)
{
    SqlDataReader sqlReader = SqlHelper.ExecuteReader(connection(), CommandType.StoredProcedure, "exam",
        new SqlParameter("@type", type),
        new SqlParameter("@username, username"),
        new SqlParameter("@email", email),
        new SqlParameter("@password", password),
        new SqlParameter("@exp", exp));
    return sqlReader;
}



this is the most simplest way to do it. Hope this helped.


Create the store procedure in SQL and call this in your code.

public void methodName(string parameter1,string parameter2)
{
SqlConnection con = new SqlConnection("Data Source=server_name;Initial Catalog=Database_name;User ID=username;Password=password");
con.Open();
SqlConnection.ClearAllPools();

SqlCommand cmd = new SqlCommand("Stored Procedure Name", con);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(new SqlParameter("@param1", SqlDbType.VarChar, 50));
cmd.Parameters[0].Value = parameter1;

cmd.Parameters.Add(new SqlParameter("@param2", SqlDbType.VarChar, 50));
cmd.Parameters[1].Value = parameter2;

SqlDataReader rdr = cmd.ExecuteReader();


return rdr.Read();

con.Close();
}


这篇关于如何使用c#在asp.net中调用存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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