开发者

ASP.NET - Insert data into an SQL server database when a field has been filled in and a button clicked?

I have three empty fields I need the customer to fill in. When they fill in these boxes and hit submit, I want the contents to be 开发者_高级运维stored in a SQL server data base. How can I do this?


This is quite an open ended question; there are lots of tutorials out there that will help you achieve this. It kind of depends on how you want to do it, but MVC is a good way to proceed nowadays.

Take a look here, and work through the tutorial here. By the end of a couple of hours you will get the hang of it.

If you don't want to use MVC please post back here and let us know more clearly the technologies you want to use and someone will help you.


  1. You take the values of those text fields.
  2. Then you write an INSERT statement like INSERT INTO TABLE1 (COLUMN1,COLUMN2,COLUMN3...) VALUES(textBox1.Text.....)

if you want to know how to implement the entire database thing then here you go:

 Using System.Data.SqlClient;
 ......

   SqlConnection con=new SqlConnection("Data Source=.";Initial   
   Catalog=Northwind;userID=sa;password=123456);
   SqlCommand cmd=new SqlCommand();
   cmd.connection=con;
   cmd.commandText="INSERT INTO TABLE1 (C1,C2,C3) VALUES (:V1,:V2,V3)";
   cmd.Parameters.Add("V1",SqlDBType.Nvarchar).value=textBox1.Text;
   /*and so on with the next two parameters. If your parameter's type is different
   you can change NVarchar to other SQL datatypes available from the drop down list
   and off course you'll have to convert the textBox's value to the appropriate 
   datatype.*/
   cmd.ExecuteNonQuery();

This is basically most of what you need.


using System.Data.SqlClient;

SqlConnection con=new SqlConnection("Data Source=.....");

SqlCommand cmd = new SqlCommand("INSERT INTO TableName (StudentId,StudentName,StudentCity) VALUES(@StudentId,@StudentName,@StudentCity)", con);

cmd.Parameters.AddWithValue("@StudentId", TextBox1.Text);

cmd.Parameters.AddWithValue("@StudentName", TextBox2.Text);

cmd.Parameters.AddWithValue("@StudentCity", TextBox3.Text);

cmd.ExecuteNonQuery();

con.Close();

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜