【问题标题】:Ambiguous column name when trying to outer join尝试外部连接时列名不明确
【发布时间】:2014-02-25 05:46:31
【问题描述】:

我有一个约会表格,它将根据约会表在网格视图中显示数据。我编写了 select 语句,以便将患者表中的患者 ID 更改为 pFirstName,并将医疗中心表中的 mcID 更改为 mcCentre。有一个 WHERE 查询,因为我只想显示仅属于当前用户登录的约会/行。当我调试时,我得到了这个错误 Ambiguous column name patientID。

我的查看预约表

我的 3 张预约表、医疗中心和患者。

我的查看预约表格代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class member_viewappointment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            // call BindGridView
            bindGridView();

        }
    }

    private void bindGridView()
    {
        int ID = Convert.ToInt32(Session["ID"].ToString());
        //get connection string from web.config
        string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnectionString"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);

        string strCommandText = "SELECT aStatus, aDate, aTime, aContact, aHeight, aWeight, med.mcCentre, pat.pFirstName from appointment AS app ";
        strCommandText += " LEFT OUTER JOIN MEDICALCENTRE as med on app.appointmentid = med.mcid";
        strCommandText += " LEFT OUTER JOIN PATIENT as pat on app.patientid = pat.patientid ";
        strCommandText += " WHERE patientid = " + ID.ToString();

        try
        {
            SqlCommand cmd = new SqlCommand(strCommandText, myConnect);

            myConnect.Open();
            SqlDataReader reader = cmd.ExecuteReader();

            DataTable dt = new DataTable();
            dt.Load(reader);
            grdViewAppointment.DataSource = dt;
            grdViewAppointment.DataBind();
            lblResult.Text = "";

            reader.Close();
        }
        catch (SqlException ex)
        {
            lblResult.Text = "Error:" + ex.Message.ToString();
        }
        finally
        {
            myConnect.Close();
        }

    }
}

【问题讨论】:

    标签: c# gridview outer-join


    【解决方案1】:

    问题:您在两个表中都有patientid 列(appointmentMEDICALCENTRE)。所以当您单独使用列名patientid 时,没有提及它的表名属于 ,无法识别,导致模棱两可的情况。

    解决方案:所以您应该在列名patientid 之前提及表名或表别名,以消除patientid 属于哪个表的歧义。

    替换这个:

    strCommandText += " WHERE patientid = " + ID.ToString();
    

    有了这个:

    strCommandText += " WHERE app.patientid = " + ID.ToString();
    

    建议:您的SELECT 查询对SQL Injection Attacks 开放,所以我建议您使用Parameterised queries 来避免它们。

    使用参数化查询:

    strCommandText += " WHERE patientid = @patientid";
    try
    {
    SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
    cmd.Parameters.AddWithValue("@patientid",ID.ToString());
    /*remaining same*/
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-14
      • 1970-01-01
      • 2021-03-05
      相关资源
      最近更新 更多