MYSTERY ZILLION တွင် English သို့မဟုတ် Unicode ဖြင့်သာ အသုံးပြုခွင့်ရှိသည်။ ဇော်ဂျီ ၊ ဧရာ စသည်တို့ကို အသုံးပြုခွင့် မရှိ။ Unicode fonts များမှာ Mon3,Yunghkio, Myanamr3 စသည်များ အသုံးပြုနိုင်သည်။ Unicode Guide ကို ဒီမှာ Download ချပါ။ Zawgyi to Unicode Converter
Don't share ebook or software if nobody request. You can find free book websites on here. We are welcome for discussion or asking question instead.

Insert and Update Multiple Table

I Have Two Table

Staffs

pk staffid
name
address
nrc

Staffs Holiday

staffid
holiday

This is my Old Query.

string sql= "IF EXISTS (SELECT staffid FROM Staffs WHRE staffid=@staffid) UPDATE SET name=@name,address=@address,nrc=@nrc WHERE staffid=@staffid ELSE INSERT INTO staffs VALUES (@staffid,name,address,nrc)"

I want to insert two table and if staffid already exits want to update Two table Please Help Me.

မှတ်ချက်များ

  • Registered Users

    Use BEGIN and END for each condition. Eg.

    IF EXISTS(SELECT 1 FROM dbo.Table1 WHERE Column1 = 123)
    BEGIN
        UPDATE dbo.Table1 SET Column2 = 123 WHERE Column1 = 123
        UPDATE dbo.Table2 SET Column2 = 456 WHERE Column1 = 123
    END
    ELSE
    BEGIN
        INSERT INTO dbo.Table1 (Column1, Column2) VALUES (123, 456)
        INSERT INTO dbo.Table2 (Column1, Column2) VALUES (123, 456)
    END
    
  • Registered Users

    BRo Thank.............He HE :-D

  • edited November 2014 Registered Users

    Bro I have 1 Problem Staff Holiday Table
    PK staffid -- PK Holiday

    The variable name '@staffid' has already been declared. Variable names must be unique within a query batch or stored procedure.

  • Registered Users
                if (checkedListBox1.SelectedIndex != -1)
                {
                    using (SqlConnection con = new SqlConnection(MyGolbalVariable.con_string))
                    {
                        con.Open();
                        using (SqlCommand cmd = new SqlCommand("IF EXISTS(SELECT staffid FROM staffs WHERE staffid = @staffid) BEGIN UPDATE staffs SET name=@name,nrc=@nrc,dob=@dob,fathername=@fathername WHERE staffid =@staffid UPDATE staffs_holiday SET holiday =@holiday WHERE staffid = @staffid END ELSE BEGIN INSERT INTO staffs VALUES (@staffid,@name,@nrc,@dob,@fathername) INSERT INTO staffs_holiday VALUES (@staffid,@holiday) END", con))
                        {
                            foreach (object item in checkedListBox1.CheckedItems)
                            {
                                cmd.Parameters.AddWithValue("@staffid", txtstaffid.Text);
                                cmd.Parameters.AddWithValue("@name", txtname.Text);
                                cmd.Parameters.AddWithValue("@nrc", txtnrc.Text);
                                cmd.Parameters.AddWithValue("@dob", dtpdob.Value);
                                cmd.Parameters.AddWithValue("@fathername", txtfathername.Text);
                                cmd.Parameters.AddWithValue("@holiday", item.ToString());
                                cmd.ExecuteNonQuery();
    
                                MessageBox.Show(this, "Save Successful", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                ClearForm();
                            }
                        }
                        con.Close();
                    }
                }
    
  • Registered Users

    Because of you are binding parameters within the loop. Check below code.

    if (checkedListBox1.SelectedIndex != -1)
                {
                    using (SqlConnection con = new SqlConnection(MyGolbalVariable.con_string))
                    {
                        con.Open();
    
                        foreach (object item in checkedListBox1.CheckedItems)
                        {
                            using (SqlCommand cmd = new SqlCommand("IF EXISTS(SELECT staffid FROM staffs WHERE staffid = @holiday) END", con))
                            {
                                cmd.Parameters.AddWithValue("@staffid", txtstaffid.Text);
                                cmd.Parameters.AddWithValue("@name", txtname.Text);
                                cmd.Parameters.AddWithValue("@nrc", txtnrc.Text);
                                cmd.Parameters.AddWithValue("@dob", dtpdob.Value);
                                cmd.Parameters.AddWithValue("@fathername", txtfathername.Text);
                                cmd.Parameters.AddWithValue("@holiday", item.ToString());
                                cmd.ExecuteNonQuery();
                            }
                        }
                        con.Close();
    
                        MessageBox.Show(this, "Save Successful", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        ClearForm();
                    }
                }
    
  • Registered Users

    အခုအလုပ်ရသွားပီ အစ်ကို အလုပ်မုာ မြန်မာ၃ လိုမရိ လို့ ေကျးဇူးနော် အစ်ကို

Sign In or Register to comment.