繁体   English   中英

使用C#中的SQL连接将数据添加到表

[英]Add data to table with SQL connection in C#

我在员工需要更新的数据库中有一个CRM表。 我已经使用Windows窗体并使用了SQL连接来链接它们,但我不断收到错误消息,提示键“ In”附近的sytanx不正确,我也不十分清楚为什么。

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 WindowsFormsApplication1
{
    public partial class Form1 : MetroFramework.Forms.MetroForm
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'traficoDataSet.Inteserado_En' table. You can move, or remove it, as needed.
            this.inteserado_EnTableAdapter.Fill(this.traficoDataSet.Inteserado_En);
            // TODO: This line of code loads data into the 'traficoDataSet.Contacto' table. You can move, or remove it, as needed.
            this.contactoTableAdapter.Fill(this.traficoDataSet.Contacto);
            // TODO: This line of code loads data into the 'traficoDataSet.Tipo' table. You can move, or remove it, as needed.
            this.tipoTableAdapter.Fill(this.traficoDataSet.Tipo);
            // TODO: This line of code loads data into the 'agora_UsuariosDataSet.Usuarios' table. You can move, or remove it, as needed.
                this.usuariosTableAdapter.Fill(this.agora_UsuariosDataSet.Usuarios);
            // TODO: This line of code loads data into the 'traficoDataSet.Como' table. You can move, or remove it, as needed.
            this.comoTableAdapter.Fill(this.traficoDataSet.Como);
            // TODO: This line of code loads data into the 'traficoDataSet.Trafico' table. You can move, or remove it, as needed.
            this.traficoTableAdapter.Fill(this.traficoDataSet.Trafico);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new     SqlConnection(global::WindowsFormsApplication1.Properties.Settings.Default.TraficoConnectionString);
            try
            {

                string sql = "INSERT INTO Trafico ('Nombre', 'Apedillo', 'Correo', 'Teléfono', 'Como', 'Comercial', 'Tipo', 'Contacto','Inteserado En') Values(" + nombreTextBox.Text + "," + apedilloTextBox.Text + "," + correoTextBox.Text + "," + teléfonoTextBox.Text + "," + comoComboBox.Text + "," + comercialComboBox.Text + "," + tipoComboBox.Text + "," + contactoComboBox.Text + ","+inteserado_EnComboBox.Text+"";
                SqlCommand exeSql = new SqlCommand(sql, cn);
                cn.Open();
                exeSql.ExecuteNonQuery();
                this.traficoTableAdapter.Fill(this.traficoDataSet.Trafico);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}

尝试将字符串sql更改为:

string sql = "INSERT INTO Trafico (Nombre, Apedillo, Correo, Teléfono, Como, Comercial, Tipo, Contacto,Inteserado En) Values('" + nombreTextBox.Text + "','" + apedilloTextBox.Text + "','" + correoTextBox.Text + "','" + teléfonoTextBox.Text + "','" + comoComboBox.Text + "','" + comercialComboBox.Text + "','" + tipoComboBox.Text + "','" + contactoComboBox.Text + "','" + inteserado_EnComboBox.Text + "')";

您忘记了放在右括号,也忘记在列值上加上“'”字符。 您还应该验证所有列都是字符串。 如果有一些数字,则可以在值之间省略“”字符。

我认为问题是您需要将值用单引号引起来,像这样

string sql = "INSERT INTO Trafico ('Nombre', 'Apedillo', 'Correo', 'Teléfono', 'Como', 'Comercial', 'Tipo', 'Contacto','Inteserado En') Values('" + nombreTextBox.Text + "','" + apedilloTextBox.Text + "','" + correoTextBox.Text + "','" + teléfonoTextBox.Text + "','" + comoComboBox.Text + "','" + comercialComboBox.Text + "''," + tipoComboBox.Text + "','" + contactoComboBox.Text + "','" + inteserado_EnComboBox.Text +"')";

像上面提到的那样,使用参数来避免sql注入。 当添加多行时,这也使修改查询变得更加容易。 您可以使用相同的查询字符串,而只需交换值。 您可以像这样使用参数

string sql = "INSERT INTO Trafico ('Nombre', 'Apedillo', 'Correo', 'Teléfono', 'Como', 'Comercial', 'Tipo', 'Contacto','Inteserado En') Values(@Nobre, @Apedillo, @ Correo, @Telephono, @Como, @Comercial, @Tipo, @Contacto, @Inteserado_En)";


exeSql.Parameters.Add("@Nobre", SqlDataType.Varchar).Value = nombreTextBox.Text;
exeSql.Parameters.Add("@Apedillo", SqlDataType.Varchar).Value = apedilloTextBox.Text
//and so on

您不再需要在变量周围添加单引号,因为您在参数中定义了数据类型。 sqlcommand会知道将其视为字符串,因为它是通过这种方式定义的。 我还注意到列名称“ Inteserado En”中的空格。 确保正确。 通常,您不应该为列名添加空格。 但这是可以完成的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM