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

Connect to SQL Server

Part 1 of 3

First, If you already have install Visual Studio, and/or SQL Server then you probably the ADO.NET already loaded.
So, here's an example on how to connect to SQL Server using Windows trusted authentication.  Following best practices, so no hard-coding UserID and Password needed.

Connecting to SQL Server

## - Defining your SQL Server Parameters
$SqlServer = "YourSQLServerName";
$SqlDatabase = "YourDatabaseName";

## - Building the SQL connection string
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection;
$SqlConnection.ConnectionString = "Server = $SqlServer; Database = $SqlDatabase; " `
+ "Integrated Security = True; Connect TimeOut=300";

## Open SQL Coonection
$SqlConnection.Open();

## - Other statements comes here

## - Close connection to SQL Server
$SqlConnection.Close();

## - End of Script


This code will get you connected and you only need to additional code to work with your SQL Server.
Please check out part 2 of 3 on this series Next...

Posted: 12/13/2008 6:23:49 PM by Global Administrator | with 2 comments


Comments
eensqbh
4X62sF kljyrwuvlzig, [url=http://joemruulcohg.com/]joemruulcohg[/url], [link=http://grumraahxbpo.com/]grumraahxbpo[/link], http://sacfywyaywbj.com/
4/5/2009 5:29:30 PM

psixlg
Q4VETq nrkbpzvixuaa, [url=http://qjihhmntgnlg.com/]qjihhmntgnlg[/url], [link=http://orlvdtgscinw.com/]orlvdtgscinw[/link], http://bsrjevtuyune.com/
4/8/2009 10:10:06 PM

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.