使用Windows窗体添加到数据库

时间:2016-06-22 16:37:41

标签: c# database winforms visual-studio

我正在为学院创建数据库和报告系统。有很多相关信息,我们必须记录客户流量。我已经设计了一个表单,因此它向数据库添加了条目,但是当我尝试添加数据并且我不明白为什么时,我一直收到错误。

 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;
using System.Configuration;
using DgvFilterPopup;


namespace DRAFT
{
    public partial class Form1 : MetroFramework.Forms.MetroForm
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void traficoBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.traficoBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.traficoDataSet);

        }

        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);
            DgvFilterManager filterManager = new  DgvFilterManager(traficoDataGridView);

        }
        private void button1_Click(object sender, EventArgs e)
        {
        }

        private void inteserado_EnLabel_Click(object sender, EventArgs e)
        {

        }

        private void fechaLabel_Click(object sender, EventArgs e)
        {

        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
        }


        private void fillByToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.traficoTableAdapter.FillBy(this.traficoDataSet.Trafico);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
         }

         private void button1_Click_1(object sender, EventArgs e)
        {
            traficoBindingSource.Filter = "Comercial = '"+ComFilComboBox.Text+"'OR Tipo ='"+TipoFilComboBox.Text+"'OR Como ='"+ComoFilComboBox.Text+"'OR Contacto ='"+ConFilComboBox.Text+"'OR 'Inteserado EN' ='"+IntFilComboBox.Text+"'";
        }

        private void traficoDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void traficoDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void metroButton1_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection("Data Source=SRVN16\sqlexpress;Initial Catalog=Trafico;Persist Security Info=True;User ID=agora;Password=********;Pooling=False");
            try
            {
                string sql = "INSERT INTO Trafico (Nombre, Apedillo, Correo, Teléfono, Como, Comercial, Tipo, Contacto, Inteserado En, Fecha) Values(" + nombreTextBox.Text + "," + apedilloTextBox.Text + "," + correoTextBox.Text + "," + teléfonoTextBox.Text + "," + comoMetroComboBox.SelectedValue + "," + comercialMetroComboBox.SelectedValue + "," + tipoMetroComboBox.SelectedValue + "," + contactoMetroComboBox.SelectedValue + "," + inteserado_EnMetroComboBox.SelectedValue + "," + fechaMetroDateTime.Text + "";
                SqlCommand exeSql = new SqlCommand(sql, cn);
                cn.Open();
                exeSql.ExecuteNonQuery();
                MessageBox.Show("Add New Record Done!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.traficoTableAdapter.Fill(this.traficoDataSet.Trafico);
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                cn.Close();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你一直在犯的错误是什么?对于初学者,您在insert语句中列出的值需要用引号括起来;

  

实施例。 ......值(“用户名”,“姓氏”,“109苏格兰”,......)