Skip to main content
 首页 » 编程设计

c#之左外连接代码的列名称不明确

2024年02月13日14lyj

我在窗体上有一个数据 GridView 来显示处方表数据。我想将处方表中的 paymentID 字段更改为付款表中存在的金额字段。但是在我的 select 语句中写了左外连接代码后,当我调试时,我得到了第二张图片中的错误。

付款和处方表

不明确的列名

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.SqlClient; 
using System.Configuration; 
 
namespace GRP_02_03_SACP 
{ 
    public partial class prescription : Form 
    { 
 
        // Data Table to store employee data 
        DataTable Prescription = new DataTable(); 
 
        // Keeps track of which row in Gridview 
        // is selected 
        DataGridViewRow currentRow = null; 
 
        SqlDataAdapter PrescriptionAdapter; 
 
        public prescription() 
        { 
            InitializeComponent(); 
        } 
 
        private void btnSubmit_Click(object sender, EventArgs e) 
        { 
            if (btnSubmit.Text == "Clear") 
            { 
                btnSubmit.Text = "Submit"; 
                ClearTextBoxes(); 
                txtmedicationID.Focus(); 
            } 
            else 
            { 
                btnSubmit.Text = "Clear"; 
                int result = AddPrescriptionRecord(); 
                if (result > 0) 
                    MessageBox.Show("Insert Successful"); 
                else 
                    MessageBox.Show("Insert Fail"); 
 
            } 
        } 
 
        private void ClearTextBoxes() 
        { 
            txtmedicationID.Clear(); 
            txtappointmentID.Clear(); 
        } 
 
        private int AddPrescriptionRecord() 
        { 
            int result = 0; 
            // TO DO: Codes to insert customer record 
            //retrieve connection information info from App.config 
            string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnection"].ConnectionString; 
            //STEP 1: Create connection 
            SqlConnection myConnect = new SqlConnection(strConnectionString); 
            //STEP 2: Create command 
            String strCommandText = "INSERT Prescription(medicationID, appointmentID) " 
                + " VALUES (@NewmedicationID, @NewappointmentID)"; 
 
            SqlCommand updateCmd = new SqlCommand(strCommandText, myConnect); 
 
            updateCmd.Parameters.AddWithValue("@NewmedicationID", txtmedicationID.Text); 
            updateCmd.Parameters.AddWithValue("@NewappointmentID", txtappointmentID.Text); 
            //updateCmd.Parameters["@clientid"].Direction = ParameterDirection.Output;  
            // STEP 3 open connection and retrieve data by calling ExecuteReader 
            myConnect.Open(); 
            // STEP 4: execute command 
            // indicates number of record updated. 
            result = updateCmd.ExecuteNonQuery(); 
 
            // STEP 5: Close 
            myConnect.Close(); 
            return result; 
 
        } 
 
        private void prescription_Load(object sender, EventArgs e) 
        { 
            LoadPrescriptionRecords(); 
        } 
 
        private void LoadPrescriptionRecords() 
        { 
 
            //retrieve connection information info from App.config 
            string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnection"].ConnectionString; 
            //STEP 1: Create connection 
            SqlConnection myConnect = new SqlConnection(strConnectionString); 
            //STEP 2: Create command 
            string strCommandText = "SELECT prescriptionID, medicationID, appointmentID, c.amount  FROM PRESCRIPTION AS a left outer join payment as c on a.paymentid = c.paymentId"; 
 
            PrescriptionAdapter = new SqlDataAdapter(strCommandText, myConnect); 
 
            //command builder generates Select, update, delete and insert SQL 
            // statements for MedicalCentreAdapter 
            SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(PrescriptionAdapter); 
            // Empty Employee Table first 
            Prescription.Clear(); 
            // Fill Employee Table with data retrieved by data adapter 
            // using SELECT statement 
            PrescriptionAdapter.Fill(Prescription); 
 
            // if there are records, bind to Grid view & display 
            if (Prescription.Rows.Count > 0) 
                grdPrescription.DataSource = Prescription; 
        } 
 
        private void btnUpdate_Click(object sender, EventArgs e) 
        { 
            int modifiedRows = 0; 
            // Get changes 
            DataTable UpdatedTable = Prescription.GetChanges(); 
            if (UpdatedTable != null) 
            { 
                // there are changes 
                // Write modified data to database  
                modifiedRows = PrescriptionAdapter.Update(UpdatedTable); 
                // accept changes 
                Prescription.AcceptChanges(); 
            } 
            else 
                MessageBox.Show("there are no changes to update"); 
 
            if (modifiedRows > 0) 
            { 
                MessageBox.Show("There are " + modifiedRows + " records updated"); 
                LoadPrescriptionRecords(); 
            } 
        } 
    } 
} 

请您参考如下方法:

查询应该是这样的:

string strCommandText = "SELECT prescriptionID, medicationID, a.appointmentID, 
                         c.amount  FROM PRESCRIPTION AS a 
                         left outer join payment as c 
                         on a.paymentid = c.paymentId"; 

由于 appointmentID 存在于两个表中,因此您需要通过将 appointmentID 替换为 a.appointmentID 来解决歧义。