我在<form>
Name:<br>
<input type="text" name="name" required><br>
Email:<br>
<input type="email" name="email" required><br>
Phone (Format: 999-999-9999):<br>
<input type="tel" name="phone" required pattern="\d{3}[\-]\d{3}[\-]\d{4}"><br>
Nature of comment:<br>
<input type="checkbox" name="comment" value="Question"> Question
<input type="checkbox" name="comment" value="Business Inquiry"> Business Inquiry
<input type="checkbox" name="comment" value="Comment"> Comment
<input type="checkbox" name="comment" value="Other"> Other <br>
Comment:<br>
<textarea></textarea><br>
<input type="submit" value="Submit">
<form onsubmit="return confirm('Do you really want to submit the form?');">
</form>
中有一个简单的注册程序,而不是只显示WinForms
中的记录我想将插入dataGridView
的数据放入textBox
}}。但是,除了tabControl
已在table
中为所有客户信息创建的SQL server
之外,我还需要为联系信息创建另一个table
,然后再显示它们在tabControl
中(一个tabPage
中的一般信息和另一个中的联系信息)。
我想知道如何连接在SQL Server中创建的两个表,因为每个客户端的每个记录都由两个表填充。
CREATE TABLE tblClientes (
ID int IDENTITY(1,1) PRIMARY KEY NOT NULL,
Nome varchar(255) NOT NULL,
Telefone varchar(255) NOT NULL,
Tipo varchar(255) NOT NULL, );
CREATE TABLE tblTelefone (
ID int NOT NULL,
Telefone varchar(255) NOT NULL,
PRIMARY KEY (ID));
string strConn= "Data Source=X\\X;Initial Catalog=X;User id=X;Password=X;";
string strCommtblCli = "SELECT * FROM tblClientes ORDER BY ID"; //table com informações gerais
string strCommtblCon = "SELECT * FROM tblTelefone ORDER BY ID"; //table de contato
SqlConnection objConn;
SqlCommand objComm;
SqlCommandBuilder cmdbldr;
SqlDataAdapter dataAdapter = new SqlDataAdapter();
DataSet dataSet = new DataSet("tblClientes");
string strConn= "Data Source=X\\X;Initial Catalog=X;User id=X;Password=X;";
string strCommtblCli = "SELECT * FROM tblClientes ORDER BY ID"; //table com informações gerais
string strCommtblCon = "SELECT * FROM tblTelefone ORDER BY ID"; //table de contato
SqlConnection objConn;
SqlCommand objComm;
SqlCommandBuilder cmdbldr;
SqlDataAdapter dataAdapter = new SqlDataAdapter();
DataSet dataSet = new DataSet("tblClientes");
public Form1()
{
InitializeComponent();
}
private void novoMenuItem_Click_1(object sender, EventArgs e)
{
alterarToolStripMenuItem.Enabled = false;
excluirToolStripMenuItem.Enabled = false;
groupBox1.Enabled = true;
reset();
txt_nome.Focus();
txt_registro.Enabled = false;
}
private void bt_pfisica_CheckedChanged_1(object sender, EventArgs e)
{
if (bt_pfisica.Checked)
{
txt_registro.Enabled = true;
txt_tipo.Text = "F";
txt_registro.Mask = "000,000,000-00";
}
}
private void bt_pjuridica_CheckedChanged_1(object sender, EventArgs e)
{
if (bt_pjuridica.Checked)
{
txt_registro.Enabled = true;
txt_tipo.Text = "J";
txt_registro.Mask = "00,000,000/0000-00";
}
}
//BOTÃO PARA ALTERAR UM CADASTRO
private void alterarToolStripMenuItem_Click_1(object sender, EventArgs e)
{
novoMenuItem.Enabled = false;
excluirToolStripMenuItem.Enabled = false;
if (dataGridView1.CurrentRow == null)
{
MessageBox.Show("Nenhum dado para editar!", "Alerta");
fn_hab_opcoesMenu();
}
else
{
groupBox1.Enabled = true;
}
}
//BOTÃO PARA EXCLUIR UM CADASTRO
private void excluirToolStripMenuItem_Click_1(object sender, EventArgs e)
{
novoMenuItem.Enabled = false;
alterarToolStripMenuItem.Enabled = false;
objConn = new SqlConnection(strConn);
dataAdapter = new SqlDataAdapter(strCommtblCli, objConn);
cmdbldr = new SqlCommandBuilder(dataAdapter);
if (dataGridView1.CurrentRow == null)
{
MessageBox.Show("Nenhum dado a excluir!", "Alerta");
}
else
{
int num_linha = dataGridView1.CurrentRow.Index;
dataSet.Tables["tblClientes"].Rows[num_linha].Delete();
dataAdapter.Update(dataSet, "tblClientes");
}
reset();
}
//BOTÃO PARA SALVAR O CADASTRO
private void bt_salva_Click(object sender, EventArgs e)
{
int num_charTextTel = txt_telefone.Text.Length;
int num_charMaskTel = txt_telefone.Mask.Length;
int num_charTextReg = txt_registro.Text.Length;
int num_charMaskReg = txt_registro.Mask.Length;
if (txt_nome.Text == string.Empty)
{
MessageBox.Show("Campo nome de preenchimento obrigatório.", "Alerta!");
txt_nome.Focus();
}
else if (num_charTextTel != num_charMaskTel)
{
MessageBox.Show("Campo telefone de preenchimento obrigatório.", "Alerta!");
txt_telefone.Focus();
}
else if (txt_registro.Enabled == false)
{
MessageBox.Show("Escolha o tipo de pessoa.", "Alerta!");
}
else if (num_charTextReg != num_charMaskReg)
{
MessageBox.Show("Campo registro de preenchimento obrigatório.", "Alerta!");
txt_registro.Focus();
}
else
{
if (bt_pfisica.Checked)
{
txt_tipo.Text = "F";
}
else
{
txt_tipo.Text = "J";
}
if (novoMenuItem.Enabled)
{
incluir();
Form1_Load_1(sender, e);
}
else if (alterarToolStripMenuItem.Enabled)
{
altera();
}
reset();
groupBox1.Enabled = false;
}
}
public void incluir()
{
objConn = new SqlConnection(strConn);
objComm = new SqlCommand(strCommtblCli, objConn);
objComm.CommandText = "INSERT INTO tblClientes (Nome,Telefone, Registro, Tipo) VALUES (@Nome, @Telefone,@Registro,@Tipo)";
try
{
objConn.Open();
objComm.Parameters.AddWithValue("@Nome", txt_nome.Text);
objComm.Parameters.AddWithValue("@Telefone", txt_telefone.Text);
objComm.Parameters.AddWithValue("@Registro", txt_registro.Text);
objComm.Parameters.AddWithValue("@Tipo", txt_tipo.Text);
dataAdapter.Update(dataSet, "tblClientes");
objComm.ExecuteNonQuery();
dataSet.Clear();
objConn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void altera()
{
try
{
objConn = new SqlConnection(strConn);
objComm = new SqlCommand(strCommtblCli, objConn);
cmdbldr = new SqlCommandBuilder(dataAdapter);
objConn.Open();
string ID = dataGridView1.CurrentRow.Cells["ID"].Value.ToString();
objComm.CommandText = "UPDATE tblClientes SET Nome=@Nome,Telefone=@Telefone,Registro=@Registro,Tipo=@Tipo WHERE ID=" + ID;
objComm.Parameters.AddWithValue("@Nome", txt_nome.Text);
objComm.Parameters.AddWithValue("@Telefone", txt_telefone.Text);
objComm.Parameters.AddWithValue("@Registro", txt_registro.Text);
objComm.Parameters.AddWithValue("@Tipo", txt_tipo.Text);
objComm.ExecuteNonQuery();
dataAdapter.Update(dataSet, "tblClientes");
dataSet.Clear();
dataAdapter.Fill(dataSet, "tblClientes");
objConn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_Load_1(object sender, EventArgs e)
{
groupBox1.Enabled = false;
objConn = new SqlConnection(strConn);
objComm = new SqlCommand(strCommtblCli, objConn);
objConn.Open();
dataAdapter = new SqlDataAdapter(strCommtblCli, objConn);
dataAdapter.Fill(dataSet, "tblClientes");
dataGridView1.DataSource = dataSet;
dataGridView1.DataMember = "tblClientes";
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
txt_nome.Text = dataGridView1.CurrentRow.Cells["Nome"].Value.ToString();
txt_telefone.Text = dataGridView1.CurrentRow.Cells["Telefone"].Value.ToString();
txt_registro.Text = dataGridView1.CurrentRow.Cells["Registro"].Value.ToString();
txt_tipo.Text = dataGridView1.CurrentRow.Cells["Tipo"].Value.ToString();
if (txt_tipo.Text == "F") bt_pfisica.Checked = true;
else bt_pjuridica.Checked = true;
}
private void Form1_FormClosing(object sender, FormClosedEventArgs e)
{
objConn.Close();
}
public Form1()
{
InitializeComponent();
}
private void novoMenuItem_Click_1(object sender, EventArgs e)
{
alterarToolStripMenuItem.Enabled = false;
excluirToolStripMenuItem.Enabled = false;
groupBox1.Enabled = true;
reset();
txt_nome.Focus();
txt_registro.Enabled = false;
}
private void bt_pfisica_CheckedChanged_1(object sender, EventArgs e)
{
if (bt_pfisica.Checked)
{
txt_registro.Enabled = true;
txt_tipo.Text = "F";
txt_registro.Mask = "000,000,000-00";
}
}
private void bt_pjuridica_CheckedChanged_1(object sender, EventArgs e)
{
if (bt_pjuridica.Checked)
{
txt_registro.Enabled = true;
txt_tipo.Text = "J";
txt_registro.Mask = "00,000,000/0000-00";
}
}
//BOTÃO PARA ALTERAR UM CADASTRO
private void alterarToolStripMenuItem_Click_1(object sender, EventArgs e)
{
novoMenuItem.Enabled = false;
excluirToolStripMenuItem.Enabled = false;
if (dataGridView1.CurrentRow == null)
{
MessageBox.Show("Nenhum dado para editar!", "Alerta");
fn_hab_opcoesMenu();
}
else
{
groupBox1.Enabled = true;
}
}
//BOTÃO PARA EXCLUIR UM CADASTRO
private void excluirToolStripMenuItem_Click_1(object sender, EventArgs e)
{
novoMenuItem.Enabled = false;
alterarToolStripMenuItem.Enabled = false;
objConn = new SqlConnection(strConn);
dataAdapter = new SqlDataAdapter(strCommtblCli, objConn);
cmdbldr = new SqlCommandBuilder(dataAdapter);
if (dataGridView1.CurrentRow == null)
{
MessageBox.Show("Nenhum dado a excluir!", "Alerta");
}
else
{
int num_linha = dataGridView1.CurrentRow.Index;
dataSet.Tables["tblClientes"].Rows[num_linha].Delete();
dataAdapter.Update(dataSet, "tblClientes");
}
reset();
}
//BOTÃO PARA SALVAR O CADASTRO
private void bt_salva_Click(object sender, EventArgs e)
{
int num_charTextTel = txt_telefone.Text.Length;
int num_charMaskTel = txt_telefone.Mask.Length;
int num_charTextReg = txt_registro.Text.Length;
int num_charMaskReg = txt_registro.Mask.Length;
if (txt_nome.Text == string.Empty)
{
MessageBox.Show("Campo nome de preenchimento obrigatório.", "Alerta!");
txt_nome.Focus();
}
else if (num_charTextTel != num_charMaskTel)
{
MessageBox.Show("Campo telefone de preenchimento obrigatório.", "Alerta!");
txt_telefone.Focus();
}
else if (txt_registro.Enabled == false)
{
MessageBox.Show("Escolha o tipo de pessoa.", "Alerta!");
}
else if (num_charTextReg != num_charMaskReg)
{
MessageBox.Show("Campo registro de preenchimento obrigatório.", "Alerta!");
txt_registro.Focus();
}
else
{
if (bt_pfisica.Checked)
{
txt_tipo.Text = "F";
}
else
{
txt_tipo.Text = "J";
}
if (novoMenuItem.Enabled)
{
incluir();
Form1_Load_1(sender, e);
}
else if (alterarToolStripMenuItem.Enabled)
{
altera();
}
reset();
groupBox1.Enabled = false;
}
}
public void incluir()
{
objConn = new SqlConnection(strConn);
objComm = new SqlCommand(strCommtblCli, objConn);
objComm.CommandText = "INSERT INTO tblClientes (Nome,Telefone, Registro, Tipo) VALUES (@Nome, @Telefone,@Registro,@Tipo)";
try
{
objConn.Open();
objComm.Parameters.AddWithValue("@Nome", txt_nome.Text);
objComm.Parameters.AddWithValue("@Telefone", txt_telefone.Text);
objComm.Parameters.AddWithValue("@Registro", txt_registro.Text);
objComm.Parameters.AddWithValue("@Tipo", txt_tipo.Text);
dataAdapter.Update(dataSet, "tblClientes");
objComm.ExecuteNonQuery();
dataSet.Clear();
objConn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void altera()
{
try
{
objConn = new SqlConnection(strConn);
objComm = new SqlCommand(strCommtblCli, objConn);
cmdbldr = new SqlCommandBuilder(dataAdapter);
objConn.Open();
string ID = dataGridView1.CurrentRow.Cells["ID"].Value.ToString();
objComm.CommandText = "UPDATE tblClientes SET Nome=@Nome,Telefone=@Telefone,Registro=@Registro,Tipo=@Tipo WHERE ID=" + ID;
objComm.Parameters.AddWithValue("@Nome", txt_nome.Text);
objComm.Parameters.AddWithValue("@Telefone", txt_telefone.Text);
objComm.Parameters.AddWithValue("@Registro", txt_registro.Text);
objComm.Parameters.AddWithValue("@Tipo", txt_tipo.Text);
objComm.ExecuteNonQuery();
dataAdapter.Update(dataSet, "tblClientes");
dataSet.Clear();
dataAdapter.Fill(dataSet, "tblClientes");
objConn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_Load_1(object sender, EventArgs e)
{
groupBox1.Enabled = false;
objConn = new SqlConnection(strConn);
objComm = new SqlCommand(strCommtblCli, objConn);
objConn.Open();
dataAdapter = new SqlDataAdapter(strCommtblCli, objConn);
dataAdapter.Fill(dataSet, "tblClientes");
dataGridView1.DataSource = dataSet;
dataGridView1.DataMember = "tblClientes";
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
txt_nome.Text = dataGridView1.CurrentRow.Cells["Nome"].Value.ToString();
txt_telefone.Text = dataGridView1.CurrentRow.Cells["Telefone"].Value.ToString();
txt_registro.Text = dataGridView1.CurrentRow.Cells["Registro"].Value.ToString();
txt_tipo.Text = dataGridView1.CurrentRow.Cells["Tipo"].Value.ToString();
if (txt_tipo.Text == "F") bt_pfisica.Checked = true;
else bt_pjuridica.Checked = true;
}
private void Form1_FormClosing(object sender, FormClosedEventArgs e)
{
objConn.Close();
}