Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET多次异步执行SQL Server存储过程_C#_Asp.net_Sql Server_Stored Procedures - Fatal编程技术网

C# ASP.NET多次异步执行SQL Server存储过程

C# ASP.NET多次异步执行SQL Server存储过程,c#,asp.net,sql-server,stored-procedures,C#,Asp.net,Sql Server,Stored Procedures,我正在通过ASP.NET调用一个存储过程,现在我正在尝试将其异步调用200次,我正在尝试通过添加一个事务来实现这一点,但是它不起作用,下面是我的代码: try { using (connection = new SqlConnection(connectionString)) { connection.Open(); transaction = connection.BeginTransaction(); for (int i =

我正在通过ASP.NET调用一个存储过程,现在我正在尝试将其异步调用200次,我正在尝试通过添加一个事务来实现这一点,但是它不起作用,下面是我的代码:

try
{
    using (connection = new SqlConnection(connectionString))
    {
        connection.Open();
        transaction = connection.BeginTransaction();

        for (int i = 0; i < 200; i++)
        {
            using (SqlCommand command = new SqlCommand("TimeSlotAppointments", connection))
            {
                command.Transaction = transaction;

                command.CommandType = CommandType.StoredProcedure;

                SqlParameter parameter1 = command.Parameters.Add("@StartTime", SqlDbType.DateTime);
                parameter1.Direction = ParameterDirection.Input;
                parameter1.Value = DateTime.Now;

                command.ExecuteNonQuery();
            }
        }

        transaction.Commit();
    }
}
catch (SqlException e)
{
    Console.Write(e);
    transaction.Rollback();
}
finally
{
    connection.Close();
    connection.Dispose();
}
试试看
{
使用(connection=newsqlconnection(connectionString))
{
connection.Open();
事务=连接。BeginTransaction();
对于(int i=0;i<200;i++)
{
使用(SqlCommand=newsqlcommand(“timeslotappoinces”,connection))
{
command.Transaction=事务;
command.CommandType=CommandType.storedProcess;
SqlParameter parameter1=command.Parameters.Add(“@StartTime”,SqlDbType.DateTime);
parameter1.Direction=ParameterDirection.Input;
parameter1.Value=DateTime.Now;
command.ExecuteNonQuery();
}
}
Commit();
}
}
捕获(SQLE异常)
{
控制台。写入(e);
transaction.Rollback();
}
最后
{
connection.Close();
connection.Dispose();
}

我将当前日期和时间作为参数传递,当我在SQL Server中签出结果时,我希望
@StartTime
是相同的,但它们不是很接近,而是每个记录的毫秒数都在增加,我这样做是错误的吗?我试图完成的是同时执行存储过程200次

开始时间值是不同的,因为您在循环内部分配值,并且在每次迭代中,时间都发生了变化(如您所提到的几毫秒)。如果希望对所有调用使用相同的值,则需要将时间戳存储在变量中的循环外部,并在循环中使用该值。 您的代码应该是这样的:

try
{
    using (connection = new SqlConnection(connectionString))
    {
        connection.Open();
        transaction = connection.BeginTransaction();
        var startTime = DateTime.Now; // I added this line 

        for (int i = 0; i < 200; i++)
        {
            using (SqlCommand command = new SqlCommand("TimeSlotAppointments", connection))
            {
                command.Transaction = transaction;

                command.CommandType = CommandType.StoredProcedure;

                SqlParameter parameter1 = command.Parameters.Add("@StartTime", SqlDbType.DateTime);
                parameter1.Direction = ParameterDirection.Input;
                parameter1.Value = startTime;  // I changed this line

                command.ExecuteNonQuery();
            }
        }

        transaction.Commit();
    }
}
catch (SqlException e)
{
    Console.Write(e);
    transaction.Rollback();
}
finally
{
    connection.Close();
    connection.Dispose();
}
试试看
{
使用(connection=newsqlconnection(connectionString))
{
connection.Open();
事务=连接。BeginTransaction();
var startTime=DateTime.Now;//我添加了这一行
对于(int i=0;i<200;i++)
{
使用(SqlCommand=newsqlcommand(“timeslotappoinces”,connection))
{
command.Transaction=事务;
command.CommandType=CommandType.storedProcess;
SqlParameter parameter1=command.Parameters.Add(“@StartTime”,SqlDbType.DateTime);
parameter1.Direction=ParameterDirection.Input;
parameter1.Value=startTime;//我更改了此行
command.ExecuteNonQuery();
}
}
Commit();
}
}
捕获(SQLE异常)
{
控制台。写入(e);
transaction.Rollback();
}
最后
{
connection.Close();
connection.Dispose();
}

如果希望所有记录的日期时间都相同,则将
日期时间.Now
存储在循环外的变量中,并将其用于
参数1.Value
。当调用
日期时间.Now
200次时,为什么希望
@StartTime
相同?时光流逝。。。然而:这段代码实际上想做什么?似乎不太可能希望使用相同的参数运行完全相同的SQL—200次。另外,您实际上并没有将其称为async(“现在我尝试将其称为200次async”),我感觉这是一个XY问题。事务与异步有什么关系?事务用于执行相反的操作。强制按顺序运行多个并发命令如果要执行相同的存储过程N次,请创建存储过程一次,然后更改参数值N次:
for(i=0;i