未向存储库类传递值
本文关键字:存储 | 更新日期: 2023-09-27 18:15:15
我正在asp.net mvc中创建一个简单的rest服务。我的问题是我的值没有传递给respository
类。我想我漏掉了什么,有人能帮我吗?这是我的controller
static LoginRespository repository = new LoginRespository();
public string AddEmployees(Login Emp)
{
//calling EmpRepository Class Method and storing Repsonse
var response = repository.AddEmployees(Emp);
return response;
}
和我的respository
课就像这样
public string AddEmployees(Login Emp)
{
SqlCommand com;
SqlConnection con = new SqlConnection(@"Data Source=DOTNET;Initial Catalog=edin;Integrated Security=True;Pooling=False");
try
{
com = new SqlCommand("select * from data where name='" + Emp.username + "'", con);
con.Open();
SqlDataReader dr = com.ExecuteReader();
if (dr.HasRows)
{
return "success";
}
else
{
return "error";
}
con.Close();
}
catch (Exception aa)
{
return aa.ToString();
}
}
,我的model
是
public string username { get; set; }
public string password { get; set; }
注意:
- 在方法addemplodys而不是传递员工的集合,你传递单个员工,并建议更改为addemploeys
- 用Select 代替Insert添加Employee
- 如果你在AddEmployee中收到null Employee,这意味着你在你的控制器中发送null Employee。
如果你调用AddEmployees on button click事件。试试这个:
Emp employeeRec = new Emp();
employeeRec.username = txtName.text;
employeeRec.password = txtPassword.text;
AddEmployees(employeeRec);