为什么我的代码每次运行都没有工作并按提交,Cmd.Exeutenonquery中有一条消息说Sqlexception未处理请帮助 [英] Why Is My Code Not Working Everytime I Run This And Press Submit,There's A Message In Cmd.Exeutenonquery Saying Sqlexception Was Unhandled Please Help

查看:104
本文介绍了为什么我的代码每次运行都没有工作并按提交,Cmd.Exeutenonquery中有一条消息说Sqlexception未处理请帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 Attendance
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void SignButton_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=(localdb)\\Projects;Initial Catalog=MyDatabase;Integrated Security=True;");
            con.Open();
            SqlCommand cmd = new SqlCommand("INSERT INTO User(Username,Password,Firstname,MiddleInitial,Lastname,Age,Phone,Address,City,State,Email) VALUES(@user,@pass,@first,@middle,@last,@age,@phone,@add,@city,@state,@email)",con);

            cmd.Parameters.AddWithValue("@user",UsernameTB.Text);
            cmd.Parameters.AddWithValue("@pass",PasswordTB.Text);
            cmd.Parameters.AddWithValue("@first",FirstnameTB.Text);
            cmd.Parameters.AddWithValue("@middle",MiddleTB.Text);
            cmd.Parameters.AddWithValue("@last",LastnameTB.Text);
            cmd.Parameters.AddWithValue("@age",AgeTB.Text);            
            cmd.Parameters.AddWithValue("@phone",PhoneTB.Text);
            cmd.Parameters.AddWithValue("@add",AddressTB.Text);
            cmd.Parameters.AddWithValue("@city",CityTB.Text);
            cmd.Parameters.AddWithValue("@state",StateTB.Text);
            cmd.Parameters.AddWithValue("@email",EmailTB.Text);

            cmd.ExecuteNonQuery();
            MessageBox.Show("Record has been inserted");
            con.Close();
        }
    }
}

推荐答案

尝试在SQL Server Management Studio中运行INSERT命令您尝试插入的所有值,并查看是否抛出任何错误。您收到的异常很可能是从SQL Server抛出的
Try running your INSERT command in SQL Server Management Studio with all the values you are trying to insert and see if it is throwing any error. The Exception you are receiving is most probably being thrown from SQL Server


这篇关于为什么我的代码每次运行都没有工作并按提交,Cmd.Exeutenonquery中有一条消息说Sqlexception未处理请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆