你好
在PHP我会做一个表单与一个动作,让我们说一个process.PHP页面,在该页面中,我将采取的职位值,并使用一个 mysql_query将做一个插入.
现在我迷路了,我正在使用visual studio 2010,在ASP.net中使用sql server 2008创建一个插入.
在PHP我会做一个表单与一个动作,让我们说一个process.PHP页面,在该页面中,我将采取的职位值,并使用一个 mysql_query将做一个插入.
现在我迷路了,我正在使用visual studio 2010,在ASP.net中使用sql server 2008创建一个插入.
我已经在App_Data文件夹中定义了一个sql数据库.
基本上我需要(除非有更好的方法)是:
>我如何获得帖子值.
>如何将它们插入数据库.
谢谢.
解决方法
在线有大量的示例代码,关于如何做到这一点.
这里只是一个例子:
http://geekswithblogs.net/dotNETvinz/archive/2009/04/30/creating-a-simple-registration-form-in-asp.net.aspx
<form id="form1" runat="server">
您创建您的文本框并将其定义为runat =“server”,如下所示:
<asp:TextBox ID="TxtName" runat="server"></asp:TextBox>
定义一个按钮来处理你的逻辑,这样(注意onclick):
<asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" />
在后面的代码中,如果用户通过定义一个名为的方法单击按钮,就可以定义您希望服务器做什么
protected void Button1_Click(object sender,EventArgs e)
或者您可以双击设计视图中的按钮.
这是一个非常快速的示例代码插入表中的按钮单击事件(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 } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。