简体   繁体   English

C#将数据插入数据库Windows窗体应用程序

[英]C# insert data into database windows form app

I need to build a application where people can make a reservation but before doing that they need to fill in some information. 我需要构建一个人们可以预订的应用程序,但在此之前,他们需要填写一些信息。 I get this error code at the moment when i try to save the data: An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll 当我尝试保存数据时,我得到此错误代码:System.Data.dll中发生类型'System.Data.SqlClient.SqlException'的未处理异常

This is my code: 这是我的代码:

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();

        }

    }
}

Remove the comma Before VALUES. 在VALUES之前删除逗号。

If that is not enough, you can debug and copy the generated string from Command Text and try running it directly in SQL Server Mangement Studio or similar 如果这还不够,您可以调试并复制“命令文本”中生成的字符串,然后尝试直接在SQL Server Mangement Studio或类似版本中运行它

出现印刷错误,请删除“值”一词前的“逗号”。

You have to pass an open SqlConnection to your SqlCommand to make it work: 您必须将一个开放的SqlConnection传递给SqlCommand才能使其工作:

com.Connection = sc;

Also, consider using named parameters to pass data to your query to make your query more error-proof: 另外,请考虑使用命名参数将数据传递给查询,以使查询更具防错能力:

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();
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
        {

        }
    }
}

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

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