你好,我總是使用SQlcommand非查詢,但現在錯了我不知道我有什麼3按鈕操作更新插入和刪除,但我創建了所有3個操作的唯一方法,問題是它不插入刪除或更新:sqlcommand執行查詢不更新刪除並插入
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();
}
爲什麼使用BeginExecuteNonQuery? – emirc 2012-07-05 12:40:32
wow .. catch(SqlException性別)和MessageBox.Show(sex.Message ...似乎intersting .. :) – 2012-07-05 12:45:39
@Oldemiro:它是否拋出任何異常或它不會進入開關的情況下,因爲您正在開關的情況下使用字符串。所以請確認..? – 2012-07-05 12:48:38