Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. namespace WindowsFormsApplication1
  2. {
  3. static class Conexao
  4. {
  5.  
  6. private static String strConn = Properties.Settings.Default.caminhoFbConnection;
  7. private static FbConnection conn = null;
  8.  
  9.  
  10. public static void Conection()
  11. {
  12.  
  13. }
  14.  
  15. public static FbConnection getConnection()
  16. {
  17. try
  18. {
  19.  
  20. if (conn == null)
  21. {
  22. conn = new FbConnection(strConn);
  23. conn.Open();
  24. return conn;
  25. }
  26. else
  27. {
  28. if (conn.State == System.Data.ConnectionState.Open)
  29. {
  30. return conn;
  31. }
  32. else
  33. {
  34. conn.Open();
  35. return conn;
  36. }
  37. }
  38. }
  39. catch (Exception excep)
  40. {
  41. MessageBox.Show("Erro - " + excep.Message);
  42. return null;
  43. }
  44. }
  45.  
  46. public static void closeConnection()
  47. {
  48. try
  49. {
  50.  
  51. conn.Close();
  52.  
  53. }
  54. catch (Exception excep)
  55. {
  56. MessageBox.Show(excep.Message);
  57. }
  58. }
  59. }
  60. }
  61.  
  62. finally
  63. {
  64. fecharConexao(cn);
  65. }
  66.  
  67. using System;
  68. using System.Collections.Generic;
  69. using System.Linq;
  70. using System.Text;
  71. using System.Windows.Forms;
  72. using System.Data;
  73. using System.IO;
  74. using FirebirdSql.Data.FirebirdClient;
  75. using Demo_NFe.Code.BLL;
  76.  
  77. namespace MeuProjeto.Code.DAL
  78. {
  79. class ConexaoBDDAL
  80. {
  81. public FbConnection abrirConexao()
  82. {
  83. #region Método responsável por abrir a conexão com Banco de Dados
  84.  
  85. FbConnection conexao = null;
  86.  
  87. UtilitariosBLL util = new UtilitariosBLL();
  88.  
  89. try
  90. {
  91. string strConexao = String.Empty;
  92. string banco = "nome_banco"
  93. string servidor = "localhost";
  94. string usuario = "SYSDBA";
  95. string senha = "masterkey";
  96.  
  97. strConexao = @"User=" + usuario + "; "
  98. + @"Password=" + senha + "; "
  99. + @"Database=" + banco + "; "
  100. + @"DataSource=" + servidor + "; "
  101. + "Dialect=3; "
  102. + "Charset=WIN1252; "
  103. + "Role=; "
  104. + "Connectionlifetime =15; "
  105. + "Pooling =true; "
  106. + "MinPoolSize =0; "
  107. + "MaxPoolSize =50; "
  108. + "PacketSize =8192; "
  109. + "ServerType =0";
  110.  
  111. conexao = new FbConnection(strConexao);
  112.  
  113. if (conexao.State == ConnectionState.Closed)
  114. conexao.Open();
  115. }
  116. catch (Exception)
  117. {
  118. MessageBox.Show("Ocorreu um erro ao Estabelecer a Comunicação com o Banco de Dados, por favor verrifique se todas as configurações "
  119. + "foram informadas corretamente!", "Atenção!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  120. }
  121.  
  122. return conexao;
  123.  
  124. #endregion
  125. }
  126.  
  127.  
  128. //Método para Fechar a Conexão com o Banco de Dados
  129. public void fecharConexao(FbConnection cn)
  130. {
  131. if (cn.State == ConnectionState.Open)
  132. cn.Close();
  133. }
  134.  
  135. //Método para Exerculta comandos SQL
  136. public void executarComando(string strQuery)
  137. {
  138. FbConnection cn = new FbConnection();
  139.  
  140. try
  141. {
  142. cn = abrirConexao();
  143. FbCommand cmd = new FbCommand();
  144. cmd.CommandText = strQuery.ToString();
  145. cmd.CommandType = CommandType.Text;
  146. cmd.Connection = cn;
  147. cmd.ExecuteNonQuery();
  148. }
  149. catch (Exception)
  150. {
  151. MessageBox.Show("Ocorreu um erro ao Excultar o Comando SQL, Por Favor Certifique-se se o mesmo foi escrito corretamente!", "Atenção!", MessageBoxButtons.OK, MessageBoxIcon.Information);
  152. }
  153. finally
  154. {
  155. fecharConexao(cn);
  156. }
  157. }
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement