如何从两个表中删除值?

时间:2014-02-08 10:56:42

标签: c# gridview outer-join

我有一个medicalcentre表和windowadmin表,它们使用mcID相互关联。我首先在techcentre表中显示mcType和mcCentre到gridview,之后我使用外连接在windowsadmin表中将winusername字段和winPassword字段显示到gridview。我想要实现的是使用删除功能删除记录。但是我得到了错误,请看第3张图片。

我的windowsadmin和medicalcentre表 enter image description here

我的windowsadmin和medicalcentre表关系 enter image description here

我的表单 enter image description here

我的错误 enter image description here

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 cen.mcid, mcType, mcCentre, win.winUsername, win.winPassword FROM MEDICALCENTRE AS cen";
            strCommandText += " LEFT OUTER JOIN WINDOWSADMIN as win on cen.mcid = win.mcid";
            MedicalCentreAdapter = new SqlDataAdapter(strCommandText, myConnect);

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

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




private int DeleteMedicalCentreRecord()
        {
            // no row in GridView selected
            if (currentRow == null)
            {
                return 0;
            }
            //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 = "DELETE FROM WINDOWSADMIN WHERE winID=@WINID; DELETE FROM MEDICALCENTRE WHERE mcID=@MCID;";
            SqlCommand deleteCmd = new SqlCommand(strCommandText, myConnect);

            // Cell - contains employee ID
            int winID = Convert.ToInt32(currentRow.Cells[0].Value);
            int mcID = Convert.ToInt32(currentRow.Cells[0].Value);
            deleteCmd.Parameters.AddWithValue("@WINID", winID);
            deleteCmd.Parameters.AddWithValue("@MCID", mcID);           
            //// STEP 3: open connection and retrieve data by calling ExecuteReader
            myConnect.Open();
            //STEP 4: execute command
            int result = deleteCmd.ExecuteNonQuery();
            // STEP 5: Close
            myConnect.Close();
            return result;
        }

1 个答案:

答案 0 :(得分:1)

我认为您需要将DELETE命令更改为

string strCommandText = @"DELETE FROM WINDOWSADMIN WHERE mcID=@MCID; 
                          DELETE FROM MEDICALCENTRE WHERE mcID=@MCID;";

这确保将从WindowsAdmin表中删除与您要删除的医疗中心相关的每个获胜用户

换句话说,你可以在WINDOWSADMIN表中删除你喜欢的每一条记录,但是如果WINDOWSADMIN表中仍有一条记录仍在你想要删除的medicalcentre中的记录,你就不能删除MEDICALCENTRE表中的记录。 / p>