SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Connecting to SQL Server from a Visual Studio 2012 Express Class


Connecting to SQL Server from a Visual Studio 2012 Express Class

Author
Message
Tim Foster
Tim Foster
Valued Member
Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)

Group: General Forum Members
Points: 52 Visits: 114
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
Lowell
Lowell
SSC Guru
SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)

Group: General Forum Members
Points: 67597 Visits: 40894
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!
Tim Foster
Tim Foster
Valued Member
Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)

Group: General Forum Members
Points: 52 Visits: 114
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?
Lowell
Lowell
SSC Guru
SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)

Group: General Forum Members
Points: 67597 Visits: 40894
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!
Attachments
frameworks_installed.png (65 views, 52.00 KB)
Tim Foster
Tim Foster
Valued Member
Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)

Group: General Forum Members
Points: 52 Visits: 114
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?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search