新建空web项目

.net简单web开发

新建首页

.net简单web开发

.net简单web开发

index.asps 添加代码 

    <div>

      <asp:TextBox ID="txt_test" runat="server"></asp:TextBox>
      <asp:Button ID="btn_test" runat="server" Onclick="btn_test_Click" Text="保存"/>
    </div>
    <asp:Repeater ID="rpt_test" runat="server">
        <ItemTemplate>
             <div>
                 <%#Eval("DeptName")%>
             </div>
        </ItemTemplate>
    </asp:Repeater>


index.aspx.cs 代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;


namespace WebApplication2
{
    public partial class index : System.Web.UI.Page
    {
        static string strConn = ConfigurationManager.ConnectionStrings["myconn"].ToString();
        SqlConnection conn = new SqlConnection(strConn);
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadInfo();
        }


        private void LoadInfo()
        {
            ConnectDB();
            string strSql = @"select DeptName from  E_Dept";
            DataTable dt = ExecuteQuery(strSql);
            rpt_test.DataSource = dt;
            rpt_test.DataBind();
            CloseDB();
        }


        protected void btn_test_Click(object sender,EventArgs e)
        {
            ConnectDB();
            string strSql = @"insert into E_Dept (DeptName) values ('" + txt_test.Text + "')";
            ExecuteNonQuery(strSql);
            LoadInfo();
            CloseDB();
        }


        public void ConnectDB()
        {
            try
            {
                conn = new SqlConnection(strConn);
                conn.Open();
            }
            catch (Exception ex)
            {


            }
        }


        public void CloseDB()
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
        }


        public DataTable ExecuteQuery(string sqlString)
        {
            try
            {
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(sqlString, conn);//从数据库中查询
                da.Fill(ds);//将数据填充到DataSet
                return ds.Tables[0];
            }
            catch (Exception ex)
            {
                return null;
            }


        }


        public int ExecuteNonQuery(string sqlString)
        {
            try
            {
                SqlCommand cmd = new SqlCommand(sqlString, conn);
                return cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                return -1;
            }

        }

    }
}

数据库创建表E_Dept

Web.config 配置数据库连接

<connectionStrings>
    <add name="myconn" connectionString="server=服务器名;database=数据库名;uid=用户名;password=密码" providerName="System.Data.SqlClient" />

  </connectionStrings>

运行

.net简单web开发

相关文章:

  • 2021-06-08
  • 2021-04-12
  • 2021-10-29
  • 2022-12-23
  • 2021-08-15
猜你喜欢
  • 2022-12-23
  • 2021-05-19
  • 2022-12-23
  • 2021-12-02
  • 2021-07-18
  • 2022-02-25
相关资源
相似解决方案