字符串或二进制数据将被截断。该声明已被终止。如何处理此错误? [英] String or binary data would be truncated. The statement has been terminated.How to handle this error?

查看:125
本文介绍了字符串或二进制数据将被截断。该声明已被终止。如何处理此错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么我得到这个例外。表的属性如下:



FirstName nchar(100)

LastName nchar(100)

电子邮件nchar(100)

密码nchar(100)

地址nchar(100)



及以下是代码:



I have no idea why i am getting this exception.The properties of table are below:

FirstName nchar(100)
LastName nchar(100)
Email nchar(100)
Password nchar(100)
Address nchar(100)

and below is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;
using System.Collections.Specialized;
using System.Data;
using System.Text.RegularExpressions;

namespace Sup
{
    /// <summary>
    /// Interaction logic for Signup.xaml
    /// </summary>
    public partial class Signup : Window
    {
        public Signup()
        {
            InitializeComponent();
        }
        private void Reset()
        {
            textBoxAddress.Text = "";
            textBoxEmail.Text = "";
            textBoxFirstName.Text = "";
            textBoxLastName.Text = "";
            passwordBox1.Password = "";
            passwordBoxConfirm.Password = "";
        }
        private void registerButton_Click(object sender, RoutedEventArgs e)
        {
            if (textBoxEmail.Text.Length == 0)
            {

                MessageBox.Show("Enter an email.");

                textBoxEmail.Focus();

            }

            else if (!Regex.IsMatch(textBoxEmail.Text, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"))
            {

                MessageBox.Show("Enter a valid email.");

                textBoxEmail.Select(0, textBoxEmail.Text.Length);

                textBoxEmail.Focus();

            }

            else
            {

                string firstname = textBoxFirstName.Text;

                string lastname = textBoxLastName.Text;

                string email = textBoxEmail.Text;

                string password = passwordBox1.Password;

                if (passwordBox1.Password.Length == 0)
                {

                    MessageBox.Show("Enter password.");

                    passwordBox1.Focus();

                }

                else if (passwordBoxConfirm.Password.Length == 0)
                {

                    MessageBox.Show("Enter Confirm password.");

                    passwordBoxConfirm.Focus();

                }

                else if (passwordBox1.Password != passwordBoxConfirm.Password)
                {

                    MessageBox.Show("Confirm password must be same as password.");

                    passwordBoxConfirm.Focus();

                }

                else
                {

                    string address = textBoxAddress.Text;

                    SqlConnection con = new SqlConnection(Properties.Settings.Default.usersConnectionString);
                    
                        con.Open();

                        SqlCommand cmd = new SqlCommand("Insert into Registration (FirstName,LastName,Email,Password,Address) values('" + firstname + "','" + lastname + "','" + email + "','" + password + "','" + address + "')", con);

                        cmd.CommandType = CommandType.Text;

                        cmd.ExecuteNonQuery();

                        con.Close();

                        MessageBox.Show("You have Registered successfully.");

                        Reset();
                    
                }

            }
        }
    }
}

推荐答案

))
{

MessageBox.Show( 输入有效的电子邮件。);

textBoxEmail.Select( 0 ,textBoxEmail.Text。长度);

textBoxEmail.Focus();

}

其他
{

string firstname = textBoxFirstName.Text;

string lastname = textBoxLastName.Text;

string email = textBoxEmail.Text;

< span class =code-keyword> string password = passwordBox1.Password;

if (passwordBox1.Password.Length == 0
{

MessageBox.Show( 输入密码。);

passwordBox1.Focus();

}

else if (passwordBoxConfirm。 Password.Length == 0
{

MessageBox.Show( 输入确认密码。);

passwordBoxConfirm.Focus();

}

else if (passwordBox1。密码!= passwordBoxConfirm.Password)
{

MessageBox.Show( 确认密码必须与密码相同。);

passwordBoxConfirm.Focus();

}

else
{

string address = textBoxAddress.Text;

SqlConnection con = new SqlConnection(Properties.Settings.Default.usersConnectionString);

con.Open();

SqlCommand cmd = new SqlCommand( 插入注册(FirstName,LastName,电子邮件,密码,地址)值(' + firstname + ',' + lastname + ',' + email + ',' +密码+ ',' +地址+ ') ,con);

cmd.CommandType = CommandType.Text;

cmd.ExecuteNonQuery();

con.Close();

MessageBox.Show( 您已成功注册。);

重置();

}

}
}
}
}
")) { MessageBox.Show("Enter a valid email."); textBoxEmail.Select(0, textBoxEmail.Text.Length); textBoxEmail.Focus(); } else { string firstname = textBoxFirstName.Text; string lastname = textBoxLastName.Text; string email = textBoxEmail.Text; string password = passwordBox1.Password; if (passwordBox1.Password.Length == 0) { MessageBox.Show("Enter password."); passwordBox1.Focus(); } else if (passwordBoxConfirm.Password.Length == 0) { MessageBox.Show("Enter Confirm password."); passwordBoxConfirm.Focus(); } else if (passwordBox1.Password != passwordBoxConfirm.Password) { MessageBox.Show("Confirm password must be same as password."); passwordBoxConfirm.Focus(); } else { string address = textBoxAddress.Text; SqlConnection con = new SqlConnection(Properties.Settings.Default.usersConnectionString); con.Open(); SqlCommand cmd = new SqlCommand("Insert into Registration (FirstName,LastName,Email,Password,Address) values('" + firstname + "','" + lastname + "','" + email + "','" + password + "','" + address + "')", con); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("You have Registered successfully."); Reset(); } } } } }


我会说你向列中传递了太多数据。您的列设置为100个字符,如果您将更多列传递给其中一个数据将被截断的列。
I'd say you're passing too much data to the columns. Your columns are set for 100 characters, if you pass more than that to one of the columns the data would become truncated.


该字段不足以保存数据。



FirstName nvarchar(max)

LastName nvarchar(max)

电子邮件nv​​archar(最大)

密码nvarchar(最大)

地址nvarchar(最大)



例如:firstname nchar(5)



如果你试图插入'aaaaaaa'
the field is not enough to hold the data.

FirstName nvarchar(max)
LastName nvarchar(max)
Email nvarchar(max)
Password nvarchar(max)
Address nvarchar(max)

For example: firstname nchar(5)

if you try to insert 'aaaaaaaa'


这篇关于字符串或二进制数据将被截断。该声明已被终止。如何处理此错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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