如何编写用于调用存储过程的C#代码? [英] How to write c# code for calling store procedure ?
本文介绍了如何编写用于调用存储过程的C#代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请告诉我如何用C#编写代码来调用此存储过程?
还请检查我的存储过程....是否正确?
实际上,我想要的是,我运行ID并单击按钮,然后它会在一个文本框中显示我的名字和姓氏,而在另一个文本框中显示我的年龄.
Please tell me that how I can write code in C# for calling this store procedure?
and please check my store procedure also.... it is correct or not?
actually i want that when i run than i write id and click on button than it show me first name+last name in one text box and age in another textbox.
alter PROCEDURE sproc
(
@Id int,
@First_Name varchar(50)=null,
@Last_Name varchar(50)=null,
@Age int=null
)
AS
SELECT First_Name + Last_Name,Age from store where Id=@Id;
GO
declare @First_Name as varchar(50)
declare @Last_Name as varchar(50)
declare @Age as int
declare @Id as int
exec sproc @First_Name=null,@Last_Name=null,@Age=null,@Id=''
select @First_Name + @Last_Name,@Age
非常感谢您的帮助.
编辑
已添加代码...
I would be very much thankful for your help.
EDIT
Code Added...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace demo_stored_procedure
{
public partial class demooooo : System.Web.UI.Page
{
DataSet ds = new DataSet();
SqlConnection con;
//Here we declare the parameter which we have to use in our application
SqlCommand cmd = new SqlCommand();
//SqlParameter sp1 = new SqlParameter();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
con = new SqlConnection("Data Source=NSEZ-DD4-028;Initial Catalog=Hima;Integrated Security= SSPI");
cmd = new SqlCommand("sproc", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Id", SqlDbType.VarChar).Value = TextBox1.Text;
cmd.Parameters.Add("@First_Name", SqlDbType.VarChar).Value = TextBox2.Text;
// cmd.Parameters.Add("@Last_Name", SqlDbType.VarChar).Value = TextBox2.Text;
cmd.Parameters.Add("@Age", SqlDbType.VarChar).Value = TextBox3.Text;
SqlParameter parameter = new SqlParameter("");
parameter.Direction = ParameterDirection.Output;
cmd.Parameters.Add(parameter);
con.Open();
cmd.Connection = con;
string outResult = parameter.Value.ToString();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
推荐答案
您需要阅读以下内容:
MSDN:ADO.NET [使用ADO.NET访问数据 [
You need to read these:
MSDN: ADO.NET[^]
Accessing Data with ADO.NET[^]
你好
这是存储过程
Hi
here is Stored procedure
create procedure test
@id int
As
begin
SET NOCOUNT ON;
SELECT (FirstName+' '+LastName) as name,age from Store where id=@id
END
并将此代码写在按钮上,点击
And write this code on the button click
protected void Button1_Click(object sender, EventArgs e)
{
Sqlconnection con=new SqlConnection("Your con string");
Sqlcommand cmd=new SqlCommand("test",con);
cmd.CommandType=CommandType.StoredProcedure;
SqlParameter[] param = new SqlParameter[1];
param[0]=new SqlParameter("@id", SqlDbType.Int);
param[0].value=TextBox1.text;
cmd.parameters.addRange(param);
SqlDataReader dr=cmd.ExecuteReader();
con.open();
while(dr.read())
{
TextBox2.Text=Convert.Tostring(dr["name"]);
TextBox3.Text=Convert.Tostring(dr["age"]);
}
con.close();
}
thnq太多了:-D
现在可以正常工作了:):)
thnq u soo much :-D
its working now :) :)
这篇关于如何编写用于调用存储过程的C#代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文