Guest User

Untitled

a guest
Oct 19th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Npgsql;
  7.  
  8. namespace GE_2._0._1
  9. {
  10. class conexao
  11. {
  12.  
  13. //parametros de conexão
  14. static string serverName = "localhost";
  15. static string port = "5432";
  16. static string userName = "postgres";
  17. static string password = "adm";
  18. static string databaseName = "dbge2";
  19. NpgsqlConnection objCon = null;
  20. string vsql = null;
  21.  
  22.  
  23.  
  24.  
  25.  
  26. //métodos
  27. public bool conectar() //Abre a conexão com o banco de dados
  28. {
  29. vsql = String.Format("Server={0};Port={1};User Id={2};Password={3};Database={4};",
  30. serverName, port, userName, password, databaseName);
  31. try
  32. {
  33. objCon = new NpgsqlConnection(vsql);
  34. objCon.Open();
  35. return true;
  36. }
  37. catch
  38. {
  39. return false;
  40. }
  41. }
  42.  
  43. public bool desconectar() //Fecha conexão
  44. {
  45. if(objCon.State != System.Data.ConnectionState.Closed) //Verifica o estado da conexão
  46. {
  47. objCon.Close();
  48. objCon.Dispose();
  49. return true;
  50. }
  51. else
  52. {
  53. objCon.Dispose();
  54. return false;
  55. }
  56. }
  57. }
  58. }
  59.  
  60. using System;
  61. using System.Collections;
  62. using System.Linq;
  63. using System.Text;
  64. using System.Threading.Tasks;
  65. using System.Data;
  66. using Npgsql;
  67. using System.Data.SqlClient;
  68.  
  69. namespace GE_2._0._1
  70. {
  71. class clientes
  72. {
  73. // #region "Métodos de execução SQL"
  74. public bool Inserir(ArrayList p_arrList)
  75. {
  76. conexao con = new conexao();
  77.  
  78. string sql = String.Format("INSERT INTO CLIENTE (NOME, CPFCNPJ, CEP, ENDERECO, CIDADE, EMAIL, TELEFONE)" +
  79. " VALUES (@NOME, @CPFCNPJ, @CEP, @ENDERECO, @CIDADE, @EMAIL, @TELEFONE) ");
  80. NpgsqlCommand objcmd = null;
  81.  
  82. if (con.conectar())
  83. {
  84. try
  85. {
  86. objcmd = new NpgsqlCommand(sql);
  87. objcmd.Parameters.Add(new NpgsqlParameter("@NOME", p_arrList[0]));
  88. objcmd.Parameters.Add(new NpgsqlParameter("@CPFCNPJ", p_arrList[1]));
  89. objcmd.Parameters.Add(new NpgsqlParameter("@CEP", p_arrList[2]));
  90. objcmd.Parameters.Add(new NpgsqlParameter("@ENDERECO", p_arrList[3]));
  91. objcmd.Parameters.Add(new NpgsqlParameter("@CIDADE", p_arrList[4]));
  92. objcmd.Parameters.Add(new NpgsqlParameter("@EMAIL", p_arrList[5]));
  93. objcmd.Parameters.Add(new NpgsqlParameter("@TELEFONE", p_arrList[6]));
  94.  
  95. objcmd.ExecuteNonQuery();
  96.  
  97. return true;
  98. }
  99. catch (NpgsqlException ex)
  100. {
  101. throw ex;
  102. }
  103. finally
  104. {
  105. con.desconectar();
  106. }
  107. }
  108. else
  109. {
  110. return false;
  111. }
  112. }
  113. /**
  114. public bool Atualizar()
  115. {
  116.  
  117. }
  118.  
  119. public bool Deletar()
  120. {
  121.  
  122. }
  123. #endregion
  124.  
  125. #region "Métodos para Listar e fazer pesquisa"
  126. public DateTable ListaGrid()
  127. {
  128.  
  129. }
  130.  
  131. public DataTable Pesquisar()
  132. {
  133.  
  134. }
  135. #endregion**/
  136. }
  137. }
  138.  
  139. System.InvalidOperationException ocorrido
  140. HResult=0x80131509
  141. Message=Connection property has not been initialized.
  142. Source=Npgsql
  143. StackTrace:
  144. em Npgsql.NpgsqlCommand.CheckReadyAndGetConnector()
  145. em Npgsql.NpgsqlCommand.<ExecuteNonQuery>d__84.MoveNext()
  146. em System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
  147. em System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  148. em Npgsql.NpgsqlCommand.ExecuteNonQuery()
  149. em GE_2._0._1.clientes.Inserir(ArrayList p_arrList) em C:UserswilliansourcereposGE 2.0.1GE 2.0.1clientes.cs:linha 36
  150. em GE_2._0._1.Form1.TestDB() em C:UserswilliansourcereposGE 2.0.1GE 2.0.1Form1.cs:linha 44
  151. em GE_2._0._1.Form1.button1_Click(Object sender, EventArgs e) em C:UserswilliansourcereposGE 2.0.1GE 2.0.1Form1.cs:linha 57
  152. em System.Windows.Forms.Control.OnClick(EventArgs e)
  153. em System.Windows.Forms.Button.OnClick(EventArgs e)
  154. em System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
  155. em System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
  156. em System.Windows.Forms.Control.WndProc(Message& m)
  157. em System.Windows.Forms.ButtonBase.WndProc(Message& m)
  158. em System.Windows.Forms.Button.WndProc(Message& m)
  159. em System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
  160. em System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
  161. em System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  162. em System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
  163. em System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
  164. em System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
  165. em System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
  166. em System.Windows.Forms.Application.Run(Form mainForm)
  167. em GE_2._0._1.Program.Main() em C:UserswilliansourcereposGE 2.0.1GE 2.0.1Program.cs:linha 19
Add Comment
Please, Sign In to add comment