ASP.net使用表单将数据插入到一个SQL Server表 [英] ASP.net using a form to insert data into an sql server table

查看:126
本文介绍了ASP.net使用表单将数据插入到一个SQL Server表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


在PHP我会做一个表格来让说process.php页面,并在页面我想借此岗位价值和使用的mysql_query会做插入的动作。
现在我失去了,我想创建一个由和使用Visual Studio 2010做一个inserstion在ASP.net与SQL Server 2008。

Hi in php i would do a form with an action to lets say a process.php page and in that page i would take the post values and using a mysql_query would do an insertion. now i am lost ,i am trying to create a from and do an inserstion in ASP.net with sql server 2008 using visual studio 2010.

我已经定义在App_Data文件夹中的SQL数据库。
basicly我需要什么(除非有一个更好的方法)是:

i have defined a sql db in the App_Data folder. basicly what i need (unless there is a better way) is:


  1. 如何得到这个职位的值。

  2. 如何将它们插入到数据库中。

感谢。

推荐答案

有吨样品code网上至于如何做到这一点。

There are tons of sample code online as to how to do this.

下面是如何做到这一点只是一个例子:
<一href=\"http://geekswithblogs.net/dotNETvinz/archive/2009/04/30/creating-a-simple-registration-form-in-asp.net.aspx\" rel=\"nofollow\">http://geekswithblogs.net/dotNETvinz/archive/2009/04/30/creating-a-simple-registration-form-in-asp.net.aspx

Here is just one example of how to do this: http://geekswithblogs.net/dotNETvinz/archive/2009/04/30/creating-a-simple-registration-form-in-asp.net.aspx

定义了以下标签之间的文本框:

you define the text boxes between the following tag:

<form id="form1" runat="server"> 

您创建文本框,并定义它们=服务器,像这样:

you create your textboxes and define them to runat="server" like so:

<asp:TextBox ID="TxtName" runat="server"></asp:TextBox>

定义一个按钮来处理你的逻辑,像这样(注意的onclick

<asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" />

在code的后面,可以定义你想要的服务器做的,如果用户定义了一个名为方法点击按钮

in the code behind, you define what you want the server to do if the user clicks on the button by defining a method named

protected void Button1_Click(object sender, EventArgs e)

或者你可以只需双击在设计视图中的按钮。

or you could just double click the button in the design view.

下面是code的一个非常快速的样品插入在按钮单击事件表(codebehind)

Here is a very quick sample of code to insert into a table in the button click event (codebehind)

protected void Button1_Click(object sender, EventArgs e)
{
   string name = TxtName.Text; // Scrub user data

   string connString = ConfigurationManager.ConnectionStrings["yourconnstringInWebConfig"].ConnectionString;
   SqlConnection conn = null;
   try
   {
          conn = new SqlConnection(connString);
          conn.Open();

          using(SqlCommand cmd = new SqlCommand())
          {
                 cmd.Conn = conn;
                 cmd.CommandType = CommandType.Text;
                 cmd.CommandText = "INSERT INTO dummyTable(name) Values (@var)";
                 cmd.Parameters.AddWithValue("@var", name);
                 int rowsAffected = cmd.ExecuteNonQuery();
                 if(rowsAffected ==1)
                 {
                        //Success notification
                 }
                 else
                 {
                        //Error notification
                 }
          }
   }
   catch(Exception ex)
   {
          //log error 
          //display friendly error to user
   }
   finally
   {
          if(conn!=null)
          {
                 //cleanup connection i.e close 
          }
   }
}

这篇关于ASP.net使用表单将数据插入到一个SQL Server表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆