0

重複の可能性:
ASP.NET MVC:ストアドプロシージャを呼び出すための最良の方法

MCV3アプリケーションを開発しています。

アプリケーションのコントローラーの1つでストアード・プロシージャーを呼び出したい。

アプリケーションに使用しているストアドプロシージャをすでにDBに保存しています。

クエリは

Create Procedure ConvertLeadToCustomer1
@CompanyID int
as
begin 
update Companies set __Disc__ = 'Customer' where CompanyID = @CompanyID
end

今、私はこの手順をコントローラーに呼び出したいと思います...

namespace CRMWeb.Controllers
{ 
    public class LeadController : Controller
    {
        private CRMWebContainer db = new CRMWebContainer();

        //
        // GET: /Lead/

        public ViewResult Index()
        {
            //return View(db.Companies.ToList());
            return View(db.Companies.OfType<Lead>().ToList());

        }

           public ActionResult Convert(int id)
        {

            // I want to write code here to call stored procedure...


        }
    }
}

それをどのように呼ぶのですか?

4

1 に答える 1

3

mvcでも違いはありません。ADO.netを使用している場合は、以下のコードでストアドプロシージャを呼び出します。

public ActionResult Convert(int id)
{
    var connection = new SqlConnection("YOUR CONNECTION STRING");

    var command = new SqlCommand("ConvertLeadToCustomer1",connection)
    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.AddWithValue("@CompanyID", id);

    connection.Open();

    command.ExcuteNonQuery();

    connection.Close();
}
于 2012-10-15T07:40:42.897 に答える