Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > Forum > connection 2 server ต้องการ insert data โดยใช้ commit and rollblack จะใช้อย่างไร



 

connection 2 server ต้องการ insert data โดยใช้ commit and rollblack จะใช้อย่างไร

 



Topic : 057335



โพสกระทู้ ( 160 )
บทความ ( 0 )



สถานะออฟไลน์




ผมใช้ database 2 ตัว และต้อง insert ข้อมูล เข้าทั้ง 2 database แต่ว่าข้อมูลใน database แรก data type เกือบทั้งหมดจะเป็น varchar ยกเว้น id
แต่ database ตัวที่ 2 จะต้องใช้ data type เป็นข้อมูลตามจริง จะใช้ Transaction แบบไหน


ผมพยายามทำตาม https://www.thaicreate.com/asp.net/c-sharp-asp.net-sql-server-transaction-commit-rollback.html นี้แล้ว มันติด
ERROR:ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized.

แล้วก็ตามไปดู http://webcache.googleusercontent.com/search?q=cache:qaWRgpfkV2kJ:www.thaicreate.com/dotnet/forum/024807.html+ExecuteNonQuery+requires+the+command+to+have+a+transaction+when+the+connection+assigned+to+the+command+is+in+a+pending+local+transaction.+The+Transaction+property+of+the+command+has+not+been+initialized.&cd=1&hl=th&ct=clnk&gl=th&source=www.google.co.th

แต่ไม่มีทางออก



Code (C#)
    //connection for win
    SqlConnection connString = new SqlConnection("Data Source=CLONENO003\\SQLSERVER;Initial Catalog=windigi;Integrated Security=True;");

    //connection for other brand
    SqlConnection connStringBranID = new SqlConnection("Data Source=CLONENO003\\SQLSERVER;Initial Catalog=siDB;Integrated Security=True;");

    SqlCommand objCmd = new SqlCommand();


    SqlCommand objCmdBrand = new SqlCommand();

   





Code (C#)
  Boolean RegisterMemberWindigi(string p_strName, string p_strSurname, string p_strAddress,string p_strNationalID,string p_strCareerPosition,string p_strTelNo,int p_intAge, string p_strEmail,int p_intMarital,string p_strGender, string p_strUserName, string p_strPassword,int p_intLifeStyleTechnology,int p_intLifeStyleSport,int p_intLifeStyleMusic,int p_intLifeStyleMovie,int p_intLifeStyleDining,int p_intLifeStyleBook,int p_intLuxuryWatch,int p_intLuxuryJewelry,int p_intLuxuryCar,int p_intFashionStylish,int p_intFashionGlamour,int p_intFashionCasual,string p_strIncomeLevel,string p_strEducationLevel,string p_strChildren,string p_strCareerTitle)

    {
        connString.Open();
        connStringBranID.Open();
        
        SqlTransaction transactionWin;
        transactionWin = connString.BeginTransaction();

        SqlTransaction transactionSi ;
        transactionSi = connStringBranID.BeginTransaction();       


        try
        {
            objCmd.Connection = connString;
            objCmd.Transaction =transactionWin;  //*** Command & Transaction ***//            

            //FrmRegTxtNationalID
            string sqlRegisMember = "INSERT INTO tbl_user("
                                + "userPassword,userName,userSurname,userAge"
                                + " ,userGender,userEmail,userTelno,userAddress"
                                + " ,userIncomeID"
                                + " ,userEducationID,userMaritalStatus,userKids,userNationalID"
                                + " ,userCareerPosition,userPrefFashionCasual,userPrefFashionGlamour,userPrefFashionStylish"
                                + " ,userPrefLuxCar,userPrefLuxJewelry,userPrefLuxWatch,userCareerTitleID"
                                + " ,userPrefLifeBook,userPrefLifeDining,userPrefLifeMovie,userPrefLifeMusic,userPrefLifeSport,userPrefLifeTechnology)"
                            + " VALUES ('" + p_strPassword + "','" + p_strUserName + "','" + p_strSurname + "','N/A'," +
                                            "'N/A','" + p_strEmail + "','" + p_strTelNo + "','N/A'," +
                                            "'N/A'" + "," +
                                            "'N/A'" + ",'N/A','N/A','N/A'," +
                                            "'N/A'" + ",'N/A','N/A','N/A'," +
                                            "'N/A'" + ",'N/A','N/A','N/A'," +
                                            "'N/A'" + ",'N/A','N/A','N/A','N/A','N/A')";

            objCmd = new SqlCommand();
            objCmd.Connection = connString;
            objCmd.CommandText = sqlRegisMember;
            objCmd.CommandType = CommandType.Text;
            objCmd.ExecuteNonQuery();
       

            //Siam 
            objCmdBrand.Connection = connStringBranID;
            objCmdBrand.Transaction = transactionSi;//*** Command & Transaction ***//
            
            string sqlRegisMemberSi = "INSERT INTO tbl_user("
                                + "userPassword,userName,userSurname,userAge"
                                + " ,userGender,userEmail,userTelno,userAddress"
                                + " ,userIncomeID"
                                + " ,userEducationID,userMaritalStatus,userKids,userNationalID"
                                + " ,userCareerPosition,userPrefFashionCasual,userPrefFashionGlamour,userPrefFashionStylish"
                                + " ,userPrefLuxCar,userPrefLuxJewelry,userPrefLuxWatch,userCareerTitleID"
                                + " ,userPrefLifeBook,userPrefLifeDining,userPrefLifeMovie,userPrefLifeMusic,userPrefLifeSport,userPrefLifeTechnology)"
                            + " VALUES ('" + p_strPassword + "','" + p_strUserName + "','" + p_strSurname + "'," + p_intAge + "," +
                                            "'" + p_strGender + "','" + p_strEmail + "','" + p_strTelNo + "','" + p_strAddress + "'," +
                                            "'" + p_strIncomeLevel + "'," +
                                            "'" + p_strEducationLevel + "'" + ",'" + p_intMarital + "','" + p_strChildren + "','" + p_strNationalID + "'," +
                                            "'" + p_strCareerPosition + "'" + ",'" + p_intFashionCasual + "','" + p_intFashionGlamour + "','" + p_intFashionStylish + "'," +
                                            "'" + p_intLuxuryCar + "','" + p_intLuxuryJewelry + "','" + p_intLuxuryWatch + "','" + p_strCareerTitle + "'," +
                                            "'" + p_intLifeStyleBook + "'" + ",'" + p_intLifeStyleDining + "','" + p_intLifeStyleMovie + "','" + p_intLifeStyleMusic + "','" + p_intLifeStyleSport + "','" + p_intLifeStyleTechnology + "')";


            objCmdBrand = new SqlCommand();
            objCmdBrand.Connection = connStringBranID;
            objCmdBrand.CommandText = sqlRegisMemberSi;
            objCmdBrand.CommandType = CommandType.Text;
            objCmdBrand.ExecuteNonQuery();

            transactionWin.Commit();
            transactionSi.Commit();
            return true;

        }
        catch (Exception ex)
        {
            transactionWin.Rollback();
            transactionSi.Rollback();
            
            Response.Write("<b><font color='red'>Created member failed (" + ex.Message + ")</font></b>");
            return false;

            
        }

        finally
        {
            connString.Close();
            connStringBranID.Close();
        }

       
    }




Tag : .NET, Web (ASP.NET), C#, VS 2010 (.NET 4.x)









ประวัติการแก้ไข
2011-03-16 14:31:19
2011-03-16 15:47:37
2011-03-16 15:47:54
2011-03-16 15:48:07
2011-03-16 15:48:47
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2011-03-16 14:27:07 By : chon2008 View : 1248 Reply : 1
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

สร้าง Connection 2 ตัวและกับเปิด Transaction 2 ตัว ของใครของมัน ทำการ Rollback ใน try{} คอบเอาไว้ ก็น่าจะสามารถทำได้น่ะครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2011-03-16 17:32:16 By : webmaster
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : connection 2 server ต้องการ insert data โดยใช้ commit and rollblack จะใช้อย่างไร
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 01
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่