Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Connecting to SQL Server from a Visual Studio 2012 Express Class Expand / Collapse
Author
Message
Posted Friday, May 10, 2013 7:53 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, September 26, 2014 4:50 AM
Points: 24, Visits: 56
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
Post #1451603
Posted Friday, May 10, 2013 11:04 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:11 PM
Points: 12,905, Visits: 32,161
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1451696
Posted Monday, May 13, 2013 8:05 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, September 26, 2014 4:50 AM
Points: 24, Visits: 56
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?
Post #1452120
Posted Monday, May 13, 2013 8:28 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:11 PM
Points: 12,905, Visits: 32,161
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son


  Post Attachments 
frameworks_installed.png (34 views, 52.41 KB)
Post #1452130
Posted Wednesday, May 15, 2013 2:00 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, September 26, 2014 4:50 AM
Points: 24, Visits: 56
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?
Post #1452961
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse