Guest User

Untitled

a guest
Aug 24th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. Getting a connection to database lost error
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Data;
  7. using System.Windows.Forms;
  8. using FirebirdSql.Data.FirebirdClient;
  9.  
  10. namespace _0912111
  11. {
  12. class DatabaseConnection
  13. {
  14. private FbConnection conn;
  15. private FbCommand sqlCommand;
  16. private FbDataAdapter DB;
  17. private DataSet DS = new DataSet();
  18.  
  19. public DatabaseConnection()
  20. {
  21. conn = new FbConnection("User=myuser;" + "Password=mypw;" + "Database=dbpath;" + "DataSource=serverip;" + "Port=dbport;" + "Dialect=3;" + "Charset=UTF8;");
  22. }
  23. public void showDbError(string theError)
  24. {
  25. MessageBox.Show("Could not connect to databasennError Details:n" + theError);
  26. }
  27. public FbConnection Openconn()
  28. {
  29. if (conn.State == ConnectionState.Closed || conn.State == ConnectionState.Broken)
  30. {
  31. try
  32. {
  33. conn.Open();
  34. }
  35. catch (Exception e)
  36. {
  37. showDbError(e.Message.ToString());
  38. }
  39. }
  40. return conn;
  41. }
  42. public FbConnection Closeconn()
  43. {
  44. if (conn.State == ConnectionState.Open)
  45. {
  46. conn.Close();
  47. }
  48. return conn;
  49. }
  50. public void nonQuery(string txtQuery)
  51. {
  52. FbCommand cmd = new FbCommand(txtQuery);
  53. try
  54. {
  55. cmd.Connection = Openconn();
  56. cmd.ExecuteNonQuery();
  57. }
  58. catch (Exception Ex)
  59. {
  60. showDbError(Ex.Message.ToString());
  61. throw Ex;
  62. }
  63. finally
  64. {
  65. cmd = null;
  66. }
  67. }
  68. public FbDataReader returnDataReader(string txtQuery)
  69. {
  70. FbCommand cmd = new FbCommand();
  71. try
  72. {
  73. cmd.Connection = Openconn();
  74. cmd.CommandText = txtQuery;
  75. FbDataReader rd;
  76. rd = cmd.ExecuteReader();
  77. return rd;
  78. }
  79. catch (Exception Ex)
  80. {
  81. showDbError(Ex.Message.ToString());
  82. throw Ex;
  83. }
  84. finally
  85. {
  86. cmd = null;
  87. }
  88. }
  89. }
  90. }
  91.  
  92. public void nonQuery(string txtQuery)
  93. {
  94. using(var conn = new FbConnection(GetMyConnectionString(...parameters...)))
  95. {
  96. using(var cmd = new FbCommand(txtQuery))
  97. {
  98. try
  99. {
  100. cmd.Connection = conn;
  101. conn.Open();
  102.  
  103. cmd.ExecuteNonQuery();
  104. }
  105. catch (Exception ex)
  106. {
  107. showDbError(ex.Message);
  108. throw;
  109. }
  110. }
  111. }
  112. }
Add Comment
Please, Sign In to add comment