你好,我一直使用的SqlCommand非查询,但现在事情错了,我不知道我有业务更新插入3个按钮和删除,但我创造了独特的方法对所有3个操作,问题是,它不会插入删除或更新:
private void operacao(String operacao) {
String comando = "";
con = new SqlConnection();
WorksDataSet dataset = new WorksDataSet();
con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Works.mdf;Integrated Security=True;User Instance=True;Asynchronous Processing=true";
try
{
con.Open();
}
catch (SqlException cox) {
MessageBox.Show(cox.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
switch (operacao) {
case "inserir":
try
{
comando = "Insert Into Estudante (Codigo,Nome,Apelido) values(" + txtID.Text + ",'" + txtNome.Text + "','" + txtapelido.Text + "')";
SqlCommand command = new SqlCommand(comando, con);
SqlDataAdapter sda=new SqlDataAdapter(command);
command.CommandType = CommandType.Text;
sda.Fill(dataset);
command.ExecuteNonQuery();
command.Dispose();
MessageBox.Show("Adicionado com Sucesso", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (SqlException sex) {
MessageBox.Show(sex.Message , this.Text,MessageBoxButtons.OK,MessageBoxIcon.Error );
}
break;
case "apagar":
comando = "delete from Estudante where Codigo=" + txtID;
try
{
SqlCommand command = new SqlCommand(comando, con);
command.BeginExecuteNonQuery();
MessageBox.Show("Removido com Sucesso", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (SqlException sex)
{
MessageBox.Show(sex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
break;
case "atualizar":
comando = "update table Estudante set nome='" + txtNome + "'^ apelido='" + txtapelido + "'";
try
{
SqlCommand command = new SqlCommand(comando, con);
command.BeginExecuteNonQuery();
MessageBox.Show("Actualizado com Sucesso", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (SqlException sex)
{
MessageBox.Show(sex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
break;
default:
break
;
}
con.Close();
}