467,075 Members | 1,044 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,075 developers. It's quick & easy.

Insert sql string

How can I make a insert sql string to insert direct in a table(not a
dataset) using the textbox.text propert??

Nov 16 '05 #1
  • viewed: 10896
Share:
10 Replies
This is pretty close. You may also be able to do cn.Execute sqlstring.
System.Data.SqlClient.SqlConnection cn = new
System.Data.SqlClient.SqlConnection(....);
cn.Open();;
System.Data.SqlClient.SqlCommand cmd=new
System.Data.SqlClient.SqlCommand(cn);
cmd.CommandText= "insert into fred (a,b) values (1,'a')";
cmd.ExecuteNonScalar();
cn.Close();

Nov 16 '05 #2
hi
you can also use
cmd.ExecuteNonQuery()

regards
Ansil
Trivandrum

"firebalrog" wrote:
This is pretty close. You may also be able to do cn.Execute sqlstring.
System.Data.SqlClient.SqlConnection cn = new
System.Data.SqlClient.SqlConnection(....);
cn.Open();;
System.Data.SqlClient.SqlCommand cmd=new
System.Data.SqlClient.SqlCommand(cn);
cmd.CommandText= "insert into fred (a,b) values (1,'a')";
cmd.ExecuteNonScalar();
cn.Close();

Nov 16 '05 #3
He meant ExecuteNonQuery(). No such thing as ExecuteNonScalar(). ;-)

--Bob

"Ansil MCAD" <An*******@discussions.microsoft.com> wrote in message
news:E5**********************************@microsof t.com...
hi
you can also use
cmd.ExecuteNonQuery()

regards
Ansil
Trivandrum

"firebalrog" wrote:
This is pretty close. You may also be able to do cn.Execute sqlstring.
System.Data.SqlClient.SqlConnection cn = new
System.Data.SqlClient.SqlConnection(....);
cn.Open();;
System.Data.SqlClient.SqlCommand cmd=new
System.Data.SqlClient.SqlCommand(cn);
cmd.CommandText= "insert into fred (a,b) values (1,'a')";
cmd.ExecuteNonScalar();
cn.Close();

Nov 16 '05 #4
hi
you can also use
cmd.ExecuteNonQuery()

regards
Ansil
Trivandrum

"firebalrog" wrote:
This is pretty close. You may also be able to do cn.Execute sqlstring.
System.Data.SqlClient.SqlConnection cn = new
System.Data.SqlClient.SqlConnection(....);
cn.Open();;
System.Data.SqlClient.SqlCommand cmd=new
System.Data.SqlClient.SqlCommand(cn);
cmd.CommandText= "insert into fred (a,b) values (1,'a')";
cmd.ExecuteNonScalar();
cn.Close();

Nov 16 '05 #5
He meant ExecuteNonQuery(). No such thing as ExecuteNonScalar(). ;-)

--Bob

"Ansil MCAD" <An*******@discussions.microsoft.com> wrote in message
news:E5**********************************@microsof t.com...
hi
you can also use
cmd.ExecuteNonQuery()

regards
Ansil
Trivandrum

"firebalrog" wrote:
This is pretty close. You may also be able to do cn.Execute sqlstring.
System.Data.SqlClient.SqlConnection cn = new
System.Data.SqlClient.SqlConnection(....);
cn.Open();;
System.Data.SqlClient.SqlCommand cmd=new
System.Data.SqlClient.SqlCommand(cn);
cmd.CommandText= "insert into fred (a,b) values (1,'a')";
cmd.ExecuteNonScalar();
cn.Close();

Nov 16 '05 #6


Ok, thx, but if I want to insert the text of a textbox, will this
works???
cmd.CommandText= "insert into fred (name) values
("+txtName.Text.ToString()+")";
Am I right???

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #7


Ok, thx, but if I want to insert the text of a textbox, will this
works???
cmd.CommandText= "insert into fred (name) values
("+txtName.Text.ToString()+")";
Am I right???

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #8
Ah yes ... he didn't really answer your original question explicitly, did
he? Although he gave you enough to put it together pretty easily.

Here's his insert:

cmd.CommandText= "insert into fred (a,b) values (1,'a')";

Now, if you want to put the value of a textbox into field b in his example,
you would replace the value 'a' with the textbox value. That would be the
Text property of the textbox, so if the texbox were named myTextBox, you'd
have:

cmd.CommandText = String.Format("insert into fred (a,b)
values(1,'{0}')",myTextBox.Text);

Of course this is static SQL which is inefficient and sets you up for SQL
injection attacks and would also blow up if for example the user happened to
type an apostrophe into the textbox, so what you would really want to do is
a parameterized query. So his original code would be something like this:

System.Data.SqlClient.SqlConnection cn = new
System.Data.SqlClient.SqlConnection(....);
cn.Open();;
System.Data.SqlClient.SqlCommand cmd=new
System.Data.SqlClient.SqlCommand(cn);
cmd.CommandText= "insert into fred (a,b) values (@a,@b)";
cmd.Parameters.Add("@a",SqlDbType.Int).Value = 1;
cmd.Parameters.Add("@b",SqlDbType.Varchar,20).Valu e = myTextBox.Text;
cmd.ExecuteNonQuery();
cn.Close();

Ignoring, of course, validation in the textbox, error handling, the exact
schema of your database, etc. But this should give you the basic direction.

--Bob

"Ricardo Luceac" <r_******@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...


Ok, thx, but if I want to insert the text of a textbox, will this
works???
cmd.CommandText= "insert into fred (name) values
("+txtName.Text.ToString()+")";
Am I right???

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #9
Ricardo Luceac <r_******@hotmail.com> wrote:
Ok, thx, but if I want to insert the text of a textbox, will this
works???
cmd.CommandText= "insert into fred (name) values
("+txtName.Text.ToString()+")";

Am I right???


Well, that will work in some situations. In others, it could do
horrible things to your database, if people put dodgy things into the
text box. You should use command parameters.

See http://www.pobox.com/~skeet/csharp/faq/#db.parameters

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10


Thx, i already made it work, but this way you show seens to be much
better... thanks...

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Howard Hinnant | last post: by
7 posts views Thread by tano | last post: by
11 posts views Thread by Chris Fink | last post: by
2 posts views Thread by Polyhedron_12 | last post: by
3 posts views Thread by mahajanvit@gmail.com | last post: by
6 posts views Thread by rn5a@rediffmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.