473,396 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

problem in updating DB using console application

Hi,

I'm using console application to connect to a db and I managed to use
the SELECT statement to display records in my DB but I also need to
UPDATE some fields in my table but after I run the code below, it didn't
update the record..

Can someone tell me?

Cheers

Claudi

SqlConnection cnn1 = new SqlConnection();
cnn1.ConnectionString = "Integrated Security=SSPI;" +
"Initial Catalog=LibraryDB;" +
"Data Source= .\\SQLExpress";
try
{

cnn1.Open();

SqlCommand cnn1Command = new SqlCommand();
cnn1Command.Connection = cnn1;
cnn1Command.CommandText = "UPDATE RESERVED SET ISBN = '"
+ isbn_number + "' , UserID = '" + userID + "'";

SqlDataReader dataReader = cnn1Command.ExecuteReader();--What should I
put in here in order the UPDATE to work?

dataReader.Close();
cnn1.Close();

}
catch (Exception ex)
{
Console.WriteLine("Error accessing the database: " +
ex.Message);
}
Cheers!

Claudi

*** Sent via Developersdex http://www.developersdex.com ***
Jun 11 '07 #1
5 2060
On Jun 11, 10:33 am, Claudia Fong <cdolphi...@yahoo.co.ukwrote:
Hi,

I'm using console application to connect to a db and I managed to use
the SELECT statement to display records in my DB but I also need to
UPDATE some fields in my table but after I run the code below, it didn't
update the record..

Can someone tell me?

Cheers

Claudi

SqlConnection cnn1 = new SqlConnection();
cnn1.ConnectionString = "Integrated Security=SSPI;" +
"Initial Catalog=LibraryDB;" +
"Data Source= .\\SQLExpress";
try
{

cnn1.Open();

SqlCommand cnn1Command = new SqlCommand();
cnn1Command.Connection = cnn1;
cnn1Command.CommandText = "UPDATE RESERVED SET ISBN = '"
+ isbn_number + "' , UserID = '" + userID + "'";

SqlDataReader dataReader = cnn1Command.ExecuteReader();--What should I
put in here in order the UPDATE to work?

dataReader.Close();

cnn1.Close();

}
catch (Exception ex)
{
Console.WriteLine("Error accessing the database: " +
ex.Message);
}
Instead of:
SqlDataReader dataReader = cnn1Command.ExecuteReader();

use:

cnn1Command.ExecuteNonQuery();

Jun 11 '07 #2
Hi,

Thanks for the tip! What if I want to change to INSERT INTO

cnn1Command.CommandText = "INSERT INTO RESERVED (ReservedID, UserID,
ISBN) VALUES (1, userID,isbn_number)"; --userID and isbn_number are
variable and I can't use inside the context.. what should I put instead
of variables? The values are input by the user...
Cheers!

Claudi

*** Sent via Developersdex http://www.developersdex.com ***
Jun 11 '07 #3
You want to use a parameterized query:

cnn1Command.CommandText = "INSERT INTO RESERVED (ReservedID, UserID,
ISBN) VALUES (1, @userID, @isbn_number)";
cnn1Command.Parameters.AddWithValue("@userID", myLiveUserId variable)
.... etc
cnn1Command.Connection.Open();
cnn1Command.ExecuteNonQuery();
cnn1Command.Connection.Close();

-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Claudia Fong" wrote:
Hi,

Thanks for the tip! What if I want to change to INSERT INTO

cnn1Command.CommandText = "INSERT INTO RESERVED (ReservedID, UserID,
ISBN) VALUES (1, userID,isbn_number)"; --userID and isbn_number are
variable and I can't use inside the context.. what should I put instead
of variables? The values are input by the user...
Cheers!

Claudi

*** Sent via Developersdex http://www.developersdex.com ***
Jun 11 '07 #4
On Jun 11, 12:25 pm, Claudia Fong <cdolphi...@yahoo.co.ukwrote:
Hi,

Thanks for the tip! What if I want to change to INSERT INTO

cnn1Command.CommandText = "INSERT INTO RESERVED (ReservedID, UserID,
ISBN) VALUES (1, userID,isbn_number)"; --userID and isbn_number are
variable and I can't use inside the context.. what should I put instead
of variables? The values are input by the user...
First off, INSERT is like UPDATE, execute it with the ExecuteNonQuery
method call.

As one responder mentions, you could accomplish that with a
parameterized query, but, IMHO, the main reason to use a parameterized
query is when one or more of the columns is a Text datatype. You can
accomplish the same thing with:

cnn1Command.CommandText = string.Format("INSERT INTO RESERVED
(ReservedID, UserID,
ISBN) VALUES (1, {0},{1})", userID, isbn_number);

Also, bear in mind this will only work if both columns are a non-
string datatype. Any column that is a string datatype would require
single quotes areound the value. For example, if userID is a VarChar,
then the statement would be:

cnn1Command.CommandText = string.Format("INSERT INTO RESERVED
(ReservedID, UserID,
ISBN) VALUES (1, '{0}',{1})", userID, isbn_number);
Jun 11 '07 #5
Wow, You should always use parameterized queries!!!
<za***@construction-imaging.comwrote in message
news:11*********************@q66g2000hsg.googlegro ups.com...
On Jun 11, 12:25 pm, Claudia Fong <cdolphi...@yahoo.co.ukwrote:
>Hi,

Thanks for the tip! What if I want to change to INSERT INTO

cnn1Command.CommandText = "INSERT INTO RESERVED (ReservedID, UserID,
ISBN) VALUES (1, userID,isbn_number)"; --userID and isbn_number are
variable and I can't use inside the context.. what should I put instead
of variables? The values are input by the user...

First off, INSERT is like UPDATE, execute it with the ExecuteNonQuery
method call.

As one responder mentions, you could accomplish that with a
parameterized query, but, IMHO, the main reason to use a parameterized
query is when one or more of the columns is a Text datatype. You can
accomplish the same thing with:

cnn1Command.CommandText = string.Format("INSERT INTO RESERVED
(ReservedID, UserID,
ISBN) VALUES (1, {0},{1})", userID, isbn_number);

Also, bear in mind this will only work if both columns are a non-
string datatype. Any column that is a string datatype would require
single quotes areound the value. For example, if userID is a VarChar,
then the statement would be:

cnn1Command.CommandText = string.Format("INSERT INTO RESERVED
(ReservedID, UserID,
ISBN) VALUES (1, '{0}',{1})", userID, isbn_number);

Jun 12 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Lentdave67t | last post by:
Thank you in advance for any help you can provide. I am writing a C# program that checks to see if the URLs of favorites/bookmarks are still good. The problem I am having is that while the...
0
by: Wayne Gibson | last post by:
Hi all, Please ignore the other post.. The cat jumped on the machine and sent it before I could stop it!! Was wondering if anybody has expericence this problem.. I am writting an application...
7
by: Job Lot | last post by:
How can I terminate console application in Try…Catch…Finally…End Try block so that code in Finally gets executed. If I use End statement Finally does not get executed. Following is my code...
2
by: jerryau | last post by:
Hi, I'm creating a number generator program, that is supposed to generate 6 unique random numbers for each game. I want to generate this for 6 games. The problem is, that it works for the...
15
by: Ron L | last post by:
We are working on a distributed VB.Net application which will access a SQL database located on a known server. Each client will run on the user's local machine. To implement this, we are trying...
1
by: r2destini | last post by:
Hi Friends, I am new to .Net. So I don't know much. I am facing a problem in updating database through ADO.Net I am creating the dataset and there is no problem in the updation and...
11
by: leiz | last post by:
Hi, I am using COM to automate Word. In the main thread of my program, I store all the Document objects from Application.Documents. In one event handler (the event generated by Word), if i get...
2
by: dc | last post by:
I have a baffling connection problem from my C# console app to a sql server express database. The console application opens the sql database using the following code:- vDataSource = "server =...
2
by: Z E R O | last post by:
I'm in a really fast pace training program and we are covering a lot of material in a relatively short period of time. I've really not had much trouble up until this problem. I had to do one other...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.