我有一个最多由 10 个客户端调用的 Web 服务。该网络服务由 7 个不同的 asmx 页面组成,每个页面大约有 100-200 个功能。
所有这些功能都适用于 MSSQL2005 或 MS SQL2000 数据库。一天中的某些时段,来自客户端的流量很大,似乎我的 sql 服务器上的连接耗尽,导致所有客户端停止。
在每个函数中,我打开一个连接,执行一些操作,然后关闭连接,有时有事务,有时没有。
在服务器上,我看到它创建了很多连接,我不知道为什么它们没有消失,但即使在功能完成后仍然保留在那里。所以我可以看到我的 10 个客户端不时创建超过 80 个连接。有时它们中的一些会消失,有时它们在使用数小时后仍然存在。那里正在进行某种汇集吗?
问题 1:是否有另一种方式来处理我应该使用的连接,例如每个 Web 服务全局一个连接或任何其他方式?
问题 2:如果可以处理每个函数的连接,那么为什么它不关闭服务器上的连接,使打开的连接列表一直变得越来越大,直到我摆脱连接错误?
这个问题与我的另一个问题相关但不相同:Strange SQL2005 Problem。“SqlConnection不支持并行事务”
我现在已将其范围缩小到“连接中断”错误。
我有以下代码
try
{
using (var connection = new SqlConnection(Utils.ConnectionString))
{
connection.Open();
using (var cmd = new SqlCommand("StoredProcedure", connection))
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
var sqlParam = new SqlParameter("id_document", idDocument);
cmd.Parameters.Add(sqlParam);
int result = cmd.ExecuteNonQuery();
if (result != -1)
return "something";
//do something here
return "something else";
}
}
//do something
}
catch (SqlException ex)
{
return "something AKA didn't work";
}
Run Code Online (Sandbox Code Playgroud)
问题是:var connection如果using括号 ( { })之间发生意外错误,是否仍然关闭?
问题是我对存储过程的大部分调用都是这样进行的,最近我收到了这个错误:
System.InvalidOperationException:超时已过期。在从池中获取连接之前超时时间已过。这可能是因为所有池连接都在使用中并且达到了最大池大小。
我访问数据库的另一种方式是通过 nHibernate。
我可以使用打开的连接从存储过程调用 dll 吗?
我有一个从 SQL Server 获取数据的 dll,当我从存储过程调用它时,我不想打开一个新连接。
谢谢
这是一个例子
public class Class1
{
public static SqlString GetName(SqlString str)
{
SqlCommand cmd = new SqlCommand(str.ToString());
cmd.CommandType = System.Data.CommandType.Text;
string name = cmd.ExecuteScalar().ToString();
return name;
}
}
Run Code Online (Sandbox Code Playgroud)
这是 SQL 代码
CREATE FUNCTION fn_TestConnection
(
@str nvarchar(255)
)
RETURNS nvarchar(max)
AS EXTERNAL NAME TestConnection.[TestConnection.Class1].GetName
GO
SELECT dbo.fn_TestConnection('SELECT FName FROM Clients WHERE Id = 1' )
Run Code Online (Sandbox Code Playgroud) 我有下面的代码,我已经阅读了Moq 和 SqlConnection? 以及如何存根 IDBconnection,但我仍然不知道如何模拟以下 sqlconnection。
public class SqlBulkWriter : ISqlBulkWriter
{
private readonly string _dbConnectionString;;
public SqlBulkWriter(string dbConnectionString)
{
this._dbConnectionString = dbConnectionString;
}
public void EmptyTable(string schema, string tableName)
{
using (var connection = new SqlConnection(this._dbConnectionString))
{
try
{
connection.Open();
using (var truncate = new SqlCommand($"TRUNCATE TABLE [{schema}].[{tableName}] ", connection))
{
truncate.ExecuteNonQuery();
}
}
catch (Exception ex)
{
throw new Exception(ex);
}
finally
{
connection.Close();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我要为EmptyTable做单元测试,我想我应该先模拟sqlconnection?或者如何对 EmptyTempTable 进行单元测试?
谢谢!许多欣赏!
C#SqlConnection支持多少个并发语句?
假设我正在处理运行 10 个线程的 Windows 服务。所有线程使用相同的SqlConnection对象但不同的SqlCommand对象,并在不同的表或相同的表但不同的数据上执行选择、插入、更新和删除等操作。它会起作用吗?单个SqlConnection对象能否同时处理 10 条语句?
我最近发布了(并且当我确定问题与实际问题无关时立即删除)一个关于SqlConnection在"ChangeDatabase"范围结束时丢失其数据库信息的问题.例:
//Other code...
dbConn = new SqlConnection(dbConnBuilder.ConnectionString);
dbConn.Open();
dbConn.ChangeDatabase(currentDatabase);
dbConn.Close();
}
Run Code Online (Sandbox Code Playgroud)
我的问题:
SqlConnection当你只需要一个给定类型的一个连接时,保持一个对象并在需要时打开和关闭它被认为是不好的做法吗?dbConn.Database 不记得'超出范围'?(哎呀,我不知道可以了解范围的方法).currentDatabaseChangeDatabase我的连接字符串是:
Data Source=server.name.com;Persist Security Info=True;User ID=username;Password=password
Run Code Online (Sandbox Code Playgroud)
谢谢大家,让我知道,如果我能给你更多的信息,仍然学习使用SO
我只想就使用SqlConnection对象的正确用法或正确设计发表意见.以下2中哪一个是最佳用途:
一个数据提供程序类,其方法(每个方法)包含SqlConnection对象(并在完成时处理).喜欢:
IList<Employee> GetAllEmployees()
{
using (SqlConnection connection = new SqlConnection(this.connectionString)) {
// Code goes here...
}
}
Employee GetEmployee(int id)
{
using (SqlConnection connection = new SqlConnection(this.connectionString)) {
// Code goes here...
}
}
Run Code Online (Sandbox Code Playgroud)
要么
SqlConnection connection; // initialized in constructor
IList<Employee> GetAllEmployees()
{
this.TryOpenConnection(); // tries to open member SqlConnection instance
// Code goes here...
this.CloseConnection();
// return
}
Employee GetEmployee(int id)
{
this.TryOpenConnection(); // tries to open member SqlConnection instance
// Code goes here...
this.CloseConnection();
// return …Run Code Online (Sandbox Code Playgroud) 我正在开发一个应用程序,它打开一个数据库连接并从中检索数据以显示给用户.在我能看到的代码中,一切看起来都很好,但是有些东西是错误的,因为在下面的方法中会抛出异常.我在"conn.Open();"处设置了一个断点.它就在那里抛出异常.
我不知道如何找出实际错误是什么以及如何解决它,并希望在这里得到一些帮助.如果有帮助,可以在此处找到异常的堆栈跟踪.
例外是:System.ApplicationException:应用程序中的错误
提前致谢!
抛出异常的方法:
public List<Movie> GetMovies() {
var movieTitles = new List<Movie>(100);
using (var conn = CreateConnection()) {
try {
var cmd = new SqlCommand("dbo.usp_GetMovies", conn);
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
using (var reader = cmd.ExecuteReader()) {
var movieIDIndex = reader.GetOrdinal("MovieID");
var nameIndex = reader.GetOrdinal("Name");
var yearIndex = reader.GetOrdinal("Year");
var lengthIndex = reader.GetOrdinal("Length");
var summaryIndex = reader.GetOrdinal("Summary");
while (reader.Read()) {
movieTitles.Add(new Movie {
MovieID = reader.GetInt32(movieIDIndex),
Name = reader.GetString(nameIndex),
Year = reader.GetInt32(yearIndex),
Length = reader.GetInt32(lengthIndex),
Summary = …Run Code Online (Sandbox Code Playgroud) 我是新手C#,目前正在自学.在我的项目中,我想要做的是从a中获取10个数据table并在循环中显示.例如,如果它有10个数据,那么所有数据都将显示在default.aspx页面中.目前我的代码只能显示第一行并循环10次.以下是我的样本.
namespace CRM_Attachment
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Sample"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
SqlCommand com;
con.Open();
string str = "SELECT TOP 10 FILE_NAME FROM FILE";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
//if (reader.HasRows)
//{
while (reader.Read())
{
labelname1.Text = reader["FILE_NAME"].ToString();
}
//}
reader.Close();
con.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
}
下面是我的default.aspx页面..
<body>
<form id="form1" runat="server">
<% for (int i=0;i<10;i++) {%>
<div> …Run Code Online (Sandbox Code Playgroud) 我正在尝试查询SQL Server数据库并以JSON格式返回响应.我正在尝试下面的代码
using (SqlConnection connection = new SqlConnection(connStr))
{
SqlCommand command = new SqlCommand(commandText, connection);
command.Parameters.Add("@ROOM_Data", SqlDbType.VarChar);
command.Parameters["@ROOM_Data"].Value = ROOM;
connection.Open();
List<DatabaseResult> records = new List<DatabaseResult>();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var row = new DatabaseResult
{
request_id = (int)reader["request_id"],
room = (string)reader["room"],
jrs_no = (int)reader["jrs_no"],
submit_date = (DateTime)reader["submit_date"],
sample_no = (int)reader["sample_no"],
animal_id = (string)reader["animal_id"],
pen_id = (string)reader["pen_id"],
ped_no = (string)reader["ped_no"],
gender = (string)reader["gender"],
dob = (DateTime)reader["dob"],
parent_mating = (string)reader["parent_mating"],
generation = (string)reader["generation"],
allele = (string)reader["allele"], …Run Code Online (Sandbox Code Playgroud) sqlconnection ×10
c# ×7
sql-server ×5
asp.net ×3
.net ×2
sql ×2
ado.net ×1
clr ×1
dll ×1
exception ×1
loops ×1
moq ×1
mysql ×1
sqlcommand ×1
t-sql ×1
unit-testing ×1
xunit ×1