【问题标题】:How to click on a label in a listBox and have text show up in multiple textBoxs如何单击列表框中的标签并在多个文本框中显示文本
【发布时间】:2018-09-25 22:29:56
【问题描述】:

我正在设计一个计划器,但当我点击我的添加任务按钮时遇到了问题。单击它后,我的程序崩溃并显示此错误:

"System.Data.SqlClient.SqlException: '关键字'table'附近的语法不正确。'"

我在哪里可以找到不正确的语法?

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 InfoHub
{
    public partial class Planner : Form
    {
        SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\epoch\source\repos\InfoHub\InfoHub\planner.mdf;Integrated Security=True;Connect Timeout=30");
        public Planner()
        {
            InitializeComponent();
        }

        private void Planner_Load(object sender, EventArgs e)
        {
            this.TopMost = true;
        }

        private void addTask_Click(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "insert into table values('" + textBox3.Text+"','"+textBox4.Text+"','"+textBox5.Text+"','"+textBox6.Text+"','"+textBox7.Text+"')";
            cmd.ExecuteNonQuery();
            con.Close();
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            textBox7.Text = "";
        }

【问题讨论】:

  • 这个问题与标签、规划器或列表框无关,而与每次都使用 SQL 参数有关。永远。
  • 我对你所说的这个可能的意思感到困惑,我应该从哪里开始使用这些参数?

标签: c# listbox listboxitem


【解决方案1】:

您错误地使用了 sql insert 命令:

cmd.CommandText = "insert into table values('" + textBox3.Text+"','"+textBox4.Text+"','"+textBox5.Text+"','"+textBox6.Text+"','"+textBox7.Text+"')";

table 是 sql 关键字,它不被视为表名。尝试将其添加为 'table' 以通知 sql 'table' 是特定表的名称而不是关键字:

  cmd.CommandText = "insert into 'table' values('" + textBox3.Text+"','"+textBox4.Text+"','"+textBox5.Text+"','"+textBox6.Text+"','"+textBox7.Text+"')";

一般来说,名字总是写在''中是个好习惯。

【讨论】:

  • 添加这些调整现在表明“表”附近存在语法错误。我认为我的表设置错误或未正确链接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-13
  • 1970-01-01
  • 1970-01-01
  • 2015-10-04
  • 1970-01-01
  • 2014-06-24
  • 1970-01-01
相关资源
最近更新 更多