C# & SQL Server 查询错误 : The multi-part identifier cannot be bound

标签 c# sql sql-server

protected void btnSubmit_Click(object sender, EventArgs e)
{
    connectionString = ConfigurationManager.ConnectionStrings["LeaveMangementSystemCS"].ConnectionString;
    conn = new SqlConnection(connectionString);
    string sql = "UPDATE LeaveType  SET LeaveType.Type=@Type, LeaveType.Description=@Description, LeaveType.NumOfDays=@NumOfDays, LeaveCategory.Category=@Category FROM LeaveType INNER JOIN LeaveCategory on LeaveType.LeaveCategoryId = LeaveCategory.Id  WHERE LeaveType.Id=@id";

    try
    {
            cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@Type", tbType.Text);
            cmd.Parameters.AddWithValue("@Description", tbDescription.Text);
            cmd.Parameters.AddWithValue("@NumOfDays",tbNumOfDays.Text);
            cmd.Parameters.AddWithValue("@Category", ddlLeaveCategory.Text);
            cmd.Parameters.AddWithValue("@id", lblIdOut.Text);

            conn.Open();
            int rows = cmd.ExecuteNonQuery();

            if (rows > 0)
            {
                lblOutput.Text = " Updated successfully.";
            }
    }
    catch (Exception ex)
    {
        lblOutput.Text = "Error Message : " + ex.Message;
    }
    finally
    {
        if (conn != null)
            conn.Close();
    }
}

我的 SQL 查询有一个错误:

The multi-part identifier "LeaveCategory.Category" could not be bound.

我曾尝试使用 leavetype 作为 a 和 leavecategory 作为 b 但仍然是这个错误。

最佳答案

您不能在一个语句中更新多个表!查看您的 sql 变量:

UPDATE LeaveType  
SET LeaveType.Type=@Type
,   LeaveType.Description=@Description
,   LeaveType.NumOfDays=@NumOfDays
,   LeaveCategory.Category=@Category 
FROM LeaveType 
INNER JOIN LeaveCategory on LeaveType.LeaveCategoryId = LeaveCategory.Id  
WHERE LeaveType.Id=@id";

在这里,您尝试更新表 LeaveType 和表 LeaveCategory 中的一列

请参阅this回答

关于C# & SQL Server 查询错误 : The multi-part identifier cannot be bound,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38324132/

相关文章:

c# - 仅写入一个 XML 属性,而不影响其余属性

c# - StructureMap 移除单个实例

c# - 如何在 powershell 中的单行上设置 Set-VMFirmware

sql - Oracle View 查询,基于一个条件检索数据,但未检索到一行

c# - 将查询包含到 C# 代码中的最佳方法

c# - 并非所选主题的所有 CSS 文件都包含在页面中

MySQL GROUP BY 每个逗号分隔值

sql-server - SQL Server 2008 上 varbinary(max) 文件流的长度

sql-server - 为什么使用 READ UNCOMMITTED 隔离级别?

c# - Entity Framework Load() 方法不会加载所有内容