Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Was hoping you could shed some insight into my problem. My CLR function that I deployed to my Sql Server 2005 database executes cleanly when it does nothing except return a value of zero. When I have it try to execute a Windows Batch file that has a single line in it (dir > c:\t-ware\dirlist.txt) I get the following error:

Msg 6522, Level 16, State 1, Procedure RunVIIMPORT, Line 0
A .NET Framework error occurred during execution of user-defined routine or aggregate "RunVIIMPORT":
System.Security.SecurityException: Request failed.
System.Security.SecurityException:
at T_Ware_SQL.Utilities.RunMAS90BatchFile()
at T_Ware_SQL.StoredProcedures.RunVIIMPORT()

The VB line of code to execute the batch file is: System.Diagnostics.Process.Start(""d:\t-ware\mytest.bat")

Also, did the following with the same results: System.Diagnostics.Process.Start("dir > test.txt")

(I need to execute a MAS90 Visual Integrator job and it works from a batch file...just need to be able to execute the batch file from Sql Server...)

If you could provide any help, it would be greatly appreciated. Thanks, Jeff.
Posted
Updated 23-Jun-11 3:07am
v2

1 solution

There is no such thing as "DOS Batch file" anymore! How much longer shall we see such questions about something which does not exist.
Batch files are batch files; there is nothing of DOS in them. By the way, the syntax became more advanced compared to DOS time; writing batch became considerably easier (for example, there are procedures now).

Now, you don't need to run this Process. Running external processes can be used only as a last resort. You need to get the files in your application code:

C#
string[] files = System.IO.Directory.GetFiles(myDirectory);


For other options, please see other overloaded System.IO.Directory.GetFiles methods:
http://msdn.microsoft.com/en-us/library/07wt70x2.aspx[^].

Never use hard-coded immediate constants like "d:\t-ware\mytest.bat". How would you maintain them? In particular, all directories should be calculated our of your entry assembly location and/or configuration data.

—SA
 
Share this answer
 
Comments
Jeff Pufnock 23-Jun-11 9:06am    
Thank you for your reply. Executing System.IO.Directory.GetFiles resulted in what looks like a security error (below). Do you know of a setting that could be changed to fix this? Thanks - Jeff

Msg 6522, Level 16, State 1, Procedure RunVIIMPORT, Line 0
A .NET Framework error occurred during execution of user-defined routine or aggregate "RunVIIMPORT":
System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
System.Security.SecurityException:
at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
at System.Security.CodeAccessPermission.Demand()
at System.IO.Directory.InternalGetFileDirectoryNames(String path, String userPathOriginal, String searchPattern, Boolean includeFiles, Boolean includeDirs, SearchOption searchOption)
at System.IO.Directory.GetFiles(String path, String searchPattern, SearchOption searchOption)
at System.IO.Directory.GetFiles(String path)
at T_Ware_SQL.StoredProcedures.RunVIIMPORT()

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900