[英]Calling stored procedure from Entity Framework in C#
This is my output looks like... 这是我的输出看起来像...
and this is my backend 这是我的后端
Beta_DatabaseEntities db = new Beta_DatabaseEntities();
table2 tb = new table2();
int ID;
string Name;
int Salary;
public void Entry()
{
ID = Convert.ToInt16(id.Text);
Name = name.Text;
Salary = Convert.ToInt32(salary.Text);
}
private void insert_Click(object sender, EventArgs e)
{
Entry();
tb.Id = ID;
tb.Name = Name;
tb.Salary = Salary;
db.table1.Add(tb);
db.SaveChanges();
db.Database.ExecuteSqlCommand("GradeEntry "+)
}
My stored procedure: 我的存储过程:
create procedure GradeEntry
(@ID int, @name nvarchar(50), @salary int)
As
Begin
if(@salary >= 2500)
Begin
insert into Table2 values(@ID, @name, 'A+', @salary)
End
else if(@salary >= 1000)
Begin
insert into Table2 values(@ID, @name, 'A', @salary)
End
else if(@salary >= 500)
Begin
insert into Table2 values(@ID, @name, 'B', @salary)
End
else
Begin
insert into Table2 values(@ID, @name, 'Interni', @salary)
End
End
I am working with Entity Framework but can't figure out how to call the stored procedure working in C#. 我正在使用Entity Framework,但无法弄清楚如何调用在C#中工作的存储过程。 I am using Visual Studio 2013 我正在使用Visual Studio 2013
I just want to call the stored procedure from C# using Entity Framework. 我只想使用Entity Framework从C#调用存储过程。
You are almost there with your ExecuteSqlCommand
您几乎可以使用ExecuteSqlCommand
You just need to add EXEC
, then pass in the parameters: 您只需要添加EXEC
,然后传入参数:
db.Database.ExecuteSqlCommand("EXEC GradeEntry @ID, @name, @salary",
new SqlParameter("@ID", ID),
new SqlParameter("@name", Name),
new SqlParameter("@salary", Salary),
)
ExecuteSqlCommand takes an array of object, as parameters. ExecuteSqlCommand将对象数组作为参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.