Connecting to SQL Server from a Visual Studio 2012 Express Class

  • Can anyone tell me how to create a connection to SQL Server from a class created in VB in Visual Studio 2012 Express?

    I want to write a routine that I can compile into a custom assembly so that I can call the function from a number of SSRS reports.

    The function is straightforward and works well as a function, but I don't want to copy it to multiple reports.

    The function is doing a simple replace in a string, but the list of replacement values needs to come from the database based on a key value

    My table has LocalisationId INT, OrigValue VARCHAR(50), LocalValue VARCHAR(50). I need to get the relevant(s) record from the db and return the LocalValue.

    Any help would be appreciated

  • here's a basic example for connecting, does this help?

    'form a command that waits longer than 30 seconds for testing

    Dim sqlcmd As String = "WaitFor Delay '00:00:45';SELECT name FROM sys.tables;"

    Dim mySqlConnectionFormat As String = "data source={0};initial catalog={1};user id={2};password={3};Trusted_Connection=False;Connect Timeout=600;Application Name=GhostInTheMachine;"

    Dim MyConn As New SqlConnection

    MyConn.ConnectionString = String.Format(mySqlConnectionFormat, ".", "SandBox", "Noobie", "NotARealPassword")

    MyConn.Open()

    'now lets get a command object

    Dim mySqlCommand As New SqlCommand

    mySqlCommand.Connection = MyConn

    mySqlCommand.CommandTimeout = 600

    mySqlCommand.CommandType = CommandType.Text

    mySqlCommand.CommandText = sqlcmd

    Dim myDataReader As SqlDataReader

    myDataReader = mySqlCommand.ExecuteReader

    ' could either loop thru the reader itself, or stick it into a datatable .

    Dim MyDataTable As New DataTable

    MyDataTable.Load(myDataReader)

    'do something witht eh data?

    For Each dr As DataRow In MyDataTable.Rows

    Debug.Print(dr!name) ' [name] because i Know the column name from the query.

    Next

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Thanks Lowell, it does help. I have one more question. My .net is non-existent and so please forgive me if this is a dumb question.

    I have in my code:

    Imports System.Data

    Imports System.Data.SqlClient

    and I'm a lot further on now thanks to your post. However, I'm getting:

    Import of type 'System.ComponentModel.Component' from assembly or module 'System' failed.

    This is occurring on the following line:

    Dim cmd As New SqlCommand

    I thought that SqlCommand would come in Data.SqlClient, but it appears not. I get a similar error on the DataReader too. I tried adding ComponentModel in my imports, but clearly it's not as simple as that.

    What else do I need to add to the imports?

  • Tim in this case, i'm not sure;

    I created a Simple Class library, and this is teh entire code, which compiles correctly in the full version of Visual Studio 2010;

    My project compiles no problem whether i go to Project>>Properties>>Compile>>Advanced Compile Objects, and i point it at any framework, 2.0, 3.5 or 4.0.

    Imports System.Data

    Imports System.Data.SqlClient

    Public Class Class1

    Private Sub x()

    'form a command that waits longer than 30 seconds for testing

    Dim sqlcmd As String = "WaitFor Delay '00:00:45';SELECT name FROM sys.tables;"

    Dim mySqlConnectionFormat As String = "data source={0};initial catalog={1};user id={2};password={3};Trusted_Connection=False;Connect Timeout=600;Application Name=GhostInTheMachine;"

    Dim MyConn As New SqlConnection

    MyConn.ConnectionString = String.Format(mySqlConnectionFormat, ".", "SandBox", "Noobie", "NotARealPassword")

    MyConn.Open()

    'now lets get a command object

    Dim mySqlCommand As New SqlCommand

    mySqlCommand.Connection = MyConn

    mySqlCommand.CommandTimeout = 600

    mySqlCommand.CommandType = CommandType.Text

    mySqlCommand.CommandText = sqlcmd

    Dim myDataReader As SqlDataReader

    myDataReader = mySqlCommand.ExecuteReader

    ' could either loop thru the reader itself, or stick it into a datatable .

    Dim MyDataTable As New DataTable

    MyDataTable.Load(myDataReader)

    'do something witht eh data?

    For Each dr As DataRow In MyDataTable.Rows

    Debug.Print(dr!name) ' [name] because i Know the column name from the query.

    Next

    End Sub

    End Class

    so my first question would be, on the developer machine you are fiddling with, do you have the .Net frameworks installed?

    look in C:\windows\Microsoft.NET\Framework, you should see multiple folders like this, representing each framework installed:

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Still no joy. I've checked that the frameworks are installed. I have 1.0.375, 1.1.4322, 2.0.50727, 3.0, 3.5, 4.0.30319.

    I've taken out all of my original code and tried to compile it but I still get the same error.

    Am I looking at the wrong approach for this?

    The reason I'm writing a custom assembly is so that I can create function that I can use across a number of different reports. It seems to be a spectacularly complicated way of creating a library of functions for RS. Have I got the wrong end of the stick? Is there a simpler method of sharing functions?

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply