473,499 Members | 1,681 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple connections to a SQL database

1 New Member
Hi, I am attempting to go through a SQL database, find an encrypted string, decrypt it, and then re-encrypt it with a new passphrase. The problem occurs when I try to write to the database while the Reader is still open.

My code roughly looks like:

SqlConnection sc = new SqlConnection("Server=" + dbserver + ";Database=" + db + ";Trusted_Connection=True;MultipleActiveResultSets =True;);
string qs = "SELECT Page, Revision, Content FROM [" + db + "].[dbo].[" + dbtable + "]";
sc.Open();
SqlCommand command = new SqlCommand(qs, sc);
myReader = command.ExecuteReader();

while(myReader.Read())
{
...
qs = "UPDATE [wiki_data_test].[dbo].[PageContent_v2] SET Content=@catContent WHERE Page=@thePage AND Revision=@theRevision";
command = new SqlCommand(qs, sc);
command.ExecuteNonQuery();
...
}

I get the error "Transaction was deadlocked on lock generic waitable...". I tried to create a separate connection just for the writes, but I get an error stating that I cannot connect while the reader is open.
Oct 5 '11 #1
1 2943
arie
64 New Member
MSDN says:

While the SqlDataReader is being used, the associated SqlConnection is busy serving the SqlDataReader, and no other operations can be performed on the SqlConnection other than closing it. This is the case until the Close method of the SqlDataReader is called. For example, you cannot retrieve output parameters until after you call Close.

Changes made to a result set by another process or thread while data is being read may be visible to the user of the SqlDataReader. However, the precise behavior is timing dependent.


So you can't use the same connection for update.

You are getting the "deadlock" error because your Reader sets a lock on the database. It is probably SHARED lock, as your Reader only "reads" data and other connections can "read" as well, but cannot write. And UPDATE command's lock "changes" during execution. First, when it searches in database for the row to update, it has SHARED lock (because it doesn't know if it'll find anything and may not need to change locks for writing), and then, if it finds anything, the lock type is changed. This may cause a deadlock.

Maybe you should use DataSets and DataTables instead of DataReaders?

What SQL database you're using? MSSQL Server? If so, this http://www.sql-server-performance.co...d-sql-locking/ may help you understand your problem.
Oct 6 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
2016
by: ss | last post by:
Dear All, I use ODBC to connect php (installed on linux) to a MS SQL Server 2000 database. I use iODBC and Freetds. Everything looks fine, until I try to open a multiple connection at once...
8
2006
by: James Baker | last post by:
I've been using ASP.NET for a couple years now and I'm trying to fall back into ASP for a new position I've acquired...and I'm having a bit of trouble remembering. I'm running to a problem where...
1
2009
by: Sergio | last post by:
Hi everybody, I have the following scenario. Several computers with shared disk in a LAN. Each of these computer has a MySQL server that serves several databases. I have several clients that...
0
1496
by: Chris Breemer | last post by:
Hi all, I believe I'm seeing a bug in DB2 UDB 8.1, or in any case a behaviour different from DB2 UDB 7.1. I have a small C program, containing a mixture of ESQLC and CLI calls, that opens two...
1
1667
by: tilmann | last post by:
We are developping an application. The application is written in Cobol, for the database the client can use either a DB2 UDB on Unix systems or a DB2 V 7 on z/OS. The application itself can run on...
0
2187
by: Yelena Varshal | last post by:
Hello, I am using ODBC to access Microsoft Access database. I use just generic ADO code. I don't have problems opening multiple connections from the same page on the development server but the...
1
5312
by: Yelena Varshal via AccessMonster.com | last post by:
Hello, What are the pre-requisites / conditions for the ability to create multiple connections to MS ACCESS database and what is the precedence of its application? adModeShareDenyNone in the code,...
1
1030
by: Anil Gupte | last post by:
This .Net Database stuff is way too complicated. Just to make a Select statement on an Access database, I have to first get a connection string, then open a connection to the database, then create...
15
20629
by: =?Utf-8?B?Vmlua2k=?= | last post by:
Hello everyone, I have this code for TCPListenPort. The code works fine, but my manager is asking me to establish multiple connections to the same port. How can i acheive that below is my...
0
1243
by: whitethomas12 | last post by:
Hi, I currently have some basic code that allows me to run the tracert command through VB.NET and it also updates my database based on the results. I was wondering if someone can help me find...
0
7128
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7385
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5467
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4917
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4597
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
661
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
294
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.