如何在asp.net中调用存储过程

时间:2014-04-22 13:17:12

标签: asp.net sql ado.net

我为insertupdatedeleteselect编写了单个存储过程。程序是:

ALTER PROCEDURE [dbo].[artdetails1]
(
    @artdetailId int,
    @artDescription varchar(50),
    @artShortdesc varchar(50),
    @artValidity varchar(50),
    @artStatus varchar(50),
    @StatementType varchar(50)

)

AS
BEGIN
  BEGIN
    IF @StatementType = 'Insert'
        BEGIN
            INSERT INFOartdetails(artDescription,artShortdesc,artValidity,artStatus)
                values(@artDescription,@artShortdesc,@artValidity,@artStatus)   

        END

    IF @StatementType = 'Select'
        BEGIN
            SELECT * FROM artdetails
        END 

    IF @StatementType = 'Update'
        BEGIN
            UPDATE artdetails SET
                artDescription=@artDescription,artShortdesc=@artShortdesc,
                artValidity=@artValidity,artStatus=@artStatus
                where artdetailId= @artdetailId
        END

    ELSE IF @StatementType = 'Delete'
        BEGIN
            UPDATE artist SET @artStatus= 'not available';
        END
  END
END

但我对如何检查C#代码中的语句类型并调用此过程感到困惑。请帮忙。我是SQL的新手。

3 个答案:

答案 0 :(得分:1)

首先,你应该 NOT 在一个程序中执行选择和更新。您应该有单独的插入,更新,选择和删除过程。

但是,这是你如何从C#调用插入过程:

  1. 创建连接
  2. 创建命令
  3. 添加参数
  4. 执行命令
  5. 捕获结果(如果适用)
  6. 示例代码:

    using(var conn = new SqlConnection(connString) 
    {
        using(var cmd = new SqlCommand("artdetails1", conn)
        {
            cmd.CommandType = CommandType.StoredProcedure;
    
            cmd.Parameters.AddWithValue("@artdetailId", detailID);
            cmd.Parameters.AddWithValue("@artDescription", description);
            cmd.Parameters.AddWithValue("@artShortdesc", shortDesc);
            cmd.Parameters.AddWithValue("@artValidity", validity);
            cmd.Parameters.AddWithValue("@artStatus", status);
            cmd.Parameters.AddWithValue("@StatementType","Insert");
    
            conn.Open();
            cmd.ExecuteNonQuery();  // No output to capture for an `INSERT`
        }
    }
    

答案 1 :(得分:0)

存储过程调用示例(假设您正在使用SQL服务器)。对于一切都有一个存储过程是一个例外,好的程序员是一个懒惰的规则:

  using(var conn = new SqlConnection(@"Server=.\SQL2008;Database=foo;Integrated Security=true")) {
    var cmd = new SqlCommand("[dbo].[artdetails1]", conn);
    cmd.Parameters.Add("@StatementType", SqlDbType.VarChar, 50).Value = "Insert";
    // set all other parameters
    cmd.CommandType = CommandType.StoredProcedure;
    conn.Open();
    cmd.ExecuteNonQuery();
  }

答案 2 :(得分:0)

从c#调用存储过程的基本过程概述如下。

String connstr = @"Data Source=myserverName;Initial Catalog=myCatalogName;Integrated Security=True";
String statementType = "Select"; //just an example

SqlConnection conn = new SqlConnection(connstr);
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;

cmd.CommandText = "StoredProcedureName";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = conn;
cmd.Parameters.Add("artdetailId", SqlDbType.Int).Value = 1;
cmd.Parameters.Add("@artDescription", SqlDbType.VarChar).Value = "myArtDescription";
cmd.Parameters.Add("@artShortdesc", SqlDbType.VarChar).Value = "myArtShortDesc";
cmd.Parameters.Add("@artValidity", SqlDbType.VarChar).Value = "myArtValidity";
cmd.Parameters.Add("@artStatus", SqlDbType.VarChar).Value = "myArtStatus";
cmd.Parameters.Add("@StatementType", SqlDbType.VarChar).Value = StatementType;

sql conn.Open();

if StatementType = "Select" {
reader = cmd.ExecuteReader();
// Use DataReader to access the data here.
)
else if StatementType = "Insert" || StatementType = "Update" || StatementType = "Delete"
rowsAffected = cmd.ExecuteNonQuery();
}

注意:代码未经过测试。