Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Insert string from C# program

sqlc# insert C# string

  • Please log in to reply
4 replies to this topic

#1 greengohome

greengohome

    CC Lurker

  • New Member
  • Pip
  • 5 posts
  • Programming Language:C++
  • Learning:C#

Posted 13 August 2012 - 07:10 AM

Hello again, everyone. I am very sorry to come here and ask another silly question, yet one hour of Google searching and forum reading could not help me solve my issue.


I am writing a C# program to which I attached an SQL database. Everything went smoothly until I wanted to INSERT data stored in variables.


What I mean is that, for example, if I have the string MyString, which would be "hello", I have no idea now to actually write that "hello" in my table.


As a sidenote, "INSERT into Table1 values ('hello')" works.


Also, for integers, I managed to achieve my purpose by using "INSERT into Table1 values ("+MyInteger+")". Unfortunately, this method will not work for strings.



So, I would be really grateful if someone could let me know how I can INSERT a string into a table by only using the name of the variable that stores it. A link to the right documentation would be equally great.


Thanks.
  • 0

#2 ertan

ertan

    CC Newcomer

  • Member
  • PipPip
  • 17 posts
  • Location:Turkey

Posted 13 August 2012 - 07:26 AM

Hi :)

calm down ...

in sql, string needs ' (single quote) means you need to write your strings between two quates in your sql query.

let me give you an example

string word = "Hello";


.....

, "INSERT into Table1 values ('" + word + "')"

it must work.

detail

... ( ' " + word + " ' ) "
  • 1

#3 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 13 August 2012 - 10:01 AM

To make things easier on yourself, use string.Format().

For example: mySqlStatement = string.Format("INSERT into Table1 values ('{0}')", myInteger);

Then execute the sql statement in the variable.
  • 1
My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#4 MuStA

MuStA

    CC Regular

  • Member
  • PipPipPip
  • 43 posts
  • Location:Saudi Arabia
  • Programming Language:C#, PHP, Transact-SQL, Lua, Others
  • Learning:C, C++, Others

Posted 13 August 2012 - 10:03 AM

To make things easier on yourself, use string.Format().

For example: mySqlStatement = string.Format("INSERT into Table1 values ('{0}')", myInteger);

Then execute the sql statement in the variable.


+1 to this one ;)

was about to write it xD
  • 1
.Musta { background-color: Black; }

#5 greengohome

greengohome

    CC Lurker

  • New Member
  • Pip
  • 5 posts
  • Programming Language:C++
  • Learning:C#

Posted 13 August 2012 - 11:22 AM

Thank you all for your answers. I got it working now, both ideas are perfect.


And by the way, I was quite calm when writing this topic :D, maybe just a bit disappointed for not being able to complete such a basic task or to at least find the answer somewhere. It's very hard to find an answer when you don't really know the question.
  • 0





Also tagged with one or more of these keywords: sqlc#, insert, C#, string

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download