code_junkie

Update more than one database using same ObjectDataSource using C#

Nov 13th, 2011
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. public void DeleteMyObject(int objectId)
  2. {
  3. SqlConnection connectionOne = new SqlConnection("MyFirstDbConnection");
  4. SqlConnection connedtionTwo = new SqlConnection("MySecondDbCOnnection");
  5. SqlCommand myCommand = new SqlCommand(connectionOne);
  6. myCommand.CommandText = "DELETE FROM myTable where myid = " + objectId.ToString();
  7. connectionOne.Open();
  8. myCommand.ExecuteNonQuery();
  9. connectionOne.Close();
  10. myCommand.Connection = connectionTwo;
  11. connectionTwo.Open();
  12. myCommand.ExecuteNonQuery();
  13. connectionTwo.Close();
  14. }
  15.  
  16. string name = (string)e.InputParameters["name"];
  17.  
  18. int id = (int)e.InputParameters["id"];
  19.  
  20. if (string.IsNullOrEmpty(name))
  21. e.Cancel = true;
  22.  
  23. else
  24. {
  25. // Here insert in all Databases currently present
  26. // Except DB with StorageIndex = 1 as will b updated by ObjectDataSource1
  27.  
  28. for (int index = 2; index <= 3; index++)
  29. {
  30. string DataConnectionString = ConfigurationManager.AppSettings["DataConnectionString " + index]);
  31. SqlConnection con = new SqlConnection(DataConnectionString);
  32. string query = "UPDATE student SET [name] = @name WHERE (([id] = @id))";
  33. int cnt = Utils.executeQuery(query, con, new object[] { "@name", "@id" }, new object[] { name, id });
  34. }
  35. }
  36. }
Add Comment
Please, Sign In to add comment