2017-11-02 4 views
4

C#で会計アプリケーション用のInsert文を発行しようとしていますが、88行目でエラーが発生していますここDesc SQLの近くに構文エラーがあります。cmd.ExecuteNonQuery()にエラーが表示されます

private void button2_Click(object sender, EventArgs e) 
{ 
    using (SqlConnection cn = new SqlConnection(constring)) 
    { 
     cn.Open(); 
     if (choice.Text == "DEPOSIT") 
     { 
      double newAccBal = Convert.ToDouble(opening_amount.Text) + Convert.ToDouble(amount.Text); 
      string newBal = newAccBal.ToString(); 
      string sql = "insert into credit (fullname,accountNo,opening_amount,amount,desc,newBal) values (@fullname,@accountNo,@opening_amount,@amount,@desc,@newBal)"; 
      using (SqlCommand cmd = new SqlCommand(sql, cn)) 
      { 
       cmd.Parameters.AddWithValue("@fullname", fullname.Text); 
       cmd.Parameters.AddWithValue("@accountNo", textBox3.Text); 
       cmd.Parameters.AddWithValue("@opening_amount", opening_amount.Text); 
       cmd.Parameters.AddWithValue("@amount", amount.Text); 
       cmd.Parameters.AddWithValue("@desc", desc.Text); 
       cmd.Parameters.AddWithValue("@newBal", newBal); 

       try 
       { 
        var msg = MessageBox.Show("Information to be Sent for Deposit" + Environment.NewLine + "Please Confirm to Continue?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question); 
        if (msg == DialogResult.Yes) 
        { 
         cmd.ExecuteNonQuery(); <------------------------------ This Area 
         string confirmation = "Full Name : '"+fullname.Text+"' "+Environment.NewLine+" Depositing Amount : '"+amount.Text+"' "+Environment.NewLine+" Description : '"+desc.Text+"' "+Environment.NewLine+" New Balance : '"+newBal+"'"; 
         MessageBox.Show("Deposit Successful" + Environment.NewLine + "Information has been Saved for Records" + Environment.NewLine + "Confirmation is as follows" + Environment.NewLine + confirmation ,"Information", MessageBoxButtons.OK, MessageBoxIcon.Information); 
        } 

        string sql2 = "update account_info set opening_amount = '"+newBal+"' where id='"+id.Text+"'"; 
        using (SqlCommand cmd2 = new SqlCommand(sql2, cn)) 
        { 
         cmd2.ExecuteNonQuery(); 
        } 
       } 
       catch(Exception ex) 
       { 
        MessageBox.Show(ex.ToString()); 
       } 
      } 
     } 
    } 
} 

は私が何をしないのです、その行cmd.ExecuteNonQuery()でdescとポイントの近くに不適切な構文を言いますか?

+3

'desc'は代わりに' [desc] 'を使うか、 –

+0

@ un-luckyの別の名前を選んでください。ありがとう、そして神様があなたを祝福してくれるでしょう。 Works 100% – Paul

+0

さて、あなたは、このコメントの精巧なバージョンのいずれかを受け入れられた回答としてマークすることができます。 –

答えて

2

DESCは、SQLの予約語で、降順の略であり、ORDER BY句で使用されます。 SQLステートメントの大括弧で囲みます。

+0

コメント以上 –

0

あなたのカラムの1つにdescという名前を付けました。ただし、それはreserved wordです。別の列名(賢明な選択肢かもしれません)を選択するか、DBMSが予約語ではなく列を意味することを確認する方法を使用する必要があります。 []を使用すると、名前に注釈を付けることができます。[desc]が動作するはずです。

関連する問題