I am creating an SSIS package programatically in vb.net version 4.0 for SQL server 2008 R2 and I am having difficulty creating an Execute SQL Task. The code for the task is as follows:
Public Sub CreateExecuteSQLTask()
Dim TaskHost = DirectCast(Package.Executables.Add("Microsoft.SqlServer.Dts.Tasks.ExecuteSQLTask.ExecuteSQLTask, Microsoft.SqlServer.SQLTask, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"), Microsoft.SqlServer.Dts.Runtime.TaskHost)
TaskHost.Name = "Task"
TaskHost.Description = "Task Description"
Dim ExecuteSQLTask As Dts.Tasks.ExecuteSQLTask.ExecuteSQLTask
ExecuteSQLTask = DirectCast(TaskHost.InnerObject, ExecuteSQLTask)
ExecuteSQLTask.Connection = DestinationDBConnectionManager.ID
ExecuteSQLTask.SqlStatementSource = "Select * from Employees"
End Sub
When the above code runs I get the following exception when casting the TaskHost to an Execute SQL command.
Unable to cast COM object of type 'System.__ComObject' to class type 'Microsoft.SqlServer.Dts.Tasks.ExecuteSQLTask.ExecuteSQLTask'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.
I have used the following article as a reference:
Creating Packages in code
Please advise how I can get past this issue.
I managed to work out the problem.
Dim ExecuteSQLTask As Dts.Tasks.ExecuteSQLTask.ExecuteSQLTask
needs to be changed to
Dim ExecuteSQLTask As Dts.Tasks.ExecuteSQLTask.IExecuteSQLTask
As the exception suggested, the COM component can be cast to an interfaces. In the above case, i was casting the COM component to the ExecuteSQLTask class rather than the interface.
I found that casting the COM component is a lot easier than setting the SQL task's properties through XML.
Your problem is .Net 4.0. Or, more specifically, the ExecuteSQLTask assembly code has something in it that makes it incompatible with .Net 4.0. So just add the following lines to your app.config file:
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<runtime>
<NetFx40_LegacySecurityPolicy enabled="true"/>
</runtime>
And it'll work. No, I have no idea why.
Related
After converting VB 2008 project to VB 2012, how to fix the error regarding the MSFlexGridLib, The error says:
Reference required to assembly 'Interop.MSFlexGridLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' containing the type 'MSFlexGridLib.AllowUserResizeSetting'. Add one to your project
The blue line for error occured on the '.AllowUserResizing'
.AllowUserResizing = MSFlexGridLib.AllowUserResizeSettings.flexResizeColumns
Go to your project's properties, select add reference, in COM find Microsoft FlexGrid Control and select it to add it as reference.
I am building the sample MvcMovie tutorial for ASP.NET MVC 4. I'm using EntityFramework Code First features and created a connectionString as follows.
<add name="MoveDBContext"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDictionary|\Movies2.mdf;Integrated Security=True"
providerName="System.Data.SqlClient"
/>
Everything worked fine at this point. But then I realized that I named my connection string MoveDBContext instead of MovieDBContext and, being the perfectionist I renamed it. After doing this I now receive an error in my MoviesController/Index method.
public class MoviesController : Controller
{
private MovieDBContext db = new MovieDBContext();
public ActionResult Index()
{
return View(db.Movies.ToList()); // Error: Invalid value for key 'attachdbfilename'
}
...
}
If I change the name back to MoveDBContext the error goes away.
Can anyone tell me where this original name is being referenced?
EF, by default, looks for a connection string with the same name as the type that extends DbContext.
Or, better put by Scott:
By default, when you create a DbContext class with EF code-first, it
will look for a connection-string that matches the name of the
context-class. Since we named our context class “NerdDinners”, it
will by default look for and use the above “NerdDinners” database
connection-string when it is instantiated within our ASP.NET
application.
Edit:
After looking closer, I think your connection string is the problem. You've got DataDictionary instead of DataDirectory. Try this (line feeds added for readability):
<add name="MovieDBContext"
connectionString="Data Source=(LocalDB)\v11.0;
AttachDbFilename=|DataDirectory|\Movies.mdf;
Integrated Security=True"
providerName="System.Data.SqlClient" />
Apparently, as Ken said, the MoveDBContext was not being referenced.
I removed the entire connectionString from the web.config and everything still functioned correctly.
So, it still begs the question, "How did Visual Studio know to create a database in my SQLExpress instance?" and "Where is that configured at?"
I’m trying to open a data base on embed vb code. On the MyConnection.Open() I get a error saying
Warning 1 [rsRuntimeErrorInExpression] The Value expression for the
textrun ‘Textbox1.Paragraphs[0].TextRuns[0]’ contains an error:
Request for the permission of type
'System.Data.SqlClient.SqlClientPermission, System.Data,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
failed. C:\Users\tedpottel\Documents\Visual Studio
My datasource in the report is Data Source=(local);Initial Catalog=bw
vb code is:
Public Function TestData () As Boolean
Dim MyConnection As System.Data.SQLClient.SqlConnection
Dim MyCommand As System.Data.SQLClient.SqlCommand
MyConnection = New System.Data.SQLClient.SqlConnection("server=(local);Initial Catalog=bw")
MyConnection.Open()
MyConnection.Close()
return True
End Function
First result on Google for "System.Data.SqlClient.SqlClientPermission":
Hi Joost,
Based on your description, this issue was probably related to the Code Access Security. Please refer to the following articles to configure the assembly to make sure it is set to fulltrust. Request for the permission of type 'SecurityPermission' failed:
http://blogs.msdn.com/b/floditt/archive/2009/08/10/request-for-the-permission-of-type-securitypermission-failed.aspx
Follow the link for information on how to configure your code to run at full trust.
The tool CasPol.exe (part of the .Net SDK) will help you to identify the CodeGroup that the System.Security assembly belongs to. In the GAC folder of this assembly I did a CasPol –rsg (resolvesGroup) on this assembly:
Now we can see that this assembly belongs to the Microsoft_Strong_Name (line 3 in the CodeGroup excerpt) and is configured for FullTrust. With the –rsp (resolvesPermissions) you can visualize all permissions that are granted to this assembly (as defined in the FullTrust named PermissionSet):
When the exception was raised, this assembly was configured to ‘Nothing’ (line 3 of the NamedPermissionSets excerpt). The problem was resolved by granting this assembly FullTrust.
I've been getting this error when I try and use my model container:
No connection string named 'PFModelContainer' could be found in the
application config file.
I have my edmx file in a separate project. I checked the app.config file and my model was there, and I also put it in my main project app.config file. Still doesn't work. Here's the connection string:
<connectionStrings>
<add name="PFModelContainer"
connectionString="metadata=res://*/PFModel.csdl|res:
//*/PFModel.ssdl|res://*/PFModel.msl;
provider=System.Data.SqlServerCe.3.5;
provider connection string="
Data Source=C:\Documents and Settings\Jon\My Documents\Visual
Studio 2010\Projects\SpreadsheetAddIn
\SpreadsheetAddIn\bin\Debug\PFData.sdf;
Password=password""
providerName="System.Data.EntityClient" />
</connectionStrings>
Here's how the context is called:
Private mdbContext As New PFModelContainer
Which goes to:
Partial Public Class PFModelContainer
Inherits DbContext
Public Sub New()
MyBase.New("name=PFModelContainer")
End Sub
I thought the answer would be similar to what happened to this guy. But unfortunately his solution doesn't work with mine.
Update:
I've noticed the error isn't caught until I hit this code. It occurs when I do the linq query on the third line.
Dim dbContext As New PFModelContainer
Dim dbAccount As IQueryable(Of Account)
dbAccount = From a In dbContext.Accounts
Where (a.AccountName = "Hello")
Select a
Update (What I've tried for connection strings - that I can remember):
1 Main Project: --> Default Creation
<add name="PFModelContainer"
connectionString="metadata=res://*/PFModel.csdl|
res://*/PFModel.ssdl|
res://*/PFModel.msl;
provider=System.Data.SqlServerCe.3.5;
provider connection string="
Data Source=C:\Documents and Settings\Jon\My Documents\Visual Studio 2010\Projects\SpreadsheetAddIn\PFDatabase\bin\Debug\PFData.sdf;
Password=password""
providerName="System.Data.EntityClient" />
Library:
<add name="PFModelContainer"
connectionString="metadata=res://*/PFModel.csdl|
res://*/PFModel.ssdl|
res://*/PFModel.msl;
provider=System.Data.SqlServerCe.3.5;
provider connection string="
Data Source=|DataDirectory|\bin\Debug\PFData.sdf;
Password=password""
providerName="System.Data.EntityClient" />
2 Main Project: --> Replace * with PFDatabase
<add name="PFModelContainer"
connectionString="metadata=res://PFDatabase/PFModel.csdl|
res://PFDatabase/PFModel.ssdl|
res://PFDatabase/PFModel.msl;
[...Same...]
Library:
[...Same w/ modifications...]
3 Main Project: --> Replace res://*/ with .\
<add name="PFModelContainer"
connectionString="metadata=.\PFModel.csdl|
.\PFModel.ssdl|
.\PFModel.msl;
[...Same...]
Library:
[...Same w/ modifications...]
4 Main Project: --> Replace res://*/ with ~\
<add name="PFModelContainer"
connectionString="metadata=~\PFModel.csdl|
~\PFModel.ssdl|
~\PFModel.msl;
[...Same...]
Library:
[...Same w/ modifications...]
If you are placing your edmx model in a separate class library, add an app.config to that class library and add the connection string to that config.
Additionally, if your datamodel resides inside a namespace then you will have to include the full namespace in your resource path:
For example, if you placed your edmx file in an assembly called MyProject.DataLayer and the namespace for the generated code is MyProject.DataLayer.DataModel, then your configuration string should be:
<add name="PFModelContainer"
connectionString="metadata=res://*/DataModel.PFModel.csdl|res:
//*/DataModel.PFModel.ssdl|res://*/DataModel.PFModel.msl;
provider=System.Data.SqlServerCe.3.5;
provider connection string="
Data Source=C:\Documents and Settings\Jon\My Documents\Visual
Studio 2010\Projects\SpreadsheetAddIn
\SpreadsheetAddIn\bin\Debug\PFData.sdf;
Password=password""
providerName="System.Data.EntityClient" />
Got some help from experts exchange on this one. Ended up doing a work around (not sure why it wasn't working like it should), since I'm using DbContext instead of EntityObject I had to create my own overrideable procedure like (the second one below):
Public Sub New()
MyBase.New("name=PFModelContainer")
End Sub
Public Sub New(ByVal connectionString As String)
MyBase.New(connectionString)
End Sub
I then had to create my own connection string, which is basically what the original code generated, so, I'm not sure why it wasn't working from the app.config file. Maybe there's a bug in the program that will be fixed the next go around? Hopefully that was it, either that or I did something wrong, mystery.
We have 3 databases providers we use to connect to our databases: DB2, MS SQL, and Interbase. I would like to create a single generic database wrapper class that can be used to talk to all three just by passing in the correct connection string, username, password, and the provider desired.
I don't want to have to add references and import all three providers in the database class. Is this possible?
I have done this before in Java using the Class.forName() function.
There is an abstract factory built into .NET 2.0 or later, an example of its use would be:
Dim factory As System.Data.Common.DbProviderFactory
factory = System.Data.Common.DbProviderFactories.GetFactory("System.Data.SqlClient")
Dim conn As System.Data.Common.DbConnection = factory.CreateConnection()
conn.ConnectionString = "connectionString"
conn.Open()
There are methods on DbProviderFactory like CreateCommand, CreateDataAdapter, etc.
If you do not want to have references to the individual providers in your application, you will need to handle this a little differently.
There are two main options I see - the first (and easiest) would be to use a dependency injection framework to just plugin the appropriate provider at runtime. This is simple, clean, and works well.
You could do it yourself without that, though. Just make a general purpose base class that provides the interface, and, for each provider, make a separate assembly (so the references are separated) that implements this base class. You can then use Activator.CreateInstance to create an instance of the appropriate type at runtime.
To expand on Patrick McDonald's answer, you can store the provider name and connection string in the <connectionStrings> section of your application configuration file. Then you don't need to have the providers hardcoded in the application:
ConnectionStringSettings c = ConfigurationManager.ConnectionStrings["MyConnectionName"];
if (c != null)
{
DbProviderFactory factory = DbProviderFactories.GetFactory(c.ProviderName);
IDbConnection connection = factory.CreateConnection();
connection.ConnectionString = c.ConnectionString;
...
}
Where your application configuration file contains a connectionStrings section something like:
<connectionStrings>
<add name="MyConnectionName" providerName="System.Data.SqlClient" connectionString="Data Source=serverName;Initial Catalog=DBName;Integrated Security=True"/>
</connectionStrings>
can you use framework 3.5 sp1?
if yes, you should look at Linq to Entity