0

I have the following database command:

CREATE DATABASE [test1] ON 
    (FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\test1.mdf'), 
    (FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\test1_log.ldf') 
FOR ATTACH

When I do it via C# it returns me more than one value. Here is the C# code:

public void RunQuery(string query, bool longRunning = false)
{
    using (var cmd = new SqlCommand(query, Connection))
    {
        cmd.CommandTimeout = longRunning ? _longRunningCommandTimeout : _commandTimeout;
        cmd.ExecuteNonQuery();
    }
}

This results in the following error from SQL Server:

Error: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated

And here is the stack trace from the exception:

at System.Data.SqlClient.SqlConnection.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.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()

The database attach succeeds, even though an error is raised.

When I execute the same query (under the same user) via Management Studio it gets me just the following:

Commands completed successfully.

Is it possible that the CREATE DATABASE statement returns more than one result?

Aaron Bertrand
  • 181,950
  • 28
  • 405
  • 624
isxaker
  • 87
  • 11

0 Answers0