我更改了一些字段以显示为其他表中的其他字段,现在我无法更新值

时间:2014-01-18 07:03:59

标签: c# sql forms join

以前它只在一个表上显示字段时有效但现在当我更改任何字段中的值,并单击更新按钮时,它给了我(不支持多个基表的动态SQL生成。)帮助。

预约表格 appointmentform

更新错误 errorupdating

约会表 Appointment table

护士桌 nurse table

medicalcentre表 medicalcentretable

病人表 patient table

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 appointment : Form
    {

        // Data Table to store employee data
        DataTable Appointment = new DataTable();

        // Keeps track of which row in Gridview
        // is selected
        DataGridViewRow currentRow = null;

        SqlDataAdapter AppointmentAdapter;

        public appointment()
        {
            InitializeComponent();
        }

        private void appointment_Load(object sender, EventArgs e)
        {
            LoadMedicalCentreRecords();
        }



        private void LoadMedicalCentreRecords()
        {

            //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 appointmentID, aDate, aTime, aStatus, aContact, aHeight, aWeight, p.pFirstName , m.mcCentre , n.nFirstName FROM APPOINTMENT AS a LEFT OUTER JOIN Nurse AS n ON a.nurseID = n.NurseID Left outer join Patient as p on a.patientid = p.patientId left outer join medicalcentre as m on a.mcID = m.mcid";

            AppointmentAdapter = new SqlDataAdapter(strCommandText, myConnect);

            //command builder generates Select, update, delete and insert SQL
            // statements for MedicalCentreAdapter
            SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(AppointmentAdapter);
            // Empty Employee Table first
            Appointment.Clear();
            // Fill Employee Table with data retrieved by data adapter
            // using SELECT statement
            AppointmentAdapter.Fill(Appointment);

            // if there are records, bind to Grid view & display
            if (Appointment.Rows.Count > 0)
                grdApp.DataSource = Appointment;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            int modifiedRows = 0;
            // Get changes
            DataTable UpdatedTable = Appointment.GetChanges();
            if (UpdatedTable != null)
            {
                // there are changes
                // Write modified data to database 
                modifiedRows = AppointmentAdapter.Update(UpdatedTable);
                // accept changes
                Appointment.AcceptChanges();
            }
            else
                MessageBox.Show("there are no changes to update");

            if (modifiedRows > 0)
            {
                MessageBox.Show("There are " + modifiedRows + " records updated");
                LoadMedicalCentreRecords();
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (printDialog1.ShowDialog() == DialogResult.OK) // this displays the dialog box and performs actions dependant on which option chosen.
            {
                printDocument1.Print();
            }
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            int columnPosition = 0;
            int rowPosition = 25;

            // run function to draw headers
            DrawHeader(new Font(this.Font, FontStyle.Bold), e.Graphics, ref columnPosition, ref rowPosition); // runs the DrawHeader function

            rowPosition += 35; // sets the distance below the header text and the next black line (ruler)

            // run function to draw each row
            DrawGridBody(e.Graphics, ref columnPosition, ref rowPosition);
        }

        // DrawHeader will draw the column title, move over, draw the next column title, move over, and continue.
        private int DrawHeader(Font boldFont, Graphics g, ref int columnPosition, ref int rowPosition)
        {
            foreach (DataGridViewColumn dc in grdApp.Columns)
            {

                //MessageBox.Show("dc = " + dc);

                g.DrawString(dc.HeaderText, boldFont, Brushes.Black, (float)columnPosition, (float)rowPosition);
                columnPosition += dc.Width + 5; // adds to colPos. value the width value of the column + 5. 
            }

            return columnPosition;
        }

        /* DrawGridBody will loop though each row and draw it on the screen. It starts by drawing a solid line on the screen, 
         * then it moves down a row and draws the data from the first grid column, then it moves over, then draws the data from the next column,
         * moves over, draws the data from the next column, and continus this pattern. When the entire row is drawn it starts over and draws
         * a solid line then the row data, then the next solid line and then row data, etc.
        */
        private void DrawGridBody(Graphics g, ref int columnPosition, ref int rowPosition)
        {
            // loop through each row and draw the data to the graphics surface.
            foreach (DataRow dr in ((DataTable)grdApp.DataSource).Rows)
            {
                columnPosition = 0;

                // draw a line to separate the rows 
                g.DrawLine(Pens.Black, new Point(0, rowPosition), new Point(this.Width, rowPosition));

                // loop through each column in the row, and draw the individual data item
                foreach (DataGridViewColumn dc in grdApp.Columns)
                {
                    // draw string in the column
                    string text = dr[dc.DataPropertyName].ToString();
                    g.DrawString(text, this.Font, Brushes.Black, (float)columnPosition, (float)rowPosition + 10f); // the last number (10f) sets the space between the black line (ruler) and the text below it.

                    // go to the next column position
                    columnPosition += dc.Width + 5;
                }

                // go to the next row position
                rowPosition = rowPosition + 60; // this sets the space between the row text and the black line below it (ruler).
            }
        }

        private void btnPrintPreview_Click(object sender, EventArgs e)
        {
            try
            {
                // PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog(); // instantiate new print preview dialog
                printPreviewDialog1.Document = this.printDocument1;
                if (printPreviewDialog1.ShowDialog() == DialogResult.OK) // Show the print preview dialog, uses printPage event to draw preview screen
                {
                    printDocument1.Print();
                }
            }
            catch (Exception exp)
            {
                System.Console.WriteLine(exp.Message.ToString());
            }
        }




    }
}

0 个答案:

没有答案