存储过程的调用
这个存储过程用winform怎么调用???????????create procedure usp_OutStudent
@name char(10),
@sex char(10) output
as
select @sex=性别 from mytable where 姓名=@name --------------------编程问答-------------------- private DataTable LoadData(int PageIndex)
{
SqlConnection con = new SqlConnection(this.ConnectStr());
SqlCommand cmd = new SqlCommand("pagination", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
cmd.Parameters.Add(new SqlParameter("@PageSize", SqlDbType.Int, 4));
cmd.Parameters["@PageSize"].Value =pageSize;
cmd.Parameters.Add(new SqlParameter("@CurrentPageIndex", SqlDbType.Int, 4));
cmd.Parameters["@CurrentPageIndex"].Value = PageIndex;
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dt);
con.Close();
return dt;
}
--------------------编程问答-------------------- SqlCommand cmd = new SqlCommand("pagination", con);
改为
SqlCommand cmd = new SqlCommand("usp_OutStudent", con);
你的两个参数也相应的改一下就可以了
WinForm用存储过程分页 --------------------编程问答-------------------- 该简单一点:
SqlConnection con = new SqlConnection(this.ConnectStr());
SqlCommand cmd = new SqlCommand("pagination", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
cmd.Parameters.Add(new SqlParameter("@name ", SqlDbType.Char, 10));
cmd.Parameters["@name "].Value ="张晓彤";
cmd.Parameters.Add(new SqlParameter("@sex", SqlDbType.Char, 10));
cmd.Parameters["@sex"].Value ="女";
cmd.ExecuteNonQuery (); --------------------编程问答-------------------- --------------------编程问答-------------------- SqlCommand com=new SqlCommand();
com.Connection=connection;
com.CommandText="usp_OutStudent";//是存储过程名称
com.CommandType=CommandType.StoredProcedure;//CommandType是一枚举类型 --------------------编程问答-------------------- 主要就是SqlCommand啦 --------------------编程问答-------------------- SqlConnection conn = new SqlConnection(this.ConnectStr());
conn.Open();
SqlCommand cmd1 = new SqlCommand();
cmd1.CommandText = "usp_OutStudent
"; //存储过程名
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Connection = conn;
try
{
cmd1.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
MessageBox.Show("执行存储成功");
}
这是没有参数的存储调用,理解了在去试着写带参数的,这样理解的更深 --------------------编程问答-------------------- 楼主是调用带输出参数的存储过程
--------------------编程问答-------------------- 学习了 谢谢 --------------------编程问答-------------------- SQL-server中编写存储过程及其调用
//创建Connection对象,并打开连接
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
//创建Command对象
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;//命令类型是存储过程
cmd.CommandText = "usp_OutStudent";//调用存储过程名称
cmd.Parameters.Add("@name", "张三");//创建参数对象,为参数@name 赋值“张三”
cmd.Parameters.Add("@sex", SqlDbType.Char);
//参数@sex是输出参数,定义Direction属性值
cmd.Parameters["@sex"].Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
Console.WriteLine("性别:{0}", cmd.Parameters["@sex"].Value.ToString());
}
}
在使用Transanct-SQL语言编程过程中,可以将某些多次调用以实现某个特定任务的代码段编写成一个过程,将其保存在数据库中,并由SQL-Server服务器通过过程名调用它们,这些过程就叫做存储过程。
存储过程可以实现一下功能:
1.接收输入参数并以输出参数的格式想调用过程或批处理返回多个值。
2.包含用于在数据库中执行操作(包括调用其他过程)的编程语句。
3.向调用过程或批处理返回状态值,以指明成功或失败(以及失败的原因)。
存储过程分为有参的和无参的:
比如:
定义无参构造函数:
create proc up_info
@startDate datetime,
@endDate datetime
as
selet orderID,CompanyName,Salename,OrderDate
from Orders inner join Customer
on Orders.CustomerID=Cistomer.CustomerID
Seller.SaleID=Orders.SaleID
where (Orders.OrderDate between @startDate and @endDate)
go
调用构造函数:
exec up_info
有输出的参存数过程的定义:
create proc mp_Customer
@CID CHAR(3),
@CompanyName CHAR(60) OUTPUT
@Address CHAR (40) OUTPUT
AS
SELECT @CompanyName=CompanyName,
@Address=ADDRESS
FROM Customer c
where c.CustomerID=@CID
调用有参数的存储过程:
DECLARE @CompanyName AS CHAR(60)
DECLARE @ADDRESS AS CHAR(40)
DECLARE @CID AS CHARE(3)
exec mp_Customer 'co2'
@CompanyName output,
@Address output
select @CompanyName as '公司名',@Address as'公司地址' --------------------编程问答-------------------- 调用存储过程名称,并且传需要的参数就可以了 --------------------编程问答-------------------- 恩恩 谢谢各位了 学习了
补充:.NET技术 , C#