asp.net中向mssqlserver中添加单条数据

作者:袖梨 2022-06-25

htm页面

 代码如下 复制代码

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>



    添加单条数据实例


   


   

       

           

           

           
               
                   
               
               
                   
                   
                   
               
               
                   
                   
                   
                   
               
               
                   
                   
                   
                   
               
               
                   
                   
                   
                   
               
               
                   
                   
                   
               
               
                   
                   
                   
               
               
                   
               
               
                   
               
           

                       

                   

                       

                   

                   

                       

                       

                   

                   

                       

                       

                   

                   

                       

                       

                   

                   

                       
                       

                   

                   

                       

                   

                                                    BorderStyle="None" BorderWidth="1px" CellPadding="4" Font-Size="Smaller" ForeColor="Black"
                            GridLines="Vertical" Width="720px" AllowPaging="True" AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="3">
                           
                           
                           
                           
                           
                           
                           
                               
                               
                               
                           

                       

                   

                       

       

   
   

   


index.cs文件

 代码如下 复制代码

using System;
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;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.bindGridview();//调用自定义方法绑定控件数据
        }
    }
    public void bindGridview()
    {
        SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]);
        con.Open();//打开数据库连接
        SqlDataAdapter ada = new SqlDataAdapter("Select * From tb_Send05", con);//创建数据适配器
        DataSet ds = new DataSet();//创建数据集
        ada.Fill(ds);//填充数据集
        GridView1.DataSource = ds;//绑定数据
        GridView1.DataBind();
        con.Close();
    }
    protected void ImgBtnSend_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]);
            con.Open();
            string InsertSql = "Insert Into tb_send05 values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')";
            SqlCommand com = new SqlCommand(InsertSql, con);//创建执行SQL命令的SqlCommand对象
            com.ExecuteNonQuery();//执行添加操作
            SqlDataAdapter ada = new SqlDataAdapter("Select * From tb_Send05", con);//创建适配器
            DataSet ds = new DataSet();
            ada.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
            con.Close();
            Response.Write("");
        }
        catch
        {
            Response.Write("");
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        this.bindGridview();
    }
}

相关文章

精彩推荐