2017-07-25 83 views
0

虽然成功运行编译后的代码错误,我有错误的关键字“终结”近C#上运行的代码

不正确的语法。

尽管代码格式良好但出现错误。 有人请帮我解决问题。我的代码在下面;

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Data.SqlClient; 

namespace Order 
{ 
    public partial class Form2 : Form 
    { 
     public Form2() 
     { 
      InitializeComponent(); 
     } 
     SqlConnection ABC = new SqlConnection(@"Data Source=iPC\SQLEXPRESS;Initial Catalog=NML;Integrated Security=True"); 

     SqlCommand command = new SqlCommand(); 
     //SqlDataReader dataRead = new SqlDataReader(); 
     private void button1_Click(object sender, EventArgs e) 
     { 
      ABC.Open(); 
      command.CommandText = "INSERT INTO tbl_REC(Qcode,Warp,Ply,Blend,TEnds,Warp1,Weft,End,Pick,,Width,Weave1,Weave2,TL) Values('" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "','" + textBox11.Text + "','" + textBox12.Text + "','" + textBox13.Text + "','" + textBox14.Text + "')"; 
      command.Connection = ABC; 
      command.ExecuteNonQuery(); 
      ABC.Close(); 
      MessageBox.Show("DATA SAVED SUCCESSFULLY"); 
      textBox2.Clear(); 
      textBox3.Clear(); 
      textBox4.Clear(); 
      textBox5.Clear(); 
      textBox6.Clear(); 
      textBox7.Clear(); 
      textBox8.Clear(); 
      textBox9.Clear(); 
      textBox10.Clear(); 
      textBox11.Clear(); 
      textBox12.Clear(); 
      textBox13.Clear(); 
      textBox14.Clear(); 
      dateTimePicker1.Value = DateTime.Now; 
     } 
     private void Form2_Load(object sender, EventArgs e) 
     { 
      command.Connection = ABC; 
     } 
    } 
} 
+6

了解传递值到查询时使用的参数。篡改查询字符串可能会导致无法解释的语法错误 - 并使代码容易受到SQL注入攻击。 –

回答

2

你的问题是在插入查询。你正在使用两次昏迷,它应该只有一次。

,End,Pick,,Width, 
+2

也'结束'是一个保留字,因此需要逃脱 – Rahul

2

你的代码有它的许多问题:

  1. 您连接具有用户输入字符串创建SQL语句。这是一个安全隐患,因为它是SQL Injection攻击的开门。你应该总是使用参数。
  2. 对于实现IDisposable接口的类,您正在使用表单级变量。虽然有时候这是无法避免的,但这不是其中之一。
  3. 对至少一个列名称使用SQL保留字(end)。应该避免这种情况,但如果要更改列名称太晚,则应将其包装在方括号中([end])。
  4. 你有几个逗号(正如Dalvinder Singh的回答中指出的那样)应该只有一个逗号。

我建议以下选择:

var success = false; 
var connectionString = @"Data Source=iPC\SQLEXPRESS;Initial Catalog=NML;Integrated Security=True"; 
var sql = "INSERT INTO tbl_REC(Qcode,Warp,Ply,Blend,TEnds,Warp1,Weft,[End],Pick,Width,Weave1,Weave2,TL) Values(@Qcode, @Warp, @Ply, @Blend, @TEnds, @Warp1, @Weft, @End, @Pick, @Width, @Weave1, @Weave2, @TL)"; 
using(var ABC = new SqlConnection(connectionString)) 
{ 
    using (var command = new SqlCommand(sql, ABC)) 
    { 
     command.Parameters.Add("@Qcode", SqlDbType.NVarChar).Value = textBox2.Text; 
     command.Parameters.Add("@Warp", SqlDbType.NVarChar).Value = textBox3.Text; 
     command.Parameters.Add("@Ply", SqlDbType.NVarChar).Value = textBox4.Text; 
     // .... 
     // I'll let you fill in the rest of the parameters... 
     // .... 

     try 
     { 
      ABC.Open(); 
      command.ExecuteNonQuery(); 
      success = true; 
     } 
     catch(Exception e) 
     { 
      // Do something with this exception! write to log, for instance. 

     } 
    } 
} 
if (success) 
{ 
    MessageBox.Show("DATA SAVED SUCCESSFULLY"); 
    textBox2.Clear(); 
    textBox3.Clear(); 
    textBox4.Clear(); 
    textBox5.Clear(); 
    textBox6.Clear(); 
    textBox7.Clear(); 
    textBox8.Clear(); 
    textBox9.Clear(); 
    textBox10.Clear(); 
    textBox11.Clear(); 
    textBox12.Clear(); 
    textBox13.Clear(); 
    textBox14.Clear(); 
    dateTimePicker1.Value = DateTime.Now; 
}