yos*_*ssi 16 forms sql-server asp.net insert
嗨我在PHP中我会做一个动作的表单,让我们说一个process.php页面,在那个页面我将采取post值,并使用mysql_query将进行插入.现在我迷路了,我正在尝试使用visual studio 2010在ASP.net中使用sql server 2008创建一个来自并做一个插件.
我在App_Data文件夹中定义了一个sql db.基本上我需要的东西(除非有更好的方法)是:
谢谢.
cly*_*lyc 16
有关如何执行此操作的大量示例代码在线.
以下是如何执行此操作的一个示例:http: //geekswithblogs.net/dotNETvinz/archive/2009/04/30/creating-a-simple-registration-form-in-asp.net.aspx
您定义以下标记之间的文本框:
<form id="form1" runat="server">
Run Code Online (Sandbox Code Playgroud)
你创建你的文本框并将它们定义为runat ="server",如下所示:
<asp:TextBox ID="TxtName" runat="server"></asp:TextBox>
Run Code Online (Sandbox Code Playgroud)
定义一个按钮来处理你的逻辑(注意onclick):
<asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" />
Run Code Online (Sandbox Code Playgroud)
在后面的代码中,如果用户通过定义名为的方法单击按钮,则定义服务器要执行的操作
protected void Button1_Click(object sender, EventArgs e)
Run Code Online (Sandbox Code Playgroud)
或者你可以双击设计视图中的按钮.
这是一个非常快速的代码示例,可以在按钮单击事件(代码隐藏)中插入到表中
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
}
}
}
Run Code Online (Sandbox Code Playgroud)