how to execute tsql scripts from cmdlet written in c# - sql-server

I want to execute tsql scripts from custom cmdlet written in c#. Can you recommend me the best practice to do it?
I have a few .sql scripts and in my cmdlet I need to iterate through them in specific order and execute each sql script. I also need to run all scripts in transaction if is it possible.
And I need to execute scripts remotely. It means, that there may not be sql server installed on the machine from which is cmdlet executed.
The target sql servers are 2008, 2008R2, 2012.
I have read some approaches, but i'm still not sure what is the best one.
thanks in advance

Related

How to batch database modifications from SQL Server Management Studio

I'm struggling to figure out how to batch modifications to our databases in SQL Server Management Studio. We'll perform actions like editing columns, creating tables, renaming tables, etc. that we need to perform on all of our environments - but not until we're ready to push the application. I'm trying to use the SQL profiler to grab the SQL but this seems horribly inefficient.
Is there a solution built into the application that will track such changes for me?
Looks like I found my answer: http://www.mssqltips.com/sqlservertip/1723/auto-generate-change-scripts-in-sql-server-management-studio-ssms-for-tables/
you can easily create scripts using the osql utility if you are on a version lower than SqlServer 2005.
From sql server 2005 onwards we use sqlcmd

executing queries from ado.net like I do from sql management studio

I am developing a small utility for my team which I want to use while deploying a database release. The developers are using management studio and whenever they make changes in an SP or table, they are creating scripts from management studio (DROP and CREATE) option.
In my utility, I use that script and when I run that script with ADO.Net execute non query, it throws several exceptions like - 'GO' is not correct keyword etc.
Can anyone please suggest what could be a way to run those scripts as is?
Thanks
Use of the Microsoft tools to run these scripts - they understand the GO command (which is not SQL) and strip it out before sending to SQL Server.
See sqlcmd and the older (deprecated) osql utilities.

How can i access to sql server from windows batch

I want to connect to Sql server and running some sql queries. How can i do that?
Thank you for helps..
Check out the SQLCMD utility:
http://msdn.microsoft.com/en-us/library/ms165702.aspx
Works not only on SQL Server 2008 Express, but also on the full versions, too.
You really need more detail in your question.
Based on my most common experiences, though, if you're just running some queries and putting the results somewhere, you'd want to create a SSIS (SQL 2k5 or 2k8) or DTS (SQL 2k5 and before) package and either have Windows schedule call the package or call it through your .bat file.

How to run a ssis package using a sql statement?

i'm using Sql Server 2008. There are options to run from command line. Is there a way to execute an ssis package using sql statement?
-Vivek
Im my experience one of the easiest way to control them from within SQL is not to try an xp_cmdshell etc but to add a job that executes the SSIS package as the first step and then from within the SQL msdb.dbo.sp_start_job 'yourjobname'
This also makes it easy to control which account (via proxy / credential) the job gets run under.

Automatically backup SQL Server database

I need to backup SQL Server database.
Is it possible to do this automatically without human intervention at regular intervals? If so yes then please suggest me how to do it and I'm using SQL Server 2005 Express Edition.
You'll need this:
http://www.codeplex.com/ExpressMaint
Then you can create a .cmd file to run it and schedule it using Scheduled Tasks. I can't give you an exact command line because your setup will be different from mine, but the docs are here:
http://www.sqldbatips.com/showarticle.asp?ID=27
http://www.sqldbatips.com/showarticle.asp?ID=29
There's a walkthrough on backing up SQL Server Express here
in sql 2005 scripts templates you will find backup script
from your code run this script in the time you want to backup your database
thats it i guess

Resources