I'd suggest splitting out the table creation part to a separate command. If the table doesn't exist, SQL might struggle to prepare the query to insert data into it.
Your
INSERT
command is currently missing a
VALUES
clause, so you should be getting an exception telling you the syntax is not correct.
using (SqlConnection conn = new SqlConnection(@"Data source = 2c4138928627\Sage ; Database=ARMINDOData ; User Id=sa ; Password=sage2008+"))
{
conn.Open();
using (SqlCommand command = new SqlCommand("IF OBJECT_ID('UXFaturas', 'U') IS NULL CREATE TABLE UXFaturas(NumFaturas char(250) NOT NULL, Cont1 char(250) NOT NULL, Cont2 char(250) NOT NULL)", conn))
{
command.CommandType = CommandType.Text;
command.ExecuteNonQuery();
}
using (SqlCommand command = new SqlCommand("INSERT INTO UXFaturas (NumFaturas, Cont, Cont2) VALUES (@NumFaturas, @Cont, @Cont2)", conn))
{
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("@NumFaturas", numfatura);
command.Parameters.AddWithValue("@Cont1", cont);
command.Parameters.AddWithValue("@Cont2", cont2);
command.ExecuteNonQuery();
}
}
INSERT (Transact-SQL) | Microsoft Docs[
^]
NB: Ideally, you should avoid connecting as
sa
. That user has unrestricted access to the entire server, which your application doesn't need. Instead, you should use an account which has only the permissions required by your application.