none
ProbleM CREATE DB WITH SQLPARAMETER (in sql server) RRS feed

  • Question

  • Hi

    i'm trying to create a DB from a DataAccessLayer to  class DBArchivioOperations using a parameter 

    ' this is in the DataAccessLayer
    
    Dim DB As New DBArchivioOperations
    Dim Ndb as string = "MyDB"
    dim Query = $" CREATE DATABASE @DbName"
    Dim Param = New SqlClient.SqlParameter("@DbName", Ndb)
    		
    Dim FlagDBCreato = DB.CreaNuovoDB(Query, Param)
    
    

    and this parte of  the code in DBArchivioOperations class

    Public Function CreaNuovoDB(ByVal query As String, ByVal param As SqlParameter) As Boolean
    
    Using CONN As New SqlConnection With {
    	.ConnectionString = EmptyConnectionString}
    	Using CMD As New SqlCommand With {.Connection = CONN}
    		CMD.CommandType = CommandType.Text
    		CMD.CommandText = query
    		CMD.Parameters.Add(param)
    	Try
    		CONN.Open()
    		CMD.ExecuteNonQuery()
    					
    		Return True
    	Catch ex As Exception
    	    MessageBox.Show("errore creazione archivio")
    					
    	    Return False
    	End Try
    
    	End Using
    End Using
    End Function

    The connectingstring is ok ( has no catalog name) and connection is well Opened but the

    CMD.ExecuteNonQuery() does not work.

    It seems that the parameter is not added to command

    I checked the 'query' received and the 'param' received by the DBArchivoOperations and they are right.

    If in the DBArchivoOperation i replace the query with 

    " CREATE DATABASE MyDB"

    in the 

    CMD.CommandText = Query

    and eliminate the param everything works

    So where is  mistake

    Wednesday, December 27, 2017 3:18 PM

Answers

All replies