[英]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. 我有三个空字段,需要客户填写。当他们填写这些框并单击“提交”时,我希望将内容存储在SQL Server数据库中。 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.
某种程度上取决于您要执行的操作,但是MVC是当今进行下去的好方法。
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. 如果您不想使用MVC,请发回此处,让我们更清楚地了解您要使用的技术,并且有人会为您提供帮助。
using System.Data.SqlClient; 使用System.Data.SqlClient;
SqlConnection con=new SqlConnection("Data Source=....."); SqlConnection con =新的SqlConnection(“ Data Source = .....”);
SqlCommand cmd = new SqlCommand("INSERT INTO TableName (StudentId,StudentName,StudentCity) VALUES(@StudentId,@StudentName,@StudentCity)", con); SqlCommand cmd =新的SqlCommand(“插入表名(StudentId,StudentName,StudentCity)VALUES(@ StudentId,@ StudentName,@ StudentCity)”,con);
cmd.Parameters.AddWithValue("@StudentId", TextBox1.Text); cmd.Parameters.AddWithValue(“ @ StudentId”,TextBox1.Text);
cmd.Parameters.AddWithValue("@StudentName", TextBox2.Text); cmd.Parameters.AddWithValue(“ @ StudentName”,TextBox2.Text);
cmd.Parameters.AddWithValue("@StudentCity", TextBox3.Text); cmd.Parameters.AddWithValue(“ @ StudentCity”,TextBox3.Text);
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
con.Close(); con.Close();
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. 这基本上就是您所需要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.