分享

调用 存储过程

 北方的白桦林 2017-05-30
namespace t3_proc
{
class Program
{
static void Main(string[] args)
{
//接收用户输入
string str = Console.ReadLine();
//读取数据连接配置信息
string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["hem09"].ConnectionString;
//创建连接对象
using (SqlConnection conn = new SqlConnection(connStr))
{
//创建命令对象,第一个参数指定为存储过程的名称
SqlCommand cmd = new SqlCommand("MyTrim", conn);
//指定执行的命令类型为“存储过程”
cmd.CommandType = CommandType.StoredProcedure;
//提供存储过程的参数,这里是双向的参数
SqlParameter p1 = new SqlParameter("@str", str);
//执行前输出
Console.WriteLine(p1.Value);
p1.Direction = ParameterDirection.InputOutput;
cmd.Parameters.Add(p1);
//insert into employee values(@name,@gender,@birthday)
//
//SqlParameter p2 = new SqlParameter("@name", "");
//p2.Direction = ParameterDirection.Output;
//cmd.Parameters.Add(p2);
//执行命令
conn.Open();
cmd.ExecuteNonQuery();
//通过参数获得存储过程返回的值
Console.WriteLine("==================");
Console.WriteLine(p1.Value);
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约