2016-05-24 11 views
0

いくつかのデータを編集したいフォームがあるので、私のUpdate SQLステートメントが正しいかどうかを知りたいと思います。しかし、何らかの理由で、フォームは更新を保存せず、dbで何も起こりません。正しい更新ステートメントc#

これは私のコードビハインドである:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data.SqlClient; 
using System.Data; 

public partial class edit : System.Web.UI.Page 
{ 
    SqlConnection con = new SqlConnection("Data Source=CASSIA-PC\\SQLEXPRESS;Initial Catalog=clientes;Integrated Security=True"); 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     string v = Request.QueryString["id"]; 
     SqlCommand cmd = new SqlCommand("SELECT idCliente, nmCliente, fantasia, cpf, cep, logradouro, numero, complemento, bairro, cidade, estado, telefone, celular, insEstadual, insMunicipal, email, homePage, tbClientes.tpCliente, tbTipoClientes.idTipoCliente, tbTipoClientes.nmTipoCliente FROM tbClientes INNER JOIN tbTipoClientes ON tbClientes.tpCliente = tbTipoClientes.idTipoCliente WHERE idCliente = '" + v + "'", con); 
     try 
     { 
      con.Open(); 
      using (var reader = cmd.ExecuteReader()) 
      { 
       while (reader.Read()) { 
        txtId.Text = reader["idCliente"].ToString(); 
        txtNome.Text = reader["nmCliente"].ToString(); 
        txtFantasia.Text = reader["fantasia"].ToString(); 
        txtCPF.Text = reader["cpf"].ToString(); 
        txtCEP.Text = reader["cep"].ToString(); 
        txtLogradouro.Text = reader["logradouro"].ToString(); 
        txtNumero.Text = reader["numero"].ToString(); 
        txtComplemento.Text = reader["complemento"].ToString(); 
        txtBairro.Text = reader["bairro"].ToString(); 
        txtCidade.Text = reader["cidade"].ToString(); 
        txtEstado.Text = reader["estado"].ToString(); 
        txtTelefone.Text = reader["telefone"].ToString(); 
        txtCelular.Text = reader["celular"].ToString(); 
        txtInscEstadual.Text = reader["insEstadual"].ToString(); 
        txtInscMunicipal.Text = reader["insMunicipal"].ToString(); 
        txtEmail.Text = reader["email"].ToString(); 
        txtSite.Text = reader["homePage"].ToString(); 
       } 
      } 

      cmd.ExecuteNonQuery(); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 
     finally 
     { 
      con.Close(); 
     } 

    } 

    protected void btnEditar_Click(object sender, EventArgs e) 
    { 
     string v = Request.QueryString["id"]; 
     con.Open(); 
     SqlCommand cmd = new SqlCommand("UPDATE tbClientes SET nmCliente = '"+txtNome.Text+"', fantasia = '"+txtFantasia.Text+"', cpf = '"+txtCPF.Text+"', cep = '"+txtCEP.Text+"', logradouro = '"+txtLogradouro.Text+"', numero = '"+txtNumero.Text+"', complemento = '"+txtComplemento.Text+"', bairro = '"+txtBairro.Text+"', cidade = '"+txtCidade.Text+"', estado = '"+txtEstado.Text+"', telefone = '"+txtTelefone.Text+"', celular = '"+txtCelular.Text+ "', insEstadual = '"+txtInscEstadual.Text+"', insMunicipal = '"+txtInscMunicipal.Text+"', email = '"+txtEmail.Text+"', homePage = '"+txtSite.Text+"' WHERE idCliente = '" + v + "'", con); 
     try 
     { 
      cmd.ExecuteNonQuery(); 
     } 
     catch(Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 
     finally 
     { 
      con.Close(); 
     } 
    } 
} 
+1

実行する前に、作成するSQL文字列を検査できるようにブレークポイントを設定してみてください。構文をチェックし、手動で実行してみてください。また、あなたのコードにはかなり深刻なセキュリティホールが存在するため、 "SQLインジェクション"を探す必要があります。 – Blorgbeard

+0

本当に、それはnullです。しかし、Page_Loadでパラメータが受信されました。 –

+0

cmd.ExecuteNonQuery();にブレークポイントを設定し、cm.CommandTextの値をチェックして、サーバーに送信している実際のSQLを確認します。コマンドのパラメータ化を行った場合は、質問を編集することをお勧めします。そこで、あなたがどのように行ったのか、どこで助けてくれるのかがわかります。コード全体を見ると、私たちはあなたを助けます。 –

答えて

3

私はあなたの問題があるかなり確信している:

WHERE idCliente = '" + v + "'" 

クライアントIDは、最も可能性の高いあなたがしたい、データベース内の数値フィールドであるため、そのように扱う:

WHERE idCliente = " + v 

Blorgbeardはあなたがにパラメータ化コマンドを使用する必要が言及したよう。これにより、アポストロフィなどを含むテキストボックスなどの問題も解決され、UPDATEも失敗する可能性があります。

+0

Jeremy Thompson、私は今、私のコード挿入コマンドでParameterisedコマンドを使用しています。私は "excluido"と呼ばれる私のdbの列を持っていて、これを "N"に設定したい。このように:cmd.Parameters.Add( "@ excluido"、SqlDbType.NVarChar); cmd.Parameters ["@ excluido"]。値= "N"; –

+0

AddWithValueを使用すると、より簡単です。 'cmd.Parameters.AddWithValue(" @ excluido "、" N ");'あなたのコメントに疑問がないかどうかわかりません。 –

+0

乾杯、ちょうどあなたが 'AddWithValue'でこの問題にぶつからないようにしてください:http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/ –

0

私はJeremyに同意しました。パラメータ化されたクエリに変更するか、ラベルを付けてクエリを設定し、クエリをコピーしてSQL Serverで直接テストするとよいでしょう。

string query = "Update..." 

SQL Serverでクエリテキストをコピーして直接テストします。

関連する問題