This web site uses Kentico CMS, the content management system for ASP.NET developers.

Executing a SQL Command

SQLPart 2.of 3

This sample PowerShell code get you connected to a SQL Server and run a SQL command "Delete..." using the ".ExecuteNonQuery".  This is all .NET Framework working for you.

Executing a SQL Command

## - SQL Database information
## - Prepare queries to be executed

$SqlServer = "YourServerName";
$SqlCatalog = "YourDatabase";

## Creating a Connection
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection;
$SqlConnection.ConnectionString = "Server = '$SqlServer'; Database = '$SqlCatalog'; " `
+ "Integrated Security = True; Connect TimeOut=300";
$SqlConnection.Open();

## Use only clear the table
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand;
$SqlCmd.Connection = $SqlConnection;
$SqlCmd.CommandTimeout = 200;
$SqlCmd.CommandText = "Delete from tblSysLogs;";
$SqlCmd.ExecuteNonQuery();
$SqlCmd.Connection.Close();
 


I will post more sample reading a table this week. 
Check out the last part 3 of 3 of the series next...
Posted: 12/8/2008 3:16:04 PM by Global Administrator | with 1 comments


Comments
lydyvp
MQeWdh phwdtbeqactz, [url=http://jokvlqpwftnf.com/]jokvlqpwftnf[/url], [link=http://soxufrvnfdth.com/]soxufrvnfdth[/link], http://obkmrwgokjve.com/
4/2/2009 4:58:12 AM

Title

In this blog section I will provide some valid samples currently use at my workplace.  I'll be covering SQL Server version: 2000, 2005, and 2008.  Also, I will post some information on SQLPS.exe.

Take a look at the three part series on:
1. Connecting to SQL Server
2. Executing a SQL Command (using "Delete from...")
3. Read/Display Data from SQL Server

l be posting more thing in the near future such as using SMO and doing some SQL admin operations.  

 

Syndication

RSS
This web site uses Kentico CMS, the content management system for ASP.NET developers.