sql server 2005 clr c# checking file status - c#

I need to check the status (existing? or last modified date) of a file on multiple remote windows servers (in LAN). The remote servers need a user name and password to access.
I was trying to do it using T-SQL (sql server 2005), but just thinking it's maybe best to be done using a CLR procedure/function? The reason for using a stored procedure is that this will be used by a ssrs report to show a list of each server has the file (last modified date) or not. The parameter for the procedure/function should be the unc/path of the file on the server.
I knows about CLR, but need the c# code to do this. Thanks.

If the files in question are locatable via UNC path names then this is quite straightforward using c#:
var fInfo = new FileInfo("uncPathGoesHere");
if (fInfo.LastWriteTime > DateTime.Now.AddHours(-1))
{
// file modified within last hour
}

Related

importing data from csv file in remote machine to postgresql table

How can i use copy command for bulk insertion of data into postgresql table from csv file present in remote machine using C#?
I've a front end in C# using which i need to load data into postgresql table from csv file. The database is in remote server and the csv file is in my machine.
Assuming the CSV file is on the machine running the C# program, you need to use COPY ... FROM STDIN. This is difficult to use directly from client drivers, but most of them support their own interfaces on top.
I'm guessing you are using nPgSQL since you didn't bother to mention what client you're using. If so, you can use NpgsqlCopyIn.
Not possible at all, unless you mount the remote machine on the postgres server. To copy postgres needs to be able to access the file locally.
You could try to scp the file from A to B, or parse the file yourself and do bulk inserts into postgres, ie:
create table file (structure of the file);
Read 100 lines
insert into tabe file
values (line 1)
,(line 2)
...
,line (100)

I want to run C# program without changing SQL Server instance or without changing SQL Server installed PC name

I have C# application which uses SQL Server R2 as its database. That database is on a separate PC by the name of SERVER. SQL Server's instance name also server.
My windows application uses DataSet to communicate with the database. Now my SQL installed PC name is change to another name, ex SERVERHP. Now all my coding works want to change my connection string. Are there any other easy way to do it ?
I am to tried to edit hosts file, but it does not work for me.
This is my coding style (http://goo.gl/FQrkp). I am using DataSet with DataAdapter with IDE designers.
I have 100 ~ 150 forms. Now I cannot compile all codings. I want to easy method to connect that SQL Server database.
I want to have a way to hide the change of the hostname of the computer
You have a couple of options:
Globally replace all server names with IP address in connection strings in the app
Globally replace all server names with the new server name in the app
Add a CNAME record to the DNS table on the server (assuming of course you're in the same network, which you are if you're using computer names)
Add an entry to the LMHOSTS file (you can add as many names as you'd like that point to the same IP)
As I understand, it is SqlConnection used to connect to database, or something like that. Why don't you use SqlConnectionStringBuilder? Then you can dinamically construct connection string you need. Also, to get list of servers, you can use SqlDataSourceEnumerator, from namespace System.Data.Sql.
It sounds like you want to have a way to hide the change of the hostname of the computer (Server/Desktop/Virtualized instance of Windows, whatever) that is running your SQL Server.
This isn't my area of expertise, but I can't think of a way to do it that only involves your application code and just the computer.
If you control the local DNS you can create a CNAME entry with the old name that "points" at the new one. Depending on how your connection strings are stored, you might have to edit them or you might not. But you won't have to worry about the location of your SQL Server changing again because you can always edit your CNAME to point at the new location.
Note for the future - not your current problem: If you continue to use MS SQL Server in particular you'll want to be careful about moving it to a computer where it isn't the default instance because then you need to put the instance name in the connection string as well, which might force you to edit all of your application web.configs and app.configs again.
Are you using Visual Studio 2012 with your development efforts? I ran into a conflict with the mini SQL db VS installs (Using Premium version) and had to modify the ConnectionStrings section of my Machine.config file to point to my SQL database. For whatever reason, VS will write references to the mini db in the Machine.Config file (for whatever version of .Net you are leveraging) throwing potetial conflicts.
The file can be found in %systemroot%\Microsoft.Net\Framework64\dot net version\Config
If you're using an x86 processor the Framework64 folder is just called 'Framework'.

Getting files info (accessed/modified dates) from FTP server (C#)

I'm creating a program which downloads files off various types of servers, such as network paths or HTTP servers, based upon criteria. So far I have it working based upon a regex, but I'd also like it to find files newer (last accessed, modified or created) than a given date. This is easy in the network path type because I can access the FileInfo for that file, but all I have in my FTP server is a 'line' string which obviously just holds the file name.
Is it easy/possible to access the last modified/accesesed/created dates for a file on an FTP server in C#?
Unfortunately FTP provides only limited information about the remote file. With default LIST command you get OS-specific response where one date is usually present (this is usually last modification time). With MLST/MLSD extension commands you get machine-parsable response string but also with just one time.
The exact way to get the date depends on what component or class you use to access the FTP server.
If you need to get more than one date (eg. date of creation and last access), and you can go SFTP route, I'd recommend using SFTP instead.
You could use a third party library such as edtFTP to connect to the FTP server and inspect the last modified/created (not sure if you can get the the last accessed timestamp) timestamps. Its quite an easy library to use:

connect to .mdf file in vs2010 , wpf application

I am using VS2010 , and I built a .mdf file using SQL server 2008
I want to use this database file in my wpf application so that I can add rows to it and delete rows from it
the problem is , I can't access this file , and all the insertion and deleting is actually hapening to the datacontexct i created .
I used myDataContexct.Submitchanges() but it didn't work either
I tried to add a connection string when I define the datacontexct that holds the url of my .mdf file and this it gave me a runtime error when trying to access this file and the error messege says :
An attempt to attach an auto-named database for file Trial.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
please help me because I searched alot but I couldn't find any help
If the application is not going to be installed in a manner that many clients are accessing the same server, you would want to consider using SQL Server Compact Edition.
Are you sure the connection string in your app.config refers to the local mdf file? perhaps it refers to the server instance?
What technology do you use, is it LinqToSql or Entity Framework (I think you have t call SaveChanges, not AcceptChanges)?
If you do intend to access the server instance, then the problem seems to be a security restriction.
Please add more details on statement no. 1, and I'll write further info.

how to do attachments for each sql record in c#

I am developing a desktop application in c# & sql 2005. With candidate data entry form I want to provide option to attach required documents(in pdf format) wid data. Kindly let me know the best method. Thank you in advance.
Simply create a table that will contain the filename and server path for the file to be attached, then create a method to copy the attached file to the server location and store the relivant information (name and path) in the table. Use other methods to retrive the file from the server location when requested. Simple.
I personaly prefer to store the documents as BLOBs since server file structures and paths can change over time.
Well then unfortunately you have to either manage the file storage yourself using the servers file system, or you could store it in the db itself (IT WILL GET BLOATED!!!)
See sql server 2005 file storage
How To: Encrypt and Manage Documents with SQL Server 2005
OK, then for file management
see this example
File Manager Component

Categories

Resources