open external files or applications from sql command using sql server - c#

i want to open notepad, word or excel files from sql server using some query. is this possible? or is it possbile using C#.NET winform application. ? also, i want sql to use dll files present it my hard drive. how do i do both these tasks? please help me out with the code as well.
EDIT:
i have a .net winform application which accessess sql server database & is used to insert data in the database.
i want that when a user deletes a new record from a table in the database, & clicks the DELETE button on the Form, then the deleted data is exported to an excel file and the file is immediately opened.
is this possible?
for the export i would use bcp utility, but how to open the file ?

You can use XP_cmdshell to execute a process from within MS SQL server.
http://msdn.microsoft.com/en-us/library/ms175046.aspx
I don't think you'll see the application actually open/run because MSSQL runs as a service and services have their own desktop that you can't see. You'll see the process running in task manager's processes tab however.
You can also excute other processes from a GUI application using the System.Diagnostics.Process class
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx
also, i want sql to use dll files
present it my hard drive
I don't understand this part. Please explain clearly what it is you're trying to do. What these dll files are and what they do and who created them. On the face of it I'd say no you can do this. But more importantly, I'd suggest you re-think the design of your application.

You can do it in .NET, using code like this:
VB.NET
Dim ps As New ProcessStartInfo
ps.UseShellExecute = True
ps.FileName = fileName
Process.Start(ps)
C#.NET
ProcessStartInfo ps = new ProcessStartInfo();
ps.FileName = filename;
Process.Start(ps);
I use it to open an Excel file I've just created. Should work just as well with other file types (assuming the proper program is installed).

Related

Create database, tables and stored procedure using existing script from txt file using C#

I have developed a winform application using C# and SQL Server 2008. Now my job is to implement it on client's machine.
I am looking for the best way to create the database tables and stored procedure on client machine to run this application. I have generated the script of all my database objects. And now i want to create all database objects on client's machine with one click C# code that read each table or stored procedure script file (i.e. .sql or .txt) and create them.
No need for smo, but a bit ugly
SqlCommand getDataPath = new SqlCommand("select physical_name from sys.database_files;", baseConnection); // get default path where the sqlserver saves files
string temp = getDataPath.ExecuteScalar().ToString();
temp = temp.Replace(temp.Split('\\').Last(), string.Empty);
StringBuilder sqlScript = new StringBuilder(Scripts.CreateDatabase); //CreateDatabase could be in ressources
///The #### are used to replace the hardcorededpath in your script
sqlScript.Replace("####MAINDATAFILENAME####", string.Concat(temp, "test.mdf"));
sqlScript.Replace("####LOGDATAFILENAME####", string.Concat(temp, "test_log.ldf"));
string[] splittedScript = new string[] { "\r\nGO\r\n" }; //remove GO
string[] commands = sqlScript.ToString().Split(splittedScript,
StringSplitOptions.RemoveEmptyEntries);
Then run every command in commands(SqlCommand cmd = new SqlCommand(command[x], baseConnection);)
Note: For some reasons this needs adminrights, so create a manifestfile.
You need to use SMO to complete this task. The normal ADO.NET stuff will complain about multi-statement execution and the like. It's really pretty easy once you integrated with SMO and have the scripts.
Visual Studio supports database projects, which generates deployment scripts for you. It also allows for deployments from scratch or to upgrade existing databases. The deployments can be automated as part a build within visual studio or the build server. If you're using TFS you can also source-control your database.
Basically no more messing about with scripts!
http://msdn.microsoft.com/en-us/library/ff678491(v=vs.100).aspx
http://msdn.microsoft.com/en-us/library/xee70aty.aspx

How to create an installation that contains a sql server database

I Created an application in c# with Visual Studio 2008, and i'am using SQL Server 2012 for implement databases for the applications.
My Question is :
How can i create an installation that contains the database without using the atach databse method.
Please F1 ! F1 !
You can export a script that will create and populate the database with data. What is the setup tool you are using ?
Or you can make the C# app create the database and default data in the first launch. In both cases you have to make a script file to create/populate the database.
To export the SQL script
right click on the database name in the SQL server manager
tasks->script
in C# open the scrip file using StreamReader and read all the lines. Then using SqlCommand execute the script file. First you need to open connections to Master database as you do not have your database created yet. Then modify the connection string and replace the master database with your database name.
The connection string can be stored in the Application Settings and you can provide a settings dialog to modify it for simplicity make the default connection string connect to the localhost computer and your database name.
The following code modify a setting key names Setting1
MessageBox.Show(Properties.Settings.Default.Setting1);
Properties.Settings.Default.Setting1 = "New Value";
Properties.Settings.Default.Save();
You can also create a custom action in your installer (.msi) file - here's a link that tells you the steps to do that. adding link to example with C# - here

Unable to open the Sqllite DB file when lancuhing the application

I used the PostBuildevnt script to launch the application form link
http://blogs.msdn.com/b/astebner/archive/2006/08/12/696833.aspx?wa=wsignin1.0&CommentPosted=true
and launching the app successfully.I am using the sqllite for app.
I added the DB file in Application Folder/DataBase and using the following code to open the Db file.
string ConnectionString = "data source=" + Path.GetFullPath(".") + "\\DataBase\\CATTDB.db";
If i launch the app from the installation wizard,it is not connecting to db file.it is throwing the error like "Unable to open the file".
If i launch from the start menu or desktop icon ,it is working fine..
What is the problem here?
please help me..
It could be that your working directoy is different when started from the installer...
What does Path.GetFullPath(".") return in this case (log and/or display the value)?
There is always the issue of permission/rights - depending on your OS (i.e. Windows 7...) and the user your running the app with (i.a. Administrator?) for security reasons you are not allowed to write in the application directory... if you need someplace with read+write you should use http://msdn.microsoft.com/de-de/library/system.windows.forms.application.userappdatapath.aspx
Just check whether the db is in that path -if not copy it there- and use it there...
Other locations could be ApplicationData/CommonApplicationData/LocalApplicationData from http://msdn.microsoft.com/de-de/library/14tx8hby.aspx and http://msdn.microsoft.com/de-de/library/system.environment.specialfolder.aspx
All the files are copying correctly..
I tried with Application.ExecutablePath instead of Path.GetFullPath(".")
it work's great...

Backup C# Windows application data

I'm designing a Windows application using C# in Visual Studio. I need to create back up button or something that would back up my data. How do I do that?
Simlpy create a back up for you data base. I should not be a big problem to triger that proces from your code.
check this sites:
How to Create Full Database Backup on MS SQL Server for a Database using T-SQL Backup Database command and SqlCmd Utility
How to: Create a Full Database Backup
Create a new store procedure similar to the content below and call it from your code.
Copy Code USE AdventureWorks2008R2;
GO
BACKUP DATABASE AdventureWorks2008R2
TO DISK = 'Z:\SQLServerBackups\AdventureWorks2008R2.Bak'
WITH FORMAT,
MEDIANAME = 'Z_SQLServerBackups',
NAME = 'Full Backup of AdventureWorks2008R2';
GO

Best way to run a tool from ASP.Net page

I have a developer tool that I want to run from an internal site. It scans source code of a project and stores the information in a DB. I want user to be able to go to the site, chose their project, and hit run.
I don't want the code to be uploaded to the site because the projects can be large. I want to be able to run my assembly locally on their machine. Is there an easy way to do this?
EDIT: I should note, for the time being, this needs to be accomplished in VS2005.
EDIT 2: I am looking for similar functionality to TrendMicro's Housecall. I want the scan to run locally, but the result to be displayed in the web page
You could use a ClickOnce project (winform/wpf) - essentially a regular client app, deployed via a web-server. At the client, it can do whatever it needs. VS2005/VS2008 have this (for winform/wpf) as "Publish" - and results in a ".application" file that is recognised by the browser (or at least, some browsers ;-p).
You might be able to do the same with Silverlight, but that has a stricter sandbox, etc. It would also need to ask the web-server to do all the db work on its behalf.
I want to be able to run my assembly
locally on their machine
Sounds like you want them to download the tool and run it from their local machine, does that work for you?
Any code can scan files given the location and permissions. For a website to open an exe on a different machine and permit that to run and get access to the files contained on the web server would require a horrifically low level of security that would mean the entire system is practically completely open to attack. If your system is completely behind a firewall and hence protected from outside intererance then you want to look more at the permissions and less at the code.
To run an exe on a machine try following notepad example, though you may have to use a specified directory as well
ProcessStartInfo psi = new ProcessStartInfo("notepad.exe");
psi.WindowStyle = ProcessWindowStyle.Hidden;
Process p = new Process();
p.EnableRaisingEvents = true;
p.Exited += new EventHandler(ExitHandlerToKillProcess);
p.StartInfo = psi;
p.Start();
and when done dont forget to kill the Process. Alternately use javascript. Either way watch the security permissions and remember the risks of doing this.
I would probably write some sort of command line tool or service that does the processing and extraction of project data. Then I would use a page to update/register projects that the web server and the command line tool both have common access to. then at specified times either manually or via cron or similar mechanisms extract the data to your database. once you have this, you just use the website to display last extraction times and the extracted data.
if the projects/end users are on a different subnet etc, then you will need the end users to run the tool and then have it post the data into the database.

Categories

Resources