我正在研究重写一个在我的代码中返回输出参数的存储过程。为什么我这样做是因为我想要一个本地存储过程而不必从MSSQL调用存储过程。
这可能吗?如果是这样,那怎么样?
答案 0 :(得分:0)
我已经研究过LINQ to Entities并开始意识到对查询的控制很少。我需要找到更多权威信息,但是从使用真实存储过程和使用多个查询开始,由于MSSQL中包含的预构建项目和大量(意味着数十万)记录,实际存储过程会更有效。的SP。当我们希望从站点的前端传输小规模数据而不是使用存储过程时,我正在使用此问题作为未来项目的参考。
此链接是我用来编写原始SQL代码的链接。我正在使用原始调用,所以我可以返回字符串变量。
http://www.entityframeworktutorial.net/EntityFramework4.3/raw-sql-query-in-entity-framework.aspx
我的存储过程的草稿如下:
在查看此答案时,我使用//
作为易读性目的。使用此代码时请使用--
。
CREATE PROCEDURE [dbo].[spSomething]
@FirstName varchar(25),
@LastName varchar(25),
@UserID varchar(25),
@RentalManID varchar(25),
@BranchAreaNum varchar(25),
@ReturnVal varchar(1) = '0' output
WITH RECOMPILE
AS
BEGIN
SET NOCOUNT ON;
DECLARE @CheckTblUserAdded varchar(25)
DECLARE @CheckTblUser varchar(25)
// ReturnVal = 0 means it failed to add user to table
// ReturnVal = 1 means it succeded in adding user to table
// ReturnVal = 2 means the user has already been added to the table
// Search for user inside tblUserAdded
SELECT @CheckTblUserAdded = [Window ID] FROM dbo.tblUserAdded WHERE [Window ID] = @UserID AND
[Rentalman ID] = @RentalManID AND
[BranchAreaNum] = @BranchAreaNum AND
[FirstName] = @FirstName AND
[LastName] = @LastName
// Search for user inside tblUser
SELECT @CheckTblUser = [UserID] FROM dbo.tblUser WHERE [UserID] = @UserID AND
[RentalManID] = @RentalManID
// Add new user if not found in either tblUserAdded or tblUser
// and if the users windows id and rentalmanid are not the same
IF (ISNULL(@CheckTblUserAdded, '') = '') AND (ISNULL(@CheckTblUser, '') = '') AND (@UserID <> @RentalManID)
BEGIN
// Do something here
SELECT @ReturnVal = '1' // as success
END
// Record attempt of adding an existing user
ELSE
BEGIN
// Do something here
SELECT @ReturnVal = '2' // already exists
END
SELECT @ReturnVal
END
这是我控制器内部的原始SQL版本:
public ActionResult Create([Bind(Include="Window_ID,Rentalman_ID,BranchAreaNum,FirstName,LastName")] tblUserAdded tbluseradded)
{
if (ModelState.IsValid)
{
if (tbluseradded.Window_ID == tbluseradded.Rentalman_ID)
{
System.Diagnostics.Debug.WriteLine("The user should already have access since the user's Windows ID and RentalMan ID are the same. Contact IAUnit for help on the issue");
}
else
{
// Rewrite stored procedure in form of raw SQL
string returnVal = "0";
// This will check to see if a user has already been added to tblUser and tblUserAdded
string checkTblUserAdded = db.Database.SqlQuery<string>("SELECT [Window ID] FROM dbo.tblUserAdded WHERE " +
"[Window ID] = '" + tbluseradded.Window_ID + "' AND " +
"[Rentalman ID] = '" + tbluseradded.Rentalman_ID + "' AND " +
"[BranchAreaNum] = '" + tbluseradded.BranchAreaNum + "' AND " +
"[FirstName] = '" + tbluseradded.FirstName + "' AND " +
"[LastName] = '" + tbluseradded.LastName + "'").FirstOrDefault<string>();
string checkTblUser = db.Database.SqlQuery<string>("SELECT [UserID] FROM dbo.tblUser WHERE " +
"[UserID] = '" + tbluseradded.Window_ID + "' AND " +
"[RentalManID] = '" + tbluseradded.Rentalman_ID + "'").FirstOrDefault<string>();
// Add new user if not found in either tblUserAdded or tblUser
if (checkTblUser == null && checkTblUserAdded == null)
{
System.Diagnostics.Debug.WriteLine("We can add the user now! We did it!");
returnVal = "1";
// Do something here
}
else
{
System.Diagnostics.Debug.WriteLine("This user already has access");
returnVal = "2";
// Do something here
}
System.Diagnostics.Debug.WriteLine("The return value is: " + returnVal);
System.Diagnostics.Debug.WriteLine("Please break here!");
db.SaveChanges();
}
return RedirectToAction("Index");
}
return View(tbluseradded);
}
我确信有一种更好的方法可以通过将实际的原始SQL放在数据访问层中来编码,但我将其用于仅供参考。