SSIS read flat file connection on script task - c#

I'm working on a 2008 SSIS in which I need to read a flat file so that I can access its content (which has 3 directories paths), so I can store those 3 paths into variables.
The flat file would be in 3 different servers, according to the instance I'm working on (dev,qa,production), so I can't just write the path into a variable because I'd have to rewrite that value every time I'd need to deploy the solution in a different instance.
Something I've tried on the past is to read a flat file using the Directory.GetCurrentDirectory(), but I couldn't debug that and using the F5/run package on VS2008 didn't work (I've read that it doesn't work on VS but once you deploy the package it works fine, but I have no means to prove it but to try).
So, I figured out that, If I can read the path saved on a flat file connection and save it in a string variable, I could modify the connection string value in the .config file once the package is deployed, and read its contents like a normal flat file.
My problem is that I can't figure out how to read the connection string value, and I couldn't find anything online that pointed me in the right direction.
Thanks in advance.

To access connection managers informations from script tasks you can use Dts.Connections property, just declare a string variable, and read the connectionstring property:
string cs;
cs = Dts.Connections["myFlatFileConnection"].AcquireConnection(Dts.Transaction);
Reference:
According to this Microsoft Docs article:
Connection managers provide access to data sources that have been configured in the package. For more information.
The Script task can access these connection managers through the Connections property of the Dts object. Each connection manager in the Connections collection stores information about how to connect to the underlying data source. Read more (+examples)

You'd want something like a C# Script task. You can modify the connection string dynamically there. Within the script you'd modify the value of (if I recall correctly) Dts.Connections.["YourConnection"].ConnectionString.

Since nothing seemed to work, I ended up doing the following:
Inserted the values I needed in a parameters table in the database
generated an Execute SQL Task
assigned the results of that task to the variables
It took me all day but I finally got it.
I followed this thread for references.

Related

Microsoft jet database engine cannot open the file.''. It is already opened exclusively by another user, or you need permission

What is the possible solution for this? I force execute the SSIS Job
but the error encountered is
Microsoft jet database engine cannot open the file.''. It is already opened exclusively by another user, or you need permission
What is solution for this I already change the security
This is the flow of the SSIS. The process of this is to update the data of the SQL Server coming from the MDB.
If I'm ever required to use a communal data source like this, especially if the tooling (Excel/Access) takes an lock when someone is just looking at the file, I find it beneficial to copy the file elsewhere for processing.
Before your Update Control Rate data flow, have a File System Task. Define it as a copy with overwrite/clobber/replace from \\server\share\TDMAT TEST\TDMAT.mdb (or whatever the obscured path is) to a local folder that SQL Server Agent/Service Account/SSIS credentialed account has read/write access to. I usually have a folder defined like C:\ssisdata\data_domain\input. So, copy TDMAT.mdb to C:\ssisdata\tdmat\tdmat.mdb.
Then, you have your JET connection managers reference the local file. Away goes your concurrent usage issues. The clever among you might question why we can copy a file that is "locked" but not read it and I can't tell you the why, just that this approach works.

what is advantage of .ini file when set db connect (winform)

I am now involved in a small c# project on winform (just 2-3 select statements),
and the project need to connect to mssql.
I know there are many ways to connect DB.
but why people use .ini or xml file to connect DB?
Is not good insert connect statement(server, id, pw ...) to class?
It is easier to use Configuration file (.ini, .xml) if you want to change connection string later. If you put your data in your code, everytime you change it, you must re-compile your code.

How to store the database in the installation folder w/o user being able to access it? (C#)

I never see database files in the installation folders of random programs, yet they obviously have one. My question is how do they do it?
EDIT: My database can be either on SQL Server, MySql, or Access I'm not bothered, however I would like the client to not have to download SQL Server or any other programs in order to be able to use mine.
You never see database files in the installation folders because installation folders are meant for programs, not for data. The data go into the appdata folders, such as "C:\Documents and Settings\User1\Application Data\Company1\Application1" or "C:\Users\User1\AppData\Company1\Application1" depending on your OS.
I never see database files in the installation folders of random programs yet they obliviously have one
If they are oblivious to database files then they don't need them. That is why you don't see them.
You can place the database file in Hidden Mode so that user can't see it until and unless he has Show Hidden Files option true
Or instead of placing database file along with exe you can place in dedicated application directory like C:\users\username\appdata\yourapp\
My suggestion would be to store your own database as a flat XML file (for example, a plain .NET DataSet saved to file via DataSet.WriteXml ) then apply your own fixed encryption to that file. the key to encrypt/decrypt will be inside your program code and need never be altered. By storing your own data as XML you wont need a client. At the start of your program, Read and decrypt your datafile into memory, then save and encrypt out when needed.

Issue with appSettings using 2 different projects

I'll try to do my best to explain my problem.
I have 2 separate projects which are part of the same application in Visual Studio. One of them is server-sided and the other is client-sided.
The client sided project uses an appSettings key called XMLFileName which is used to retrieve data from an XML and populate a dataset with the retrieved info. In this client sided project I have a method that performs some check in the dataset.
When I try to call that method from the server sided project, I can't get the dataset populated since the XMLFileName isn't being read by the server sided project due to it not being defined in its application settings. If I hardcode the file name string on the server sided project it won't find it since it looks in a different folder.
How should I proceed with this? Am I being clear enough?
Thanks,
Eton B.
Why not just add the same setting to the server-side project? Am I missing something?
I may not understand the problem completely, but it sounds like you have two different applications (client and server) using the same method (e.g. GetXmlFile()) to retrieve the same file (File.xml). If that's the case, then you probably need to have the XML file on a shared drive that can be accessed by a UNC path (e.g. `\myclient\XMLFiles\File.xml').
If that is the scenario, can you change the method GetXmlFile() to use a UNC path to access the file, and to store a UNC path and file name in your appSettings?
If I'm mis-understanding the situation, please let me know and I'll update my answer accordingly.
Hope this helps.

How to overcome vs_needsnewmetadata error in Data Flow task?

I have an SSIS package that copies the data in a table from one SQL Server 2005 to another SQL Server 2005. I do this with a "Data Flow" task. In the package config file I expose the destination table name.
Problem is when I change the destination table name in the config file (via notepad) I get the following error "vs_needsnewmetadata". I think I understand the problem... the destination table column mapping is fixed when I first set up the package.
Question: what's the easiest way to do the above with an ssis package?
I've read online about setting up the metadata programmatically and all but I'd like to avoid this. Also I wrote a C# console app that does everything just fine... all tables etc are specified in the app.config ... but apparently this solution isn't good enough.
Have you set DelayValidation to False on the Data Source Destination properties? If not, try that.
Edit: Of course that should be DelayValidation to True, so it just goes ahead and tries rather than checking. Also, instead of altering your package in Notepad, why not put the table name in a variable, put the variable into an Expression on the destination, then expose the variable in a .DtsConfig configuration file? Then you can change that without danger.
Matching source destination column with case sensitive has done the work for me.
Like in my case SrNo_prod was column in dev and using it we developed the dtsx, while it is been created as SrNo_Prod in prod, after making case change from P to p, we got successful execution of package.
Check if the new destination table has the same columns as the old one.
I believe the error occurs if the columns are different, and the destination can no longer map its input columns to the table columns. If two tables have the same schema, this error should not occur.
If all you are doing is copying data from one SQL2005 server to another I would just create a Linked Server and use a stored proc to copy the data. An SSIS package is overkill.
How to Create linked server
Once the linked server is created you would just program something like...
INSERT INTO server1.dbo.database1.table1(id,name)
SELECT id, name FROM server2.dbo.database1.table1
As far the SSIS package I have always had to reopen and rebuild the package so that the meta data gets updated when modifying the tables column properties.

Categories

Resources