我在表单上有一个datagridview以显示处方表数据。我想将我的处方表中的paymentID字段更改为付款表中存在的金额字段。但是在我的选择语句中写完左外部联接代码后,当我调试时,我在第二个图像中得到了错误。

付款和处方表


列名不明确


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来解决歧义。

10-08 00:16