C#将数据插入数据库窗口表单app

时间:2016-07-01 12:37:59

标签: c#

我需要构建一个人们可以预订的应用程序,但在此之前他们需要填写一些信息。我在尝试保存数据时收到此错误代码:System.Data.dll中出现未处理的“System.Data.SqlClient.SqlException”类型异常

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BonTemps
{
    public partial class Home : Form
    {
        public Home()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var Form1 = new Form1();
            Form1.Show();
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void Home_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'bonTempsDBDataSet.Tafel' table. You can move, or remove it, as needed.
            this.tafelTableAdapter.Fill(this.bonTempsDBDataSet.Tafel);

        }

        private void btnOpslaan_Click(object sender, EventArgs e)
        {
            SqlConnection sc = new SqlConnection();
            SqlCommand com = new SqlCommand();
            sc.ConnectionString = ("Data Source=ACER;Initial Catalog=BonTempsDB;Integrated Security=True");
            sc.Open();

            com.Connection = sc;
            com.CommandText = (@"INSERT INTO Klant (Naam, Adres, Woonplaats, Telefoonnummer, EmailAdres), VALUES ('" + txtNaam.Text + "','" + txtAdres.Text + "','" + txtWoon.Text + "','" + txtTel.Text + "','" + txtMail.Text + "'");
            com.ExecuteNonQuery();

            sc.Close();

        }

    }
}

4 个答案:

答案 0 :(得分:1)

删除以前的逗号。

如果这还不够,您可以从Command Text调试和复制生成的字符串,并尝试直接在SQL Server Mangement Studio中运行它或类似的

答案 1 :(得分:1)

印刷错误在单词VALUES之前删除COMMA。

答案 2 :(得分:0)

您必须将一个打开的SqlConnection传递给您的SqlCommand才能使其正常工作:

com.Connection = sc;

另外,请考虑使用named parameters将数据传递给您的查询,以使您的查询更加防错:

SqlConnection sc = new SqlConnection();
SqlCommand com = new SqlCommand();
sc.ConnectionString = ("Data Source=ACER;Initial Catalog=BonTempsDB;Integrated Security=True");
sc.Open();

com.Connection = sc;
com.CommandText = @"INSERT INTO Klant (Naam, Adres, Woonplaats, Telefoonnummer, EmailAdres) VALUES (@naam, @adres, @woon, @tel, @mail)";
com.Parameters.AddWithValue("@naam", txtNaam.Text);
com.Parameters.AddWithValue("@adres", txtAdres.Text);
com.Parameters.AddWithValue("@woon", txtWoon.Text);
com.Parameters.AddWithValue("@tel", txtTel.Text);
com.Parameters.AddWithValue("@mail", txtMail.Text);

com.ExecuteNonQuery();
sc.Close();

答案 3 :(得分:0)

using (var sc = new SqlConnection("Data Source=ACER;Initial Catalog=BonTempsDB;Integrated Security=True"))
{
    using (var com = new SqlCommand("sql cmd text", sc))
    {
        try
        {
            sc.Open();
            com.ExecuteNonQuery();
        }
        catch
        {

        }
    }
}