从数据库中的两个表中获取数据,从数据库中删除整行。 [英] Get data in either of 2 tables from database, Delete entire row from database.

查看:77
本文介绍了从数据库中的两个表中获取数据,从数据库中删除整行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void button3_Click(object sender, EventArgs e)
       {
           string coursetype = "";

           if (cbxType.Text == "JUNIOR")
           {
               coursetype = "JUNIOR_STUDENT";
           }
           else if (cbxType.Text == "DEGREE")
           {
               coursetype = "DEGREE_STUDENT";
           }
           else
           {
               MessageBox.Show("Please choose one of the CourseType.", "BFMS-ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
           }
           try
           {
               using (SqlConnection con = new SqlConnection("Data Source=ROHIT-PC\\SQLEXPRESS;Initial Catalog=FMS;Integrated Security=True"))
  
               // this is what confusing me if the uid is from junior then it will retrieve data from junior student or else from degree student    but it show incorrect syntax near'='.          
                  using (SqlCommand cmd = new SqlCommand("select from" + coursetype + "where UID = @UID", con))
               {
                   cmd.Parameters.AddWithValue("@UId", textBox1.Text);
                   con.Open();
                   using (SqlDataReader reader = cmd.ExecuteReader())
                   {
                       if (reader.Read())
                       {
                           txtName.Text = reader["STUDENT_NAME"].ToString();
                           txtUid.Text = reader["UID"].ToString();
                           txtRoll.Text = reader["ROLL_NO"].ToString();
                           textGender.Text = reader["GENDER"].ToString();
                           textYear.Text = reader["YEAR_ID"].ToString();
                           txtRemarks.Text = reader["REMARKS"].ToString();
                           txtAcademicYear.Text = reader["ACADEMIC_YEAR"].ToString();
                           txtAdmissiontype.Text = reader["ADMISSION_TYPE"].ToString();
                           textCategory.Text = reader["CATEGORY_ID"].ToString();
                           txtSubcategory.Text = reader["SUB_CATEGORY"].ToString();
                           textCource.Text = reader["CLASS_ID"].ToString();


                       }


                   }
               }
           }
           catch (Exception er)
           {
               MessageBox.Show("There was an Error : " + er, "BFMS CRITICAL ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);

           }
           try
           {
               using (SqlConnection con = new SqlConnection("Data Source=ROHIT-PC\\SQLEXPRESS;Initial Catalog=FMS;Integrated Security=True"))
             
               using (SqlCommand cmd = new SqlCommand("select * from PAYMENT where STUD_UID= @UID", con))

               {
                   cmd.Parameters.AddWithValue("@UId", textBox1.Text);
                   con.Open();
                   using (SqlDataReader reader = cmd.ExecuteReader())
                   {
                       if (reader.Read())
                       {
                           textAmtpaid.Text = reader["AMOUNT_PAID"].ToString();
                           textBalance.Text = reader["BALANCE"].ToString();
                           mbxChequeno.Text = reader["CHEQUE_NO"].ToString();
                           txtBankname.Text = reader["BANK_NAME"].ToString();
                           textPayType.Text = reader["PAYMENT_TYPE"].ToString();
                       }
                       reader.Close();
                       con.Close();
                   }
               }
           }
           catch (Exception er)
           {
               MessageBox.Show("There was an Error : " + er, "BFMS CRITICAL ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);

           }
       }

       private void textBox2_TextChanged(object sender, EventArgs e)
       {

       }

       public SqlConnection con { get; set; }

       private void Cancellation_Load(object sender, EventArgs e)
       {
           // TODO: This line of code loads data into the 'bFMSDataSet3.CLASS' table. You can move, or remove it, as needed.
           this.cLASSTableAdapter.Fill(this.bFMSDataSet3.CLASS);
           // TODO: This line of code loads data into the 'bFMSDataSet2.CATEGORY' table. You can move, or remove it, as needed.
           this.cATEGORYTableAdapter.Fill(this.bFMSDataSet2.CATEGORY);
           // TODO: This line of code loads data into the 'bFMSDataSet4.YEAR_IN' table. You can move, or remove it, as needed.
           this.yEAR_INTableAdapter.Fill(this.bFMSDataSet4.YEAR_IN);
           // TODO: This line of code loads data into the 'yearData.ACC_YEAR' table. You can move, or remove it, as needed.


       }

       public string PAYMENTTYPE { get; set; }

       public string TYPE { get; set; }

       public string PAYMENT_TYPE { get; set; }

       private void button1_Click(object sender, EventArgs e)
       {

           try
           {


               con = new SqlConnection("Data Source=ROHIT-PC\\SQLEXPRESS;Initial Catalog=FMS;Integrated Security=True");

               string insertQuery = @"INSERT INTO CANCEL_STUDENT_RECORDS (STUDENT_NAME, UID,ROLL_NO,ADMISSION_TYPE, GENDER, SUB_CATEGORY,ACADEMIC_YEAR, REMARKS, CLASS, CATEGORY)
                                       VALUES(@NAME,@UID ,@ROLL_NO, @ADM_TYPE, @GENDER, @SUB_CAT, @ACDMYEAR, @REMARKS, @CLASSID, @CATID)";

               insertCommand = new SqlCommand(insertQuery);
               insertCommand.Parameters.AddWithValue("@NAME", txtName.Text);
               insertCommand.Parameters.AddWithValue("@UID", txtUid.Text);
               insertCommand.Parameters.AddWithValue("@ROLL_NO", txtRoll.Text);
               insertCommand.Parameters.AddWithValue("@ADM_TYPE", txtAdmissiontype.Text);
               insertCommand.Parameters.AddWithValue("@GENDER", textGender.Text);
               insertCommand.Parameters.AddWithValue("@SUB_CAT", txtSubcategory.Text);
               insertCommand.Parameters.AddWithValue("@ACDMYEAR", txtAcademicYear.Text);
               insertCommand.Parameters.AddWithValue("@REMARKS", txtRemarks.Text);
               insertCommand.Parameters.AddWithValue("@CLASSID", textCource.Text);
               insertCommand.Parameters.AddWithValue("@CATID", textCategory.Text);

               insertCommand.Connection = con;
               con.Open();
               insertCommand.ExecuteNonQuery();


               MessageBox.Show("Student : " + txtName.Text + " Data Successfully Inserted ", "Student Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

           }

           catch (SqlException ex)
           {
               MessageBox.Show("Could not insert " + txtName.Text + " Data \n " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

           }



           try
           {
               con = new SqlConnection("Data Source=ROHIT-PC\\SQLEXPRESS;Initial Catalog=FMS;Integrated Security=True");
                // and at this point record insert in another table where i want but data is not delete from respective table (Junior or degree)
               string insertQuery = "DELETE FROM" + coursetype + "WHERE UID = '@UID'";
           }
           catch (SqlException ex1)
           {
               MessageBox.Show("Could not Delete " + txtName.Text + " Data \n " + ex1.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

           }

              MessageBox.Show("Confirm Deletion" + txtName.Text + " Data \n ");
       }







感谢高级




Thankss in advanced

推荐答案

带你连接的字符串,并尝试在SSMS中运行它,当它失败时,结构直到它工作。现在复制代码中的结构!



几乎可以肯定缺少空格。
Take you concatenated string and try running it in SSMS, when it fails fiddle with the structure until it works. Now duplicate the structure in your code!

It is almost certainly missing spaces.


在您的代码中,尽管您已经创建了删除sql但你没有执行它,执行代码在哪里?这是insertCommand.ExecuteNonQuery();?



另外问题似乎在这一行



In your code though you have made the delete sql but you are not executing it, where is the execution code? that is insertCommand.ExecuteNonQuery();?

In addition the problem seems to be in this line

string insertQuery = "DELETE FROM" + coursetype + "WHERE UID = '@UID'";





用它改变它





Change it with this

string insertQuery = "DELETE FROM " + coursetype + " WHERE UID = '@UID'";


这篇关于从数据库中的两个表中获取数据,从数据库中删除整行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆