如何在SQL Server中插入值 [英] How to insert values in sql server
问题描述
海,
我想将值插入Web应用程序中的sql server数据库中.例如,在设计部分中分别具有五个标签框和五个文本框.插入编码仅在aspx.cs中完成,但是我想如何插入从设计器零件文本框到sqlserver的值.//下面是此示例代码.//
//Default.aspx
Hai,
I want to insert the values in sql server database in the web application.For ex.In the designing part having five label boxes and five text boxes respectively.The insertion coding is done in the aspx.cs only.But I want how to insert values from designer part textboxes to sqlserver.//Below is a sample code for this.//
//Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication11._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 152px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="3" cellspacing="3" class="style1" dir="ltr">
<tr>
<td class="style2">
<asp:Label ID="Label1" runat="server" Text="ID">
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server">
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label2" runat="server" Text="Name">
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server">
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label3" runat="server" Text="Marks">
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server">
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label4" runat="server" Text="School">
</td>
<td>
<asp:TextBox ID="TextBox4" runat="server">
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label5" runat="server" Text="City">
</td>
<td>
<asp:TextBox ID="TextBox5" runat="server">
</td>
</tr>
<tr>
<td class="style2" colspan="2">
<asp:Button ID="Button1" runat="server" Text="SUBMIT"/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Default.aspx.cs
Default.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace WebApplication11
{
public partial class _Default : System.Web.UI.Page
{
string strcon = ("Data Source=MACFEESERVER;Initial Catalog=ePO_LENOVO;User ID=sa;Password=sa");
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
SqlDataAdapter ada;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgrid();
}
}
private void bindgrid()
{
ds.Clear();
con.ConnectionString = strcon;
cmd.Connection = con;
con.Open();
string insert = "insert into student(ID,Name,Marks,School,City)values(3,''Srinivasan'',78,''National matric.school'',''Aranthangi'')";
cmd = new SqlCommand(insert, con);
cmd.ExecuteNonQuery();
con.Close();
}
}
}
在上面的Default.aspx.cs代码中,有代码片段"private void bindgrid"事件.在此代码中,有一行
In the above Default.aspx.cs code there is code snippet "private void bindgrid" event.In this code there is a line
string insert = "insert into student(ID,Name,Marks,School,City)values(3,''Srinivasan'',78,''National matric.school'',''Aranthangi'')";
我的疑问是在运行此应用程序后插入了上面的值.如果我在设计部件中输入diff.values,它将无法插入.请帮助我.
My doubt is after run this application the above value is inserted.If I enter the diff.values in my designing part it can''t insert.Plz help me.
推荐答案
您需要将插入代码放置在提交按钮中,而不是page_load方法中.
You need to place the insert code in the submit button rather than the page_load method.
在您的提交按钮中添加一个onclick
事件,如下所示:
Add aonclick
event to your submit button which looks like this:
<asp:Button ID="Button1" runat="server" onclick="Button1_onclick" Text="SUBMIT"/>
并在后面的代码中
And in code behind as
Protected void Button1_onclick(object sender, EventArgs e)
{
//Place your bindgrid code here
}
这篇关于如何在SQL Server中插入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!