[英]Asp.NET C# MVC - How show the result a query with multiples table
I'm executing the following code:我正在执行以下代码:
public ActionResult BranchUser(int ? idBranch)
{
CtrForm objCtrForm = new CtrForm();
objCtrForm.Connect();
objCtrForm.query.Connection = objCtrForm.connection;
objCtrForm.query.CommandType = CommandType.Text;
List<tblUser> listBranchUser = new List<tblUser>();
string sql = @"SELECT B.name, B.phone, B.email, B.idUser FROM tblBranchUser A
INNER JOIN tblUsers B ON A.idUser = B.idUser
WHERE A.idBranch = " + idBranch;
objCtrForm.query.CommandText = sql;
objCtrForm.connection.Open();
SqlDataReader sReader = objCtrForm.query.ExecuteReader();
while (sReader.Read())
{
listBranchUser.Add(
new tblUser
{
idUser = Convert.ToInt32(sReader["idUser"]),
name = Convert.ToString(sReader["name"]),
phone = Convert.ToString(sReader["phone"]),
email = Convert.ToString(sReader["email"])
}
);
}
objCtrForm.connection.Close();
return View(listBranchUser);
}
but I got the following error when I call the function:但是当我调用该函数时出现以下错误:
System.Data.SqlClient.SqlException: 'Incorrect syntax near '='.' System.Data.SqlClient.SqlException: ''=' 附近的语法不正确。
I don't understand why I get this, due to I executed in other functions but I only called one table, not two.我不明白为什么我得到这个,因为我在其他函数中执行但我只调用了一张表,而不是两张。 Any advice I will appreciate.
任何建议我都会很感激。
I believe C# won't implicitly convert an integer to a string.我相信 C# 不会将整数隐式转换为字符串。
Try replacing尝试更换
string sql = @"SELECT B.name, B.phone, B.email, B.idUser FROM tblBranchUser A
INNER JOIN tblUsers B ON A.idUser = B.idUser
WHERE A.idBranch = " + idBranch;
With和
string sql = @"SELECT B.name, B.phone, B.email, B.idUser FROM tblBranchUser A
INNER JOIN tblUsers B ON A.idUser = B.idUser
WHERE A.idBranch = " + idBranch.ToString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.