Friend, do the following. create a class: I called mine "Dal"
mine is like this>
using Npgsql;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
public class Dal
{
static string serverName = "10.1.1.2";
static string port = "5438";
static string userName = "username";
static string password = "suasenha";
static string databaseName = "seubanco";
NpgsqlConnection conn = null;
string connString = null;
public Dal()
{
connString = String.Format("Host={0};Port={1};User Id={2};Password={3};Database={4}; Pooling=false; Timeout=100",
serverName, port, userName, password, databaseName);
conn = new NpgsqlConnection(connString);
conn.Close();
}
public void InserirRegistros(string Carro, string Placa, string Fabricante, string Ano, string Cor, string Status, string Data)
{
try
{
conn.Open();
NpgsqlTransaction transacao = conn.BeginTransaction();
StringBuilder sb = new StringBuilder();
String sqlTcc = "INSERT INTO tcc (Carro,Placa,Fabricante,Ano,Cor,Status,Data) VALUES (@Carro,@Placa,@Fabricante,@Ano,@Cor,@Status,@Data)";
NpgsqlCommand cmdTcc = new NpgsqlCommand(sqlTcc, conn, transacao);
cmdTcc.Parameters.AddWithValue("@Carro", Carro);
cmdTcc.Parameters.AddWithValue("@Placa", Placa);
cmdTcc.Parameters.AddWithValue("@Fabricante", Fabricante);
cmdTcc.Parameters.AddWithValue("@Ano", Ano);
cmdTcc.Parameters.AddWithValue("@Cor", Cor);
cmdTcc.Parameters.AddWithValue("@Status", Status);
cmdTcc.Parameters.AddWithValue("@Data", Data);
cmdTcc.ExecuteNonQuery();
transacao.Commit();
conn.Close();
}
catch (NpgsqlException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
}
}
And then do it:
serialPort1.Open();
string entrada = serialPort1.ReadLine();
txtIDTag.Text = entrada;
serialPort1.Close();
Dal Insert = new Dal();
if(entrada=="1")
{
Insert.InserirRegistros("seus parametros...");
}
I have error in the "Npgsqltransaction" section. I added more information in the post
– Jon