gpt4 book ai didi

c# - 为什么我在 ASP.NET 中得到 System.Data.SqlClient.SqlInternalConnection.OnError...?

转载 作者:行者123 更新时间:2023-11-30 21:50:27 25 4
gpt4 key购买 nike

我是 ASP.NET 的新手,我正在尝试将 Web 表单的数据保存到数据库中。我已经测试了数据库表与 Aspx 文件的连接,它在浏览器中正确地将表的数据(手动输入的数据)加载到表中。

但是,当我填写表格并按“保存”时,出现此错误:Screenshot of error message

这是我的aspx.cs代码。

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.Configuration;

public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
{

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
conn.Open();


Response.Write("Database connection successful");


conn.Close();
}

}



protected void Button2_Click(object sender, EventArgs e)
{
Response.Write("Database connection successful");

}

protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
conn.Open();

string insertQuery = "insert into Table (Name, Address, Phone, Email, Source, Message) values (@name, @address, @phone, @email, @source, @message");
SqlCommand com = new SqlCommand(insertQuery, conn);
com.Parameters.AddWithValue("@name", Name.Text);
com.Parameters.AddWithValue("@address", Address.Text);
com.Parameters.AddWithValue("@phone", Phone.Text);
com.Parameters.AddWithValue("@email", Email.Text);
com.Parameters.AddWithValue("@source", Source.SelectedItem.ToString());
com.Parameters.AddWithValue("@message", "text");

com.ExecuteNonQuery();

Response.Write("Data is saved");

conn.Close();


}

catch (Exception ex)
{

Response.Write("Error:" + ex.ToString());

}


}
}

我的错误是这样说的:

System.Data.SqlClient.SqlInternalConnection.OnError(SqlException 异常, bool breakConnection,操作 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 method, TaskCompletionSource 1 完成,Int32 超时,任务和任务, bool 异步写入)在 System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 完成, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at _Default.Button1_Click(Object sender, EventArgs e) 在 D:\documents\visual studio 2015\WebSites\Enquiry Tracking Sys\Default.aspx.cs:line 45 ClientConnectionId:6c142f6c-40d7-4977-b75d-8fb0d774b0c6 错误号:156,State:1,Class:15

最佳答案

传输层错误通常与 sql server 的连接被破坏有关……通常是网络。

Timeout Expired 通常在 sql 查询运行时间过长时抛出。

所以我会排查您的 Sql Server 链接的问题,然后监控哪些查询超时。

听起来像一个 SQL 作业正在运行,备份?这可能是锁定表或重新启动服务。

Connection problems with SQL Server in ASP.NET applications using out-of-process session state

关于c# - 为什么我在 ASP.NET 中得到 System.Data.SqlClient.SqlInternalConnection.OnError...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36321971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com