C# ASP.NET 注册页将不再更新数据库
本文关键字:不再 更新 数据库 ASP NET 注册 | 更新日期: 2023-09-27 18:33:05
>我的注册页面(VS 2015 上的 C# ASP.NET(更新了我的 SQL Server 数据库中的一些条目,然后我更改了数据库,更新了代码(更新web.config
字符串等(,现在它只显示此错误:
错误:System.Data.SqlClient.SqlException (0x80131904(:字符串或二进制数据将被截断。该语句已终止。at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction( at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose( at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean&dataReady( at System.Data.SqlClient.SqlCommand.FinishExecuteReader (SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString( at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest( at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior, Boolean returnStream, String 方法, TaskCompletionSource1 completion, Int32 timeout, Task& task, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource
1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite( at System.Data.SqlClient.SqlCommand.ExecuteNonQuery(( at Registration.Button1_Click(Object sender, EventArgs e( in c:''Users''718358''Documents''Visual Studio 2015''WebSites''Registration2''Registration.aspx.cs:line 61 ClientConnectionId:d0e00199-8bf8-435b-a2ea-03d6f5c8dc40 错误号:8152,状态:13,类:16 注册
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class Registration : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None;
if (IsPostBack)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationDatabaseConnectionString"].ConnectionString);
conn.Open();
string checkuser = "SELECT count(*) FROM Customers WHERE CustUserName='" + txtCustUserName.Text + "'";
SqlCommand com = new SqlCommand(checkuser, conn);
int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
if (temp == 1)
{
Response.Write("User already exists");
}
conn.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationDatabaseConnectionString"].ConnectionString);
conn.Open();
string insertQuery = "INSERT into Customers (CustUserName, CustPassword, CustFirstName, CustLastName, CustAddress, CustCity, CustProv, CustPostal, CustCountry, CustHomePhone, CustBusPhone, CustEmail) values (@custUsername ,@custPassword ,@custFirstName ,@custLastName ,@custAddress ,@custCity ,@custProv ,@custPostal, @custCountry ,@custHomePhone ,@custBusPhone ,@custEmail)";
SqlCommand com = new SqlCommand(insertQuery, conn);
com.Parameters.AddWithValue("@custUsername", txtCustUserName.Text);
com.Parameters.AddWithValue("@custPassword", txtCustPassword.Text);
com.Parameters.AddWithValue("@custFirstName", txtCustFirstName.Text);
com.Parameters.AddWithValue("@custLastName", txtCustLastName.Text);
com.Parameters.AddWithValue("@custAddress", txtCustAddress.Text);
com.Parameters.AddWithValue("@custCity", txtCustCity.Text);
com.Parameters.AddWithValue("@custProv", txtCustProv.Text);
com.Parameters.AddWithValue("@custPostal", txtCustPostal.Text);
com.Parameters.AddWithValue("@custCountry", txtCustCountry.Text);
com.Parameters.AddWithValue("@custHomePhone", txtCustHomePhone.Text);
com.Parameters.AddWithValue("@custBusPhone", txtCustBusPhone.Text);
com.Parameters.AddWithValue("@custEmail", txtCustEmail.Text);
com.ExecuteNonQuery();
Response.Redirect("Manager.aspx");
Response.Write("Registration is successful" );
conn.Close();
}
catch(Exception ex)
{
Response.Write("Error:"+ex.ToString());
}
}
}
谢谢。
根据错误消息,一个或多个字段获取的值比相应列长。 您应该仔细检查列的长度。 如果不知道更多详细信息,就不可能说出更改数据库如何或为什么导致这种情况。
将以下代码替换为您现有的代码:-- (
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationDatabaseConnectionString"].ConnectionString);
conn.Open();
string insertQuery = "INSERT into Customers (CustUserName, CustPassword, CustFirstName, CustLastName, CustAddress, CustCity, CustProv, CustPostal, CustCountry, CustHomePhone, CustBusPhone, CustEmail) values (@custUsername ,@custPassword ,@custFirstName ,@custLastName ,@custAddress ,@custCity ,@custProv ,@custPostal, @custCountry ,@custHomePhone ,@custBusPhone ,@custEmail)";
SqlCommand com = new SqlCommand(insertQuery, conn);
com.Parameters.Add("@custUsername", txtCustUserName.Text);
com.Parameters.Add("@custPassword", txtCustPassword.Text);
com.Parameters.Add("@custFirstName", txtCustFirstName.Text);
com.Parameters.Add("@custLastName", txtCustLastName.Text);
com.Parameters.Add("@custAddress", txtCustAddress.Text);
com.Parameters.Add("@custCity", txtCustCity.Text);
com.Parameters.Add("@custProv", txtCustProv.Text);
com.Parameters.Add("@custPostal", txtCustPostal.Text);
com.Parameters.Add("@custCountry", txtCustCountry.Text);
com.Parameters.Add("@custHomePhone", txtCustHomePhone.Text);
com.Parameters.Add("@custBusPhone", txtCustBusPhone.Text);
com.Parameters.Add("@custEmail", txtCustEmail.Text);
com.CommandType = CommandType.Text;
com.ExecuteNonQuery();
Response.Redirect("Manager.aspx");
Response.Write("Registration is successful" );
conn.Close();
}
catch(Exception ex)
{
Response.Write("Error:"+ex.ToString());
}
}
}
您正在创建一个小缺陷,只是将AddWithValue替换为仅添加并添加以下行"com。CommandType = CommandType.Text;">
谢谢大家。 @JohnEphraimTugado回答了这个问题。发生此错误的原因是字段收到的输入大于字段接受的限制。
好吧,错误本身就说明了问题,数据库中有一个字段正在获取比其限制更多的数据来容纳(用蹩脚的语言解释(。请检查该字段,确保执行以下两项之一:
- 将
- 发送到数据库的数据字符串修剪为特定长度,低于数据库中的字符限制。
- 更改数据库字段的限制,以便它可以容纳更多长度。
如果您仍然收到错误,则可能是以下原因:
错误 : (字符串或二进制数据将被截断(
原因 :
1( 您在数据库字段中输入的数据属于不同类型。
2(您在数据库字段中输入的数据比声明的数据大
。3(您没有将传入的数据表单转换为适当的字段类型。
我的猜测:(您直接从字段中输入数据(
补救措施 :
1(将字符串数据转换为字符串(在最后收到的输入中连接.toString(((+根据其他数据类型以及int/float/etc转换数据
2( 增加数据库中的字段输入大小(例如:城市(25(到城市(MAX((