Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Npgsql;
- namespace GE_2._0._1
- {
- class conexao
- {
- //parametros de conexão
- static string serverName = "localhost";
- static string port = "5432";
- static string userName = "postgres";
- static string password = "adm";
- static string databaseName = "dbge2";
- NpgsqlConnection objCon = null;
- string vsql = null;
- //métodos
- public bool conectar() //Abre a conexão com o banco de dados
- {
- vsql = String.Format("Server={0};Port={1};User Id={2};Password={3};Database={4};",
- serverName, port, userName, password, databaseName);
- try
- {
- objCon = new NpgsqlConnection(vsql);
- objCon.Open();
- return true;
- }
- catch
- {
- return false;
- }
- }
- public bool desconectar() //Fecha conexão
- {
- if(objCon.State != System.Data.ConnectionState.Closed) //Verifica o estado da conexão
- {
- objCon.Close();
- objCon.Dispose();
- return true;
- }
- else
- {
- objCon.Dispose();
- return false;
- }
- }
- }
- }
- using System;
- using System.Collections;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Data;
- using Npgsql;
- using System.Data.SqlClient;
- namespace GE_2._0._1
- {
- class clientes
- {
- // #region "Métodos de execução SQL"
- public bool Inserir(ArrayList p_arrList)
- {
- conexao con = new conexao();
- string sql = String.Format("INSERT INTO CLIENTE (NOME, CPFCNPJ, CEP, ENDERECO, CIDADE, EMAIL, TELEFONE)" +
- " VALUES (@NOME, @CPFCNPJ, @CEP, @ENDERECO, @CIDADE, @EMAIL, @TELEFONE) ");
- NpgsqlCommand objcmd = null;
- if (con.conectar())
- {
- try
- {
- objcmd = new NpgsqlCommand(sql);
- objcmd.Parameters.Add(new NpgsqlParameter("@NOME", p_arrList[0]));
- objcmd.Parameters.Add(new NpgsqlParameter("@CPFCNPJ", p_arrList[1]));
- objcmd.Parameters.Add(new NpgsqlParameter("@CEP", p_arrList[2]));
- objcmd.Parameters.Add(new NpgsqlParameter("@ENDERECO", p_arrList[3]));
- objcmd.Parameters.Add(new NpgsqlParameter("@CIDADE", p_arrList[4]));
- objcmd.Parameters.Add(new NpgsqlParameter("@EMAIL", p_arrList[5]));
- objcmd.Parameters.Add(new NpgsqlParameter("@TELEFONE", p_arrList[6]));
- objcmd.ExecuteNonQuery();
- return true;
- }
- catch (NpgsqlException ex)
- {
- throw ex;
- }
- finally
- {
- con.desconectar();
- }
- }
- else
- {
- return false;
- }
- }
- /**
- public bool Atualizar()
- {
- }
- public bool Deletar()
- {
- }
- #endregion
- #region "Métodos para Listar e fazer pesquisa"
- public DateTable ListaGrid()
- {
- }
- public DataTable Pesquisar()
- {
- }
- #endregion**/
- }
- }
- System.InvalidOperationException ocorrido
- HResult=0x80131509
- Message=Connection property has not been initialized.
- Source=Npgsql
- StackTrace:
- em Npgsql.NpgsqlCommand.CheckReadyAndGetConnector()
- em Npgsql.NpgsqlCommand.<ExecuteNonQuery>d__84.MoveNext()
- em System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
- em System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
- em Npgsql.NpgsqlCommand.ExecuteNonQuery()
- em GE_2._0._1.clientes.Inserir(ArrayList p_arrList) em C:UserswilliansourcereposGE 2.0.1GE 2.0.1clientes.cs:linha 36
- em GE_2._0._1.Form1.TestDB() em C:UserswilliansourcereposGE 2.0.1GE 2.0.1Form1.cs:linha 44
- em GE_2._0._1.Form1.button1_Click(Object sender, EventArgs e) em C:UserswilliansourcereposGE 2.0.1GE 2.0.1Form1.cs:linha 57
- em System.Windows.Forms.Control.OnClick(EventArgs e)
- em System.Windows.Forms.Button.OnClick(EventArgs e)
- em System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
- em System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
- em System.Windows.Forms.Control.WndProc(Message& m)
- em System.Windows.Forms.ButtonBase.WndProc(Message& m)
- em System.Windows.Forms.Button.WndProc(Message& m)
- em System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
- em System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
- em System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
- em System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
- em System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
- em System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
- em System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
- em System.Windows.Forms.Application.Run(Form mainForm)
- 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