Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6967463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:19:30+00:00 2026-05-27T16:19:30+00:00

Introduction I’m writing a web application (C#/ASP.NET MVC 3, .NET Framework 4, MS SQL

  • 0

Introduction

I’m writing a web application (C#/ASP.NET MVC 3, .NET Framework 4, MS SQL Server 2008, System.Data.ODBC for database connections) and I’m having quite some issues regarding database creation/deletion.

I have a requirement that application should be able to create and delete databases.

Problem

Application fails stress testing for that function. More specifically, if client starts to quickly create, delete, create again a database with the same name then eventually (~on 5th request) server code throws ODBCException ‘Connection has been disabled.’. This behavior is observed on all machines that test has been performed on – the exact failing request may be not 5th but somewhere around that value.

Research

Googling on exception gave very low output – the exception seems very generic one and no analogue issues found. One of suggestions I’ve found was that my development Windows 7 might not be able to handle numerous simultaneous connections as it’s not Server OS. I’ve tried installing our app on Windows 2008 Server – almost no change in behavior, just a bit more requests processed before exception occurs.

Code and additional comments on implementation

Databases are created using stored procedure like this:

CREATE PROCEDURE [dbo].[sp_DBCreate]
...     
    @databasename nvarchar(124)     -- 124 is max length of database file names
AS
    DECLARE @sql nvarchar(150);
BEGIN
...
    -- Create a new database
    SET @sql = N'CREATE DATABASE ' + quotename(@databasename, '[');
    EXEC(@sql);

    IF @@ERROR <> 0
        RETURN -2;
...    
    RETURN 0;
END

Databases are deleted using the following SP:

CREATE PROCEDURE [dbo].[sp_DomainDelete]
...
    @databasename nvarchar(124)     -- 124 is max length of database file names
AS
    DECLARE @sql nvarchar(200);
BEGIN
...
    -- check if database exists
    IF EXISTS(SELECT * FROM [sys].[databases] WHERE [name] = @databasename)
    BEGIN
        -- drop all active connections
        SET @sql = N'ALTER DATABASE' + quotename(@databasename, '[') + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE';
        EXEC(@sql);
            -- Delete database
        SET @sql = N'DROP DATABASE ' + quotename(@databasename, '[');
        EXEC(@sql);

        IF @@ERROR <> 0
            RETURN -1;  --error deleting database
    END
    --ELSE database does not exist. consider it deleted.

    RETURN 0; 
END

In both SPs I’ve skipped less relevant parts like sanity checks.

I’m not using any ORMs, all SPs are called from code by using OdbcCommand instances. New OdbcConnection is created for each function call.

I sincerely hope someone might give me clue to the problem.

UPD: The exactly same problem occurs if we just rapidly create a bunch of databases. Thanks to everyone for suggestions on database delete code, but I’d prefer to have a solution or at least a hint for more general problem – the one which occurs even without deleting DBs at all.

UPD2: The following code is used for SP calls:

public static int ExecuteNonQuery(string sql, params object[] parameters)
{
    try
    {
        var command = new OdbcCommand();
        Prepare(command, new OdbcConnection( GetConnectionString() /*irrelevant*/), null, CommandType.Text, sql,
          parameters == null ?
          new List<OdbcParameter>().ToArray() :
          parameters.Select(p => p is OdbcParameter ? (OdbcParameter)p : new OdbcParameter(string.Empty, p)).ToArray());

        return command.ExecuteNonQuery();
    }
    catch (OdbcException ex)
    {
        // Logging here
        throw;
    }
}

public static void Prepare(
    OdbcCommand command, 
    OdbcConnection connection, 
    OdbcTransaction transaction, 
    CommandType commandType, 
    string commandText, 
    params OdbcParameter[] commandParameters)
{
    if (connection.State != ConnectionState.Open)
    {
        connection.Open();
    }
    command.Connection = connection;
    command.CommandText = commandText;
    if (transaction != null)
    {
        command.Transaction = transaction;
    }
    command.CommandType = commandType;
    if (commandParameters != null)
    {
        command.Parameters.AddRange(
            commandParameters.Select(p => p.Value==null && 
                p.Direction == ParameterDirection.Input ?
                    new OdbcParameter(p.ParameterName, DBNull.Value) : p).ToArray());
    }
}

Sample connection string:

Driver={SQL Server}; Server=LOCALHOST;Uid=sa;Pwd=<password here>;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:19:31+00:00Added an answer on May 27, 2026 at 4:19 pm

    Okay. There may be issues of scope for OdbcConnection but also you don’t appear to be closing connections after you’ve finished with them. This may mean that you’re reliant on the pool manager to close off unused connections and return them to the pool as they timeout. The using block will automatically close and dispose of the connection when finished, allowing it to be returned to the connection pool.

    Try this code:

    public static int ExecuteNonQuery(string sql, params object[] parameters)
    {
        int result = 0;
            try
            {
                var command = new OdbcCommand();
                using (OdbcConnection connection = new OdbcConnection(GetConnectionString() /*irrelevant*/))
                {
                   connection.Open();
                   Prepare(command, connection, null, CommandType.Text, sql,
                           parameters == null ?
                                               new List<OdbcParameter>().ToArray() :
                                               parameters.Select(p => p is OdbcParameter ? (OdbcParameter)p : new OdbcParameter(string.Empty, p)).ToArray());
    
                   result = command.ExecuteNonQuery();
                }
    
            }
            catch (OdbcException ex)
            {
                // Logging here
                throw;
            }
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Long introduction: Normally all data necessary for my web application are stored in session
First a little introduction. We have an SQL Server Express 2008 database, which schema
I watched a little introduction into ASP.NET Dynamic Data, and I noticed this option
Introduction I am programming a semantic web application in haskell. With hsparql http://hackage.haskell.org/package/hsparql I
Has the introduction of the .net framework made raw programming in COM and DCOM
Small introduction: I've tried to develop my project with sql database, entity framework, linq.
Introduction I'm using ASP.Net MVC3. My Controllers talk to a service layer, and the
Introduction In my current organisation, we have many desktop and web applications all feeding
Introduction After watching this video from LIDNUG, about .NET code protection http://secureteam.net/lidnug_recording/Untitled.swf (especially from
Introduction: I have an AdvancedDataGrid displaying hierarchical data illustrated by the image below: The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.