code_junkie

good way to query many databases in ASP.NET

Nov 12th, 2011
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. public void BindData(string mySQL)
  2. {
  3. OracleConnection myConnection;
  4. OracleDataAdapter TempDataAdapter;
  5. DataSet MainDataSet = new DataSet();
  6. DataTable MainDataTable = new DataTable();
  7. DataSet TempDataSet;
  8. DataTable TempDataTable;
  9. string connectionString = "";
  10. Label1.Visible = false;
  11. Label1.Text = "";
  12.  
  13. foreach (ListItem li in CheckBoxList1.Items)
  14. {
  15. if (li.Selected)
  16. {
  17. connectionString = "Data Source=" + li.Text + "";
  18. connectionString += ";Persist Security Info=True;User ID=user;Password=pass;Unicode=True";
  19. myConnection = new OracleConnection(connectionString);
  20. try
  21. {
  22. TempDataAdapter = new OracleDataAdapter(mySQL, myConnection);
  23. TempDataSet = new DataSet();
  24. TempDataTable = new DataTable();
  25. TempDataAdapter.Fill(TempDataSet);
  26. TempDataTable = TempDataSet.Tables[0].Copy();
  27. /* If the main dataset is empty, create a table by cloning from temp dataset, otherwise
  28. copy all rows to existing table.*/
  29. if (MainDataSet.Tables.Count == 0)
  30. {
  31. MainDataSet.Tables.Add(TempDataTable);
  32. MainDataTable = MainDataSet.Tables[0];
  33. }
  34. else
  35. {
  36. foreach (DataRow dr in TempDataTable.Rows)
  37. {
  38. MainDataTable.ImportRow(dr);
  39. }
  40. }
  41. }
  42. catch (OracleException e)
  43. {
  44. Label1.Visible = true;
  45. Label1.Text = Label1.Text + e.Message + " on " + li.Text + "<br>";
  46.  
  47. }
  48. finally
  49. {
  50. if (myConnection != null)
  51. {
  52. myConnection.Close();
  53. myConnection = null;
  54. }
  55. TempDataSet = null;
  56. TempDataAdapter = null;
  57. TempDataTable = null;
  58.  
  59. }
  60. }
  61. }
  62. GridView1.DataSourceID = String.Empty;
  63. if (MainDataSet.Tables.Count != 0)
  64. {
  65. GridView1.DataSource = MainDataSet;
  66. if (GridView1.DataSource != null)
  67. {
  68. GridView1.DataBind();
  69. }
  70. }
  71. }
  72. protected void Button1_Click(object sender, EventArgs e)
  73. {
  74. BindData(TextBox1.Text);
  75. }
  76.  
  77. public void BindData(string mySQL)
  78. {
  79. OracleConnection myConnection;
  80. // Empty connection string for now
  81. OracleDataAdapter MainDataAdapter = new OracleDataAdapter(mySQL, "");
  82. DataTable MainDataTable = new DataTable();
  83. string connectionString = "";
  84. Label1.Visible = false;
  85. Label1.Text = "";
  86.  
  87. foreach (ListItem li in CheckBoxList1.Items)
  88. {
  89. if (li.Selected)
  90. {
  91. connectionString = "Data Source=" + li.Text + "";
  92. connectionString += ";Persist Security Info=True;User ID=user;Password=pass;Unicode=True";
  93. MainDataAdapter.SelectCommand.Connection.ConnectionString = connectionString
  94. try
  95. {
  96. MainDataAdapter.Fill(MainDataTable);
  97. }
  98. catch (OracleException e)
  99. {
  100. Label1.Visible = true;
  101. Label1.Text = Label1.Text + e.Message + " on " + li.Text + "<br>";
  102. }
  103. }
  104. }
  105. GridView1.DataSourceID = String.Empty;
  106. GridView1.DataSource = MainDataTable;
  107. GridView1.DataBind();
  108. }
Add Comment
Please, Sign In to add comment