c#:运行代码时出错

时间:2017-07-25 12:09:22

标签: c# sql-server

在成功编译后运行代码时,出现错误

  

关键字“结束”附近的语法不正确。

虽然代码格式正确但有错误。 有人,请帮我纠正这个问题。我的代码如下;

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;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

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

,End,Pick,,Width,

答案 1 :(得分:2)

您的代码中存在多个问题:

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

    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;
    }