942,956 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 1718
  • VB.NET RSS
Apr 14th, 2009
0

Need Help Executing SQL statement in VB

Expand Post »
I need to execute the following SQL command from within VB:

VB.NET Syntax (Toggle Plain Text)
  1. CREATE LOGIN [rx] WITH PASSWORD=N'rx', DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF

I've been trying to find info for days but everything always deals with updating tables etc

If someone could show me the connection string and have to execute the statement that would be great!!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Merovingian is offline Offline
40 posts
since Apr 2009
Apr 15th, 2009
0

Re: Need Help Executing SQL statement in VB

You didn't mention which DB you're using.

For help with connection strings, check The connection string reference. After that you may succeed in creating a new login.
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Apr 15th, 2009
0

Re: Need Help Executing SQL statement in VB

Ok this is my script:

VB.NET Syntax (Toggle Plain Text)
  1. sqlservername = Tservername.Text
  2. Dim SQLStr As String
  3. Dim ConnString As String
  4. ConnString = "Data Source=" + System.Environment.MachineName + "\" + sqlservername + ";Initial Catalog=Master;User ID=sa;Password=rx"
  5.  
  6. SQLStr = "CREATE LOGIN [rx] WITH PASSWORD=N'rx4', DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF"
  7. SQLStr = "EXEC sys.sp_addsrvrolemember @loginame = N'rx', @rolename = N'sysadmin'"
  8. SQLStr = "ALTER LOGIN [rx] ENABLE"
  9. SQLStr = "CREATE DATABASE [RxMa] ON PRIMARY ( NAME = N'RxMa', FILENAME = N'C:\SQL\RxMa.mdf' , SIZE = 3000KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) LOG ON ( NAME = N'RxMa_log', FILENAME = N'C:\SQL\RxMa_log.ldf' , SIZE = 1024KB , MAXSIZE = 300GB , FILEGROWTH = 10%)"
  10. SQLStr = "ALTER DATABASE [RxMa] SET COMPATIBILITY_LEVEL = 100"
  11.  
  12. Dim SQLConn As New SqlConnection() 'The SQL Connection
  13. Dim SQLCmd As New SqlCommand() 'The SQL Command
  14.  
  15. SQLConn.ConnectionString = ConnString 'Set the Connection String
  16. SQLConn.Open() 'Open the connection
  17. Try
  18. SQLCmd.Connection = SQLConn 'Sets the Connection to use with the SQL Command
  19. SQLCmd.CommandText = SQLStr 'Sets the SQL String
  20.  
  21. SQLCmd.ExecuteNonQuery() 'Executes SQL Commands Non-Querys only
  22. Ldbcomplete.Text = "DB and Users Successfully Setup"
  23. Ldbcomplete.ForeColor = Color.DarkGreen
  24. Ldbcomplete.Visible = True
  25. Catch ex As Exception
  26.  
  27. MessageBox.Show("Something Went Wrong, Check SQL Server Name", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
  28. End Try
  29. SQLConn.Close() 'Close the connection

It connects just fine but it only executes the first SQLStr then errors out. Any ideas?
Last edited by Merovingian; Apr 15th, 2009 at 3:10 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
Merovingian is offline Offline
40 posts
since Apr 2009
Apr 15th, 2009
0

Re: Need Help Executing SQL statement in VB

You replace SQLstr variable when you build the command. You should append commands to previous one (and a linefeed)
VB.NET Syntax (Toggle Plain Text)
  1. SQLStr = "CREATE LOGIN [rx] WITH PASSWORD=N'rx4', DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF" & Environment.NewLine
  2. SQLStr = SQLStr & "EXEC sys.sp_addsrvrolemember @loginame = N'rx', @rolename = N'sysadmin'" & Environment.NewLine
  3. SQLStr = SQLStr & "ALTER LOGIN [rx] ENABLE" & Environment.NewLine
  4. SQLStr = SQLStr & "CREATE DATABASE [RxMa] ON PRIMARY ( NAME = N'RxMa', FILENAME = N'C:\SQL\RxMa.mdf' , SIZE = 3000KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) LOG ON ( NAME = N'RxMa_log', FILENAME = N'C:\SQL\RxMa_log.ldf' , SIZE = 1024KB , MAXSIZE = 300GB , FILEGROWTH = 10%)" & Environment.NewLine
  5. SQLStr = SQLStr & "ALTER DATABASE [RxMa] SET COMPATIBILITY_LEVEL = 100"
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Apr 15th, 2009
0

Re: Need Help Executing SQL statement in VB

Your a life saver!!! thank you so much!
Reputation Points: 10
Solved Threads: 0
Light Poster
Merovingian is offline Offline
40 posts
since Apr 2009
Apr 15th, 2009
0

Re: Need Help Executing SQL statement in VB

I have one more question.

The SQL script above has variables that change depending on system name, installed dir etc. The rest of the SQL I need to pass never changes, I have a txt file dbsetup.sql that contains the other SQL statements.

Now I think what I need to do is read them in line by line and append them to SQLStr, I'm not sure on my syntax.

Should it be:

VB.NET Syntax (Toggle Plain Text)
  1. 'Pulls text out of dbsetup.sql
  2. Dim origsql_NAME As String = System.Environment.CurrentDirectory + "\setupscripts\templates\dbuser.sql"
  3. Dim TextLine As String
  4.  
  5. If System.IO.File.Exists(origsql_NAME) = True Then
  6. Dim objReader As New System.IO.StreamReader(origsql_NAME)
  7. Do While objReader.Peek() <> -1
  8. SQLStr = SQLStr & objReader.ReadLine() & vbNewLine
  9. Loop
  10.  
  11. objReader.Close()
  12. Else
  13. MsgBox("dbuser.sql Doesn't Exist at" + System.Environment.CurrentDirectory + "\SQL\setupscripts\templates")
  14. End If
Last edited by Merovingian; Apr 15th, 2009 at 3:54 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
Merovingian is offline Offline
40 posts
since Apr 2009
Apr 16th, 2009
0

Re: Need Help Executing SQL statement in VB

VB.NET has a very usefull namespace called My. You can access, for example, computer and filesystem easily with it
VB.NET Syntax (Toggle Plain Text)
  1. Dim origsql_NAME As String = System.Environment.CurrentDirectory & "\setupscripts\templates\dbuser.sql"
  2.  
  3. If My.Computer.FileSystem.FileExists(origsql_NAME) Then
  4. SQLStr = My.Computer.FileSystem.ReadAllText(origsql_NAME)
  5. Else
  6. MessageBox.Show("dbuser.sql Doesn't Exist at " & System.Environment.CurrentDirectory & "\SQL\setupscripts\templates", "Script Not Found", MessageBoxButtons.OK, MessageBoxIcon.Warning)
  7. End If
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Printing in VB8, pleas help
Next Thread in VB.NET Forum Timeline: Project HELP





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC