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.Connection String = "Integrated Security=SSPI;" +
"Initial Catalog=Library DB;" +
"Data Source= .\\SQLExpress";
try
{
cnn1.Open();
SqlCommand cnn1Command = new SqlCommand();
cnn1Command.Con nection = cnn1;
cnn1Command.Com mandText = "UPDATE RESERVED SET ISBN = '"
+ isbn_number + "' , UserID = '" + userID + "'";
SqlDataReader dataReader = cnn1Command.Exe cuteReader();--What should I
put in here in order the UPDATE to work?
dataReader.Clos e();
cnn1.Close();
}
catch (Exception ex)
{
Console.WriteLi ne("Error accessing the database: " +
ex.Message);
}
Cheers!
Claudi
*** Sent via Developersdex http://www.developersdex.com *** 5 2083
On Jun 11, 10:33 am, Claudia Fong <cdolphi...@yah oo.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.Connection String = "Integrated Security=SSPI;" +
"Initial Catalog=Library DB;" +
"Data Source= .\\SQLExpress";
try
{
cnn1.Open();
SqlCommand cnn1Command = new SqlCommand();
cnn1Command.Con nection = cnn1;
cnn1Command.Com mandText = "UPDATE RESERVED SET ISBN = '"
+ isbn_number + "' , UserID = '" + userID + "'";
SqlDataReader dataReader = cnn1Command.Exe cuteReader();--What should I
put in here in order the UPDATE to work?
dataReader.Clos e();
cnn1.Close();
}
catch (Exception ex)
{
Console.WriteLi ne("Error accessing the database: " +
ex.Message);
}
Instead of:
SqlDataReader dataReader = cnn1Command.Exe cuteReader();
use:
cnn1Command.Exe cuteNonQuery();
Hi,
Thanks for the tip! What if I want to change to INSERT INTO
cnn1Command.Com mandText = "INSERT INTO RESERVED (ReservedID, UserID,
ISBN) VALUES (1, userID,isbn_num ber)"; --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 ***
You want to use a parameterized query:
cnn1Command.Com mandText = "INSERT INTO RESERVED (ReservedID, UserID,
ISBN) VALUES (1, @userID, @isbn_number)";
cnn1Command.Par ameters.AddWith Value("@userID" , myLiveUserId variable)
.... etc
cnn1Command.Con nection.Open();
cnn1Command.Exe cuteNonQuery();
cnn1Command.Con nection.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.Com mandText = "INSERT INTO RESERVED (ReservedID, UserID,
ISBN) VALUES (1, userID,isbn_num ber)"; --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 ***
On Jun 11, 12:25 pm, Claudia Fong <cdolphi...@yah oo.co.ukwrote:
Hi,
Thanks for the tip! What if I want to change to INSERT INTO
cnn1Command.Com mandText = "INSERT INTO RESERVED (ReservedID, UserID,
ISBN) VALUES (1, userID,isbn_num ber)"; --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.Com mandText = 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.Com mandText = string.Format(" INSERT INTO RESERVED
(ReservedID, UserID,
ISBN) VALUES (1, '{0}',{1})", userID, isbn_number);
Wow, You should always use parameterized queries!!!
<za***@construc tion-imaging.comwrot e in message
news:11******** *************@q 66g2000hsg.goog legroups.com...
On Jun 11, 12:25 pm, Claudia Fong <cdolphi...@yah oo.co.ukwrote:
>Hi,
Thanks for the tip! What if I want to change to INSERT INTO
cnn1Command.Co mmandText = "INSERT INTO RESERVED (ReservedID, UserID, ISBN) VALUES (1, userID,isbn_num ber)"; --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.Com mandText = 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.Com mandText = string.Format(" INSERT INTO RESERVED
(ReservedID, UserID,
ISBN) VALUES (1, '{0}',{1})", userID, isbn_number);
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 program is checking the URLs, the text in
a label on the current window will not update until after all URLs are checked.
I think the Form/Window is frozen while the http requests are occuring. Does
anyone know of a way I can update the Form in real...
|
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 in C# using windows forms, to create windows
user profiles and then updating the registry information for the new
profiles.
I have managed to create the user successfully and thought that I was
|
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 written in Console Application.
Module Module1
Sub Main()
Call Testing()
|
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 first game, but then
all the other games have exactly the same numbers (e.g. Game 1:
1,2,3,4,5,6.. Game 2: 1,2.3,4,5,6). I don't know how to get delete
the previous array with those numbers, and then generate a new one.
Here is my code:
|
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 to use remoting for our
access to the SQL server, with the remoting being via IIS. Since all of our
users will have accounts in the destination domain, we want to have IIS
handle the security for us and not allow anonymous. We have set this up...
| |
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
deletion or insertion in the dataset but when I am updating the
|
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 all the current
documents using Application.Documents, the objects I got are not same
ones stored previously. The event handler is called by some threads
generated by C#. But, if I get all the current documents using my own
threads, the ones...
|
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 = " + vgSqlServer + "; " +
"database = " + vgSqlServerDb + ";" +
"uid = XXX; pwd = XXX; ";
|
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 problem using concurrency and it worked out well but this problem is stumping me a little. The problem is a non-network black jack game with concurrency.
Here are the specs given:
The game will consist of 3 types of parts: a human user player, a...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |