Thursday, March 5, 2009

Vintage Girdle Gallery

How to make Visual Studio 2008 Express Edition (VB.net) to connect to a remote SQL Server

The Express Edition of Microsoft Visual Studio lacked the tools to connect to a remote SQL database to produce. One can indeed make the wizard to connect to a local instance, but we want a client-server database application tinker. And that "for free".

So here's a quick guide on how I do without frills a corresponding connection can produce, without the tools and wizards.

After we have created a new project (Windows Forms application), we
Create Our first and foremost a form called frmStart, comes to a text field name txtFeld . The craft was about it for now with form.

I use this example in advance of the simplicity that you have a working SQL server on the network, in which (if SQL Express) Remote Connections are enabled!

the code here:


Public Class frmStart

frmStart_Load Private Sub (ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim cmdSQL As SqlClient.SqlCommand
Dim asyncResult As System.IAsyncResult
Dim returnValue As SqlClient.SqlDataReader
Dim strSQL As String

Using conSQL As New SqlClient.SqlConnection("Data Source=[SERVERNAME/INSTANZ];Initial Catalog=[DATENBANKNAME];User Id=[USERNAME];Password=[PASSWORT];Async=true")
conSQL.Open()
strSQL = "[SQLSTRING]"
cmdSQL = New SqlClient.SqlCommand(strSQL, conSQL)
cmdSQL.BeginExecuteReader asyncResult = ()
returnValue = cmdSQL.EndExecuteReader (asyncResult)
returnValue.Read If () Then
Me.txtFeld.Text returnValue.Item = ( "[Field Name]")
End If End Using

End Sub End Class




What does this mean in detail?

The variable declaration:

As SqlConnection Dim cmdSQL
Dim asyncResult As System.IAsyncResult
returnValue SqlClient.SqlDataReader
Dim strSQL As String

  • cmdSQL is the Command object, which is the container for the SQL command
  • asyncResult is information on the status the asynchronous operation (New in ADO.net 2.0 - try on asynchronous / synchronous operations please Googel, leading too far)
  • return value is the Reader object that later will contain our result
  • strSQL is only the string variable for our SQL command
Connecting to the database server:

Using conSQL As New SqlClient.SqlConnection ("Data Source = [SERVERNAME / INSTANCE]; Initial Catalog = [dbname]; User Id = [USERNAME] Password = [PASSWORD]; Async = true")
conSQL.Open ()


  • Using [Connection variable] As New SqlClient.SqlConnection ([Connection String]) ensures that we use the connection string defined with the connection within the Using statement. As the Connection String should look like? -> http://www.connectionstrings.com/ The connection string must have the statement, included so that the asynchronous with the Async = true " Operation works!
  • conSQL.Open () opens the connection finally
Send Command / Result received:

strSQL = "[SQLString]
cmdSQL = New SqlConnection (strSQL, conSQL)
asyncResult = cmdSQL.BeginExecuteReader ()

  • strSQL = "..." Here we define our SQL string, so SELECT * FROM tblIrgendwas
  • then the Command object, the SQL string and the connection will go to the command passed
  • and we have the status of the async operation of the previously defined variable
display the data:

returnValue = cmdSQL.EndExecuteReader (asyncResult)
returnValue.Read If () Then
Me.txtFeld.Text returnValue.Item = ("[Field Name]")

  • our reader object "return value" we have the result of the query to ...
  • ... then, important thing, only when the reader object has finished reading ...
  • ... we have to the text box, type a value! If this would be missing if it comes höchstwarscheinlich an error because the program is faster in the next line when the server is the result has delivered! It then comes the error:
    "To evaluate an indexed property, the property must be qualified and the arguments must be explicitly provided by the user." (Item) and
    "Invalid attempt to read when no data are available." (Message)
and the fuss ...

End If
End Using
End Sub
End Class

  • close everything back what we have to press opened
then F5 and pleased that in the text box the contents of the field indicates that you have requested. tata!

0 comments:

Post a Comment