如何处理此错误,关键字“表格”附近的语法不正确。”

时间:2019-06-29 20:12:04

标签: c#

我正在为我的兄弟创建一个可以帮助他工作的应用程序,我创建了一个C#表单,该表单应该将数据传递到sql表中,但是我收到错误消息。

不知道该怎么做。

我使用的代码来自视频https://www.youtube.com/watch?v=TIAOr2S6-SY&feature=youtu.be

我尝试了其他无法帮助我理解的方法,但无济于事。我正在尝试进行编码,希望能提供帮助。

“ 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.IO;
    using System.Data.SqlClient;

    namespace WindowsFormsApp2
    {
        public partial class Form1 : Form
        {
            SqlConnection con=new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\lerro\OneDrive\Documents\DatabaseAmal.mdf;Integrated Security=True;Connect Timeout=30");

            public SqlConnection Con { get => con; set => con = value; }

            public Form1()
            {
                InitializeComponent();
            }

            private void label1_Click(object sender, EventArgs e)
            {

            }

            private void label2_Click(object sender, EventArgs e)
            {

            }

            private void label5_Click(object sender, EventArgs e)
            {

            }

            private void label9_Click(object sender, EventArgs e)
            {

            }

            private void button4_Click(object sender, EventArgs e)
            {
            con.Open();
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "insert into Table values ('" +textBox1.Text+ "','" +textBox5.Text+ "','" + textBox2.Text + "','" +textBox3.Text + "','" +textBox4.Text + "','" +textBox6.Text + "','" +textBox4.Text + "','" +textBox7.Text + "','" +comboBox1.Text + "','" +textBox8.Text + "','" +textBox9.Text + "','" +dateTimePicker2.Text + "','" +textBox10.Text + "','" +comboBox2.Text + "','" +comboBox3.Text + "','" +comboBox4.Text + "','" +textBox14.Text + "','" +textBox11.Text + "','" +textBox12.Text + "','" +textBox13.Text + "','" + BrowseTextbox.Text + "','" + pictureBox1.Text + "','" + textBox15.Text + "','" +dateTimePicker1.Text + "')";
            cmd.ExecuteNonQuery();
            con.Close();

                MessageBox.Show("record inserted successfully");

            }

         private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Image Files(*jpg; *.jpeg; *.gif; *.bmp;)|*jpg; *.jpeg; *.gif; *.bmp;";
            if (open.ShowDialog() == DialogResult.OK)
            {
                BrowseTextbox.Text = open.FileName;
                pictureBox1.Image = new Bitmap(open.FileName);

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = String.Empty;
            textBox2.Text = String.Empty;
            textBox3.Text = String.Empty;
            textBox4.Text = String.Empty;
            textBox5.Text = String.Empty;
            textBox6.Text = String.Empty;
            textBox7.Text = String.Empty;
            textBox8.Text = String.Empty;
            textBox9.Text = String.Empty;
            textBox10.Text = String.Empty;
            textBox11.Text = String.Empty;
            textBox12.Text = String.Empty;
            textBox13.Text = String.Empty;
            textBox14.Text = String.Empty;
            textBox15.Text = String.Empty;
            pictureBox1.Image = null;
            comboBox1.Text = String.Empty;
            comboBox2.Text = String.Empty;
            comboBox3.Text = String.Empty;
            comboBox4.Text = String.Empty;
            BrowseTextbox.Text = String.Empty;






        }

            private void comboBox2_SelectedIndexChanged(object sender, 
    EventArgs e)
            {

            }
        }
    }

    

System.Data.dll中发生了类型为'System.Data.SqlClient.SqlException'的未处理异常 关键字“表格”附近的语法不正确。”

1 个答案:

答案 0 :(得分:0)

您可以在视频中看到他在测试数据库中调用了表格

Table1

但是在您的插入语句中,您写的表是

table

如果您观看了视频,必须留下来

 cmd.CommandText = "insert into Table1 values

或其他您选择的名称代替表1