1
I’m new here and also in the world of programming, today I managed to connect C# to SQL, by making a simple connection with the BD I can insert records, but when trying to insert 2 data in the same table simultaneously it creates 2 entries or just inserts the last one, please help me, follow the code below...
private void bt_Cadastrar_Click(object sender, EventArgs e)
{
if (bt_Cadastrar.Text == "Salvar")
{
tb_ID.Enabled = false;
bt_Cadastrar.Text = "Cadastrar";
}
Int16 Cheque, Convenio;
if (cb_Cheque.Checked) Cheque = 1;
else Cheque = 0;
if (cb_Convenio.Checked) Convenio = 1;
else Convenio = 0;
SqlConnection conn = new SqlConnection("Server= localhost; Database=Cadastros; Integrated Security=true"); //Cria a conexão com o Banco de dados devido a Dll que tem na pasta do programa.
SqlCommand cmd = new SqlCommand("insert into ccliente (Nome) values ('" + tb_Nome.Text.ToUpper() + "')", conn); //Comando SQL para criar inserir um item em uma tabela existente
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
/*
cmd = new SqlCommand("insert into ccliente (CPF) values ('" + tb_CPF.Text.ToUpper() + "')", conn);
cmd = new SqlCommand("insert into ccliente (RG) values ('" + tb_RG.Text.ToUpper() + "')", conn);
cmd = new SqlCommand("insert into ccliente (CEP) values ('" + tb_CEP.Text.ToUpper() + "')", conn);
cmd = new SqlCommand("insert into ccliente (Cidade) values ('" + tb_Cidade.Text.ToUpper() + "')", conn);
cmd = new SqlCommand("insert into ccliente (Email) values ('" + tb_Email.Text.ToUpper() + "')", conn);
cmd = new SqlCommand("insert into ccliente (Telefone1) values ('" + tb_Telefone1.Text.ToUpper() + "')", conn);
cmd = new SqlCommand("insert into ccliente (Telefone2) values ('" + tb_Telefone2.Text.ToUpper() + "')", conn);
cmd = new SqlCommand("insert into ccliente (Observacao) values ('" + tb_Observacao.Text.ToUpper() + "')", conn);
cmd = new SqlCommand("insert into ccliente (Cheque) values ('" + Cheque + "')", conn);
cmd = new SqlCommand("insert into ccliente (Convenio) values ('" + Convenio + "')", conn);
*/
}
If necessary follow below the link to the complete project: goo.Gl/6P8Hwc (Please, if any moderator thinks this is wrong can delete the link, I’m new here and do not know the rules yet).
Hi buddy, what do you mean, do two simultaneous records? recommend you create a specific class of access to the bank, as if it were a DAO, and then, if you want to enter more than one record, pass a list and insert one at a time with a foreach, understood?
– M. Bertolazo