2012-07-05 78 views
0

你好,我總是使用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(); 
    } 
+0

爲什麼使用BeginExecuteNonQuery? – emirc 2012-07-05 12:40:32

+2

wow .. catch(SqlException性別)和MessageBox.Show(sex.Message ...似乎intersting .. :) – 2012-07-05 12:45:39

+0

@Oldemiro:它是否拋出任何異常或它不會進入開關的情況下,因爲您正在開關的情況下使用字符串。所以請確認..? – 2012-07-05 12:48:38

回答

4

您應該使用參數化查詢。總是.....

這對於插入操作。

comando = "Insert Into Estudante (Codigo,Nome,Apelido) values(@id, @nome, @apelido"); 
SqlCommand command = new SqlCommand(comando, con);      
command.Parameters.AddWithValue("@id", txtID.Text); 
command.Parameters.AddWithValue("@nome", txtNome.Text); 
command.Parameters.AddWithValue("@apelido", txtapelido.Text); 
command.CommandType = CommandType.Text;      
command.ExecuteNonQuery(); 

這裏不需要使用數據集或數據適配器。只是ExecuteNonQuery

這是刪除操作。

comando = "delete from Estudante where [email protected]"; 
SqlCommand command = new SqlCommand(comando, con); 
command.Parameters.AddWithValue("@id", txtID.Text); 
command.CommandType = CommandType.Text; 
command.ExecuteNonQuery();      

請注意,您應該通過Text屬性,而不是整個文本框

本作的更新運算

comando = "update table Estudante set [email protected], [email protected] where [email protected]"; 
SqlCommand command = new SqlCommand(comando, con); 
command.Parameters.AddWithValue("@id", txtID.Text); 
command.Parameters.AddWithValue("@nome", txtNome.Text); 
command.Parameters.AddWithValue("@apelido", txtapelido.Text); 
command.CommandType = CommandType.Text; 
command.ExecuteNonQuery();      

同樣,此處使用Text屬性不是文本框對象

通過這種方式,您不必擔心字符串參數中的引號,並且您關閉了
的大門

+0

這是正確的,但我不認爲這就解決了這個問題:) – Tomtom 2012-07-05 12:51:41

+0

不幸的是,不過謝謝你的一切 – 2012-07-05 18:44:14

+0

任何解決方案??? – 2012-07-16 18:39:55

1

要執行insert/delete/update聲明,您只需創建SqlCommandSqlConnection對象。 DataSetDataAdapter沒用。

插入一行:

string [email protected]"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Works.mdf;Integrated Security=True;User Instance=True;Asynchronous Processing=true"; 

using(SqlConnection con = new SqlConnection(cnstr)) 
{ 
    string sql = "Insert Into Estudante (Codigo,Nome,Apelido) values(@Codigo,@Nome,@Apelido)"; 
    using(SqlCommand command= new SqlCommand(sql,con)) 
    { 
    command.Parameters.Add("@Codigo",SqlDbType.Int).Value=txtID.Text; 
    command.Parameters.Add("@Nome",SqlDbType.VarChar,30).Value=txtNome.Text; 
    command.Parameters.Add("@Apelido",SqlDbType.VarChar,30).Value=txtapelido.Text; 
    con.Open(); 
    cmd.ExecuteNonQuery(); 
    con.Close(); 
    } 
} 
+0

當我嘗試重複一條記錄時它沒有發生它會生成sqlException,但是當我用Explorer訪問Table時沒有任何修改; – 2012-07-05 18:12:39

+0

從解決方案資源管理器中選擇.mdf文件+打開屬性窗口設置'複製到輸出目錄=複製如果newer' – adatapost 2012-07-06 01:55:42

+0

任何更改到項目 – 2012-07-06 15:26:13

0

您呼叫的的sqlDataAtapter的填充方法來填充這是不必要的數據庫。刪除該語句並查看。這應該工作。

+0

nop without too – 2012-07-06 17:52:44