这个存储过程用winform怎么调用???????????
create procedure usp_OutStudent
@name char(10),
@sex char(10) output
as
select @sex=性别 from mytable where 姓名=@name
create procedure usp_OutStudent
@name char(10),
@sex char(10) output
as
select @sex=性别 from mytable where 姓名=@name
{
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("usp_OutStudent", con);
你的两个参数也相应的改一下就可以了
WinForm用存储过程分页
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 ();
com.Connection=connection;
com.CommandText="usp_OutStudent";//是存储过程名称
com.CommandType=CommandType.StoredProcedure;//CommandType是一枚举类型
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("执行存储成功");
}这是没有参数的存储调用,理解了在去试着写带参数的,这样理解的更深
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());
}
}