Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Prevent Deadlocks

Status
Not open for further replies.

JustScriptIt

Technical User
Oct 28, 2011
73
0
0
US
Hello,

I am scripting in powershell to read from table, and then update table.

It works very well - except when another user or program is trying to read or update the table at the same time.

This is unavoidable, so I would like to know how to prevent transaction deadlocks.

Here is code to read from the large table

Code:
$conn.Open()

$sql = SQL statement to read table 

$cmd = New-Object System.Data.SqlClient.SqlCommand($sql,$conn)

$rdr = $cmd.ExecuteReader()

while ($rdr.read()){
    $sql_output += ,@($rdr.GetValue(0), $rdr.GetValue(1), $rdr.GetValue(2), $rdr.GetValue(3))
}

$conn.Close()

Here is code to update the large table:

Code:
$conn_update.Open()

$sql_update = SQL statement to update table

$cmd_update = New-Object System.Data.SqlClient.SqlCommand($sql_update,$conn_update)

$found = $cmd_update.ExecuteNonQuery()  

$conn_update.Close()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top