致力于从Web应用程序获取存储过程。我可以验证值是否正在填充,但我得到两个错误之一。
这是相关的代码,我也环顾四周,尝试了几种不同的解决方案,只是坚持为什么它不起作用。
// incorrect syntax near newCompany - have tried with both the parameter1 and parameter1.Value, parameter2 and parameter2.Value
public virtual int usp_TransferRecords(int oldCompany, int newCompany)
{
SqlParameter parameter1 = new SqlParameter("OldCompany", oldCompany);
SqlParameter parameter2 = new SqlParameter("NewCompany", newCompany);
return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreCommand("usp_TransferRecords @OldCompany @NewCompany", parameter1, parameter2);
}
// Message = "Must declare the scalar variable \"@OldCompany\"." - have tried with both the parameter1 and parameter1.Value, parameter2 and parameter2.Value
public virtual int usp_TransferRecords(int oldCompany, int newCompany)
{
SqlParameter parameter1 = new SqlParameter("OldCompany", oldCompany);
SqlParameter parameter2 = new SqlParameter("NewCompany", newCompany);
return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreCommand("usp_TransferRecords @OldCompany, @NewCompany", parameter1, parameter2);
}
这是存储过程:
CREATE PROCEDURE [dbo].[usp_TransferRecords]
@OldCompany int,
@NewCompany int
AS
BEGIN
-- delete existing records from new company
DELETE FROM [dbo].[Table] WHERE CompanyID = @NewCompany
-- copy records from old company to new new company
INSERT INTO [dbo].[Table] ([ID], [CompanyID], [City], [State], [Priority])
SELECT NEWID(), @NewCompany, City, State, Priority
FROM [dbo].[TABLE]
WHERE CompanyID = @OldCompany
-- reset record pages for both new and old companies
SET NOCOUNT ON;
-- deletes Dynamic Content Cache for both new and old companies
DELETE FROM [dbo].[Table]
WHERE CompanyID IN (@OldCompany, @NewCompany)
AND (ContentKey LIKE '%-generic-service-page' OR
ContentKey LIKE '%-item-custom-service-page' OR
ContentKey LIKE '%-about-page')
-- deletes contents to so they will be regenerated
DELETE FROM [dbo].[TABLE]
WHERE CompanyID IN (@OldCompany, @NewCompany)
AND IsCityPage = 1
END
Web UI标记:</ p>
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "Form", enctype = "multipart/form-data" }))
{
<h2 style="text-align:center"> This copy Records from old Company to new Company</h2>
<div class="centered" style="width:400px">
@Html.TextBox("oldCompany", id, new { onkeyup = "this.value=this.value.replace(/[^0-9]/g,'')", watermark = "Old Company #", @class = "watermark", style = "display:inline-block;" })
@Html.TextBox("newCompany", String.Empty, new { onkeyup = "this.value=this.value.replace(/[^0-9]/g,'')", watermark = "New Company #", @class = "watermark", style = "display:inline-block;" })
<input type="submit" name="submitButton" value="Copy" class="right orangebutton" style="position:absolute;left:62%;" />
</div>
}
我通过更改以下内容使其正常工作
// Original
public virtual int usp_TransferRecords(int oldCompany, int newCompany)
{
SqlParameter parameter1 = new SqlParameter("OldCompany", oldCompany);
SqlParameter parameter2 = new SqlParameter("NewCompany", newCompany);
return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreCommand("usp_TransferRecords @OldCompany, @NewCompany", parameter1, parameter2);
}
为:
// Working
public virtual int usp_TransferRecords(int oldCompany, int newCompany)
{
var @params = new SqlParameter[]
{
new SqlParameter("OldCompany", oldCompany),
new SqlParameter("NewCompany", newCompany)
};
return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreCommand("usp_TransferRecords @OldCompany, @NewCompany", @params);
}
如果有任何关于为什么这种方法比其他原始方法有效的任何见解我很想知道。谢谢所有回复的人。
答案 0 :(得分:2)
// Message = "Must declare the scalar variable \"@OldCompany\"." - have tried with both the parameter1 and parameter1.Value, parameter2 and parameter2.Value
public virtual int usp_TransferRecords(int oldCompany, int newCompany)
{
SqlParameter parameter1 = new SqlParameter("OldCompany", oldCompany);
SqlParameter parameter2 = new SqlParameter("NewCompany", newCompany);
return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreCommand("usp_CopyRecord @OldCompany, @NewCompany", parameter1, parameter2);
}
您的错误消息告诉您问题。
&#34;必须声明标量变量\&#34; @OldCompany \&#34;。&#34;
"@OldCompany" != "OldCompany"
答案 1 :(得分:1)
这是最终的
// Working
public virtual int usp_TransferRecords(int oldCompany, int newCompany)
{
var @params = new SqlParameter[]
{
new SqlParameter("OldCompany", oldCompany),
new SqlParameter("NewCompany", newCompany)
};
return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreCommand("usp_TransferRecords @OldCompany, @NewCompany", @params);
}