Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need a simple example of how to insert a record with a "text" field into a SQL Server 2008 database table from VB.net.

Note that I am talking about the "text" data type specifically, not strings in general (e.g., not "varchar").
Posted
Updated 8-Mar-11 11:26am
v3
Comments
AspDotNetDev 8-Mar-11 16:46pm    
FYI, I updated your question with the comment you posted in reply to my answer. I have also deleted my answer, since it is not what you were looking for.
SumTinWong 8-Mar-11 17:02pm    
sorry about that.

SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=testdb;Integrated Security=True");
conn.Open();
string Insquery = "INSERT INTO userinfo values('"+TextBox1.text+"')"
SqlCommand cmd = new SqlCommand(Insquery,conn);
cmd.ExecuteNonQuery();

Response.Write("Inserted");

conn.Close();


I hope the above information will be helpful. If you have more concerns, please let me know.
 
Share this answer
 
Comments
AspDotNetDev 8-Mar-11 17:22pm    
Noooooooooooo! One should never do this! Use command parameters instead of creating the command using string concatenation.
Espen Harlinn 8-Mar-11 17:40pm    
Good point ...
William Winner 8-Mar-11 18:14pm    
As an FYI, in case you were curious, the reason you don't do this would be this example:

Someone types the following into TextBox1:

"something');DROP TABLE userinfo;CREATE TABLE Junk (something varchar(255));INSERT INTO Junk VALUES('I just dropped your table and created my own!"

Now, your full insert string equals:

INSERT INTO userinfo values('something');DROP TABLE userinfo;CREATE TABLE Junk (something varchar(255));INSERT INTO Junk VALUES('I just dropped your table and created my own!')
AspDotNetDev 8-Mar-11 18:19pm    
For further research into SQL injection, I recommend reading this: http://xkcd.com/327/

:-)
Monjurul Habib 9-Mar-11 3:10am    
thanx
a look

you can query this on You tube.

and www.asp.net
 
Share this answer
 
v2
Comments
SumTinWong 8-Mar-11 17:01pm    
Thanks. I'll look atit.
Here is a few answers from google[^]

You'll find what you need here[^]

Regards
Espen Harlinn
 
Share this answer
 
Comments
SumTinWong 8-Mar-11 17:01pm    
Thanks. I'll look atit.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900