0

I need to run the below script from my vb.net app & would like to know how would I make the database name (mynewdatabase_db) a parameter?

Basically I need to run this script on certain databases so I need the database name to be dynamic.

    Dim MySqlQuery2 As String = "USE mynewdatabase_db " & _
        "TRUNCATE TABLE MyTable " & _
        " " & _
        "INSERT INTO MyTable ([MyNumber], [HTTPAlias]) VALUES (0, @MyDomainAlias1) " & _
        "INSERT INTO MyTable ([MyNumber], [HTTPAlias]) VALUES (0, @MyDomainAlias2) " & _
        "INSERT INTO MyTable ([MyNumber], [HTTPAlias]) VALUES (0, @MyDomainAlias3) "

    Using cmd2 = New SqlCommand(MySqlQuery2.ToString, myConnection)
        cmd2.Parameters.AddWithValue("@MyDomainAlias1", MyDomainName)
        cmd2.Parameters.AddWithValue("@MyDomainAlias2", "www." & MyDomainName)
        cmd2.Parameters.AddWithValue("@MyDomainAlias3", MyDomainName & "." & MyTempDomainName)
        cmd2.ExecuteNonQuery()

    End Using

Thanks in advance ;)

2 Answers 2

1

I would make it part of your connection string(s). That way it wouldn't be hard coded into your application.

Sign up to request clarification or add additional context in comments.

1 Comment

That did the trick - thanks. I did my first sql command, closed the database connection then opened up a new database connection & did my second sql command.. Thanks for your help :)
1

There is a very usefull class named SqlConnectionStringBuilder

Supposing you have a initial connection string stored in your app.config or web.config.
Then you could write a method like this, which load your stored connection string and
change the Initial Catalog to a new one passed as parameter

Public Function GetNewConnectionString(newDataName as String) as String
    Dim cnnString as String = ConfigurationManager.ConnectionStrings["YourConnName"].ConnectionString
    Dim builder As SqlConnectionStringBuilder = new SqlConnectionStringBuilder(cnnString);
    builder.InitialCatalog = newDataName
    return builder.ConnectionString
End Function

You could change your connection string calling this method and then executing the query above dropping the part that try to use a different database.

Using myConnection = new SqlConnection(GetNewConnectionString("mynewdatabase-db"))
Using cmd2 = New SqlCommand(MySqlQuery2.ToString, myConnection)     
    cmd2.Parameters.AddWithValue("@MyDomainAlias1", MyDomainName)     
    cmd2.Parameters.AddWithValue("@MyDomainAlias2", "www." & MyDomainName)     
    cmd2.Parameters.AddWithValue("@MyDomainAlias3", MyDomainName & "." & MyTempDomainName)     
    cmd2.ExecuteNonQuery()     
End Using
End Using     

2 Comments

Thanks for the info - I appreciate it! FYI: I didn't delete the table in my query, I deleted it's contents.
Oh yes a bit confused. Time to go to bed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.