Results 1 to 9 of 9

Thread: [RESOLVED] sql script for attaching db in deployment project vb.net2005

  1. #1
    Hyperactive Member yousufkhan's Avatar
    Join Date
    Jan 02
    Location
    India
    Posts
    455

    Resolved [RESOLVED] sql script for attaching db in deployment project vb.net2005

    I am doing a deployment project in that i am calling a sql script to attach sql db by making customaction
    i m gettting a erro Incorredt systex near 'Filneme' where i m writing wrong syntax in the following functions

    Code:
     Private Sub ExecuteSql(ByVal databasename As String, ByVal Sql As String)
            Dim command As New SqlClient.SqlCommand(Sql, masterconnection)
            'Initialize the conncetion, open it and set it to the master db
            masterconnection.ConnectionString = My.Settings.masterConnectionString
            command.Connection.Open()
            command.Connection.ChangeDatabase(databasename)
            Try
                command.ExecuteNonQuery()
            Finally
                'closing the connection shuld be done in a finally block
                command.Connection.Close()
            End Try
    
        End Sub
        Protected Sub AddDBTable(ByVal strDBName As String)
            Try
    
                'Attaching database 
    
                'Creates the batabase
    
                ExecuteSql("master", "CREATE DATABASE " & strDBName & "ON ( FILENAME=" & My.Application.Info.DirectoryPath & "\Data\HospData.mdf ) FOR ATTACH")
    
                'Creates the tables
                ExecuteSql(strDBName, GetSql("sql.txt"))
    
            Catch ex As Exception
                'reports any erros and abort
                MsgBox("In execption handler create database: " & ex.Message)
                Throw ex
    
            End Try
        End Sub
      Public Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)
            MyBase.Install(stateSaver)
            'AddDBTable(Me.Context.Parameters.Item("dbname"))
            AddDBTable("HospData")
    
        End Sub
    Last edited by yousufkhan; Aug 18th, 2012 at 04:57 AM.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,872

    Re: sql script for attaching db in deployment project vb.net2005

    Have you actually looked at the String you're creating? I'll wager not. It never ceases to amaze me that people get errors due to their data but they don't actually look at the data. This is also an example of why it's a good idea to use String.Format rather than multiple & operators: it improves readability. Try displaying that second argument to ExecuteSql and see if it is what you think it is.

  3. #3
    Hyperactive Member yousufkhan's Avatar
    Join Date
    Jan 02
    Location
    India
    Posts
    455

    Re: sql script for attaching db in deployment project vb.net2005

    yes i know my syntax is wrong thats why I am seeking help could you please help me out in writing the correct syntax to attach the database

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,872

    Re: sql script for attaching db in deployment project vb.net2005

    Have you actually done what I said? If so then you should be able to show us what value you are actually passing to ExecuteSql in that second argument.

  5. #5
    Hyperactive Member yousufkhan's Avatar
    Join Date
    Jan 02
    Location
    India
    Posts
    455

    Re: sql script for attaching db in deployment project vb.net2005

    i dont no how to use string.format i tried but i fail to do it properly i want to attach a database from the specific path for which i am trying this functions

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,872

    Re: sql script for attaching db in deployment project vb.net2005

    For the last time, please follow my SIMPLE instruction and display the actual String you're creating. That takes about 5 seconds.
    Code:
    MessageBox.Show("CREATE DATABASE " & strDBName & "ON ( FILENAME=" & My.Application.Info.DirectoryPath & "\Data\HospData.mdf ) FOR ATTACH")
    If you actually look at the data you're using, as I suggested in the first place, it should be quite obvious where the problem is.

  7. #7
    Hyperactive Member yousufkhan's Avatar
    Join Date
    Jan 02
    Location
    India
    Posts
    455

    Re: sql script for attaching db in deployment project vb.net2005

    I have now changed the syntax now i am copying the data files in the folder and trying to attach it,
    now it giving the message database already exist

    ExecuteSql("master", String.Format("CREATE DATABASE HospData ON (FILENAME='D:\MI Clinic\Data\Hospdata.mdf' )FOR ATTACH"))

  8. #8
    Hyperactive Member yousufkhan's Avatar
    Join Date
    Jan 02
    Location
    India
    Posts
    455

    Re: sql script for attaching db in deployment project vb.net2005

    I have now changed the syntax now i am copying the data files in the folder and trying to attach it,
    now it giving the message database already exist

    ExecuteSql("master", String.Format("CREATE DATABASE HospData ON (FILENAME='D:\MI Clinic\Data\Hospdata.mdf' )FOR ATTACH"))

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,872

    Re: sql script for attaching db in deployment project vb.net2005

    You haven't actually changed the syntax of the SQL code. You've simply inserted the space between the database name and and the keyword ON that was missing in the first place. The fact that you are now using a single String literal rather than using string concatenation as you were before makes me worry that you never did actually do what I suggested and see the obvious error and realise that all you had to do was change this:
    Code:
    ExecuteSql("master", "CREATE DATABASE " & strDBName & "ON ( FILENAME=" & My.Application.Info.DirectoryPath & "\Data\HospData.mdf ) FOR ATTACH")
    to this:
    Code:
    ExecuteSql("master", "CREATE DATABASE " & strDBName & " ON ( FILENAME=" & My.Application.Info.DirectoryPath & "\Data\HospData.mdf ) FOR ATTACH")
    I could have told you that from the start but then you could have seen that from the start yourself simply by looking at the String.

    Regardless, if the error message says that the database already exists then I think the issue should be fairly obvious, i.e. the database already exists. I'm not sure of all the exact circumstances under which that error would result but logic suggests that either that data file is already attached to that instance or that instance already has a database with that name. The most likely explanation is that you've simply executed that line of code twice, meaning that, on the second attempt, that data file is already attached as a database with that name, i.e. both at the same time. That means that, if you want to retest that code after executing it successfully once before then you must detach the database first, either using code or manually.

  10. #10
    Hyperactive Member yousufkhan's Avatar
    Join Date
    Jan 02
    Location
    India
    Posts
    455

    Resolved [RESOLVED] Re: sql script for attaching db in deployment project vb.net2005

    Yes the database was already exist on the on which i was trying the setup hence there was no sql management tool so i was not able to understand or check
    I tried on another machine and it was successful thanks for your help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •