• ADO1


    1. 新建Web窗体

      客户端控件:html控件

      服务器控件:用的比较少

      

    2. 数据库连接

       

            protected void btnLogin_Click(object sender, EventArgs e)
            {
                try //这里做检测,是因为如果没有检测,连接错误后会把数据库里的内容显示在出错网页中
                {
                    string username = txtUserName.Text.Trim(); //删除空白字符
                    string pwd = txtPwd.Text.Trim();
                    if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(pwd))
                    {
                        //Response.Write("用户名或者密码不能为空");
                        Response.Write("<script>alert('用户名或者密码不能为空');</script>");
                    }
                    else
                    {
                        //写Sql语句
                        string sSql = string.Format("select UserID from TestDB.dbo.UserInfor Where UserName='{0}' and Pwd='{1}'", username, pwd);
    
                        //数据库连接语句
                        //DataBase:要连接的数据库名称;  Server:服务器名称,'.'代表本机服务器,如果服务器在其他地方,这里输入IP地址
                        //Integrated Security: ture代表windows身份验证,登录名和密码就不需要了;false代表SQL Server身份验证
                        //Uid用户名;Password密码
                        //string connStr = "Database=TestDB;Server=.;Integrated Security=false;Uid=sa;Password=123;";
    
                        //读取配置文件中的数据库连接语句
                        string connStr = ConfigurationManager.ConnectionStrings["TestDB"].ToString();
    
                        //普通写法,需要自己释放资源
                        {
                            //和数据库建立连接,并打开连接
                            SqlConnection con = new SqlConnection(connStr);
                            con.Open();
    
                            //执行语句,要传两个参数,一个是SQL语句,一个是数据库连接类
                            SqlCommand cmd = new SqlCommand(sSql, con);
    
                            //把查询结果赋值给SqlDataReader对象
                            SqlDataReader read = cmd.ExecuteReader();
    
                            //查看是否有记录
                            if (read.HasRows)
                            {
                                Response.Redirect("UserInforM.aspx");
                            }
                            else
                            {
                                Response.Write("用户名或者密码错误");
                            }
    
                            //释放资源,并关闭
                            read.Dispose();
                            read.Close();
                            con.Dispose();
                            con.Close();
                        }
    
                        //用Using可以自动释放资源
                        {
                            using (SqlConnection con = new SqlConnection(connStr)) //自动释放con
                            {
                                con.Open();
                                SqlCommand cmd = new SqlCommand(sSql, con);
                                using (SqlDataReader read = cmd.ExecuteReader())  //自动释放read
                                {
                                    if (read.HasRows)
                                    {
                                        Response.Write("登录成功");
                                    }
                                    else
                                    {
                                        Response.Write("用户名或者密码错误");
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Response.Write("网站正在维修中");
                }
            }

    配置文件:

    <connectionStrings>
        <add  name="TestDB" connectionString="Database=TestDB;Server=.;Integrated Security=false;Uid=sa;Password=123;" providerName="System.Data.SqlClient"/>
      </connectionStrings>

    3. 绑定服务端控件显示数据库内容

     public partial class UserInforM : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                try
                {
                    string sSql = string.Format("select UserID,UserName,RealName,ClassName,Major from TestDB.dbo.UserInfor");
                    string connStr = ConfigurationManager.ConnectionStrings["TestDB"].ToString();
    
                    using (SqlConnection con = new SqlConnection(connStr)) //自动释放con
                    {
                        con.Open();
                        SqlCommand cmd = new SqlCommand(sSql, con);
                        using (SqlDataReader read = cmd.ExecuteReader())  //自动释放read
                        {
                            GridView1.DataSource = read;
                            GridView1.DataBind();
                        }
                    }
                }
                catch (Exception ex)
                {
    
                    Response.Write("错误");
                }
            }
        }

  • 相关阅读:
    js日历,使用datepicker.js,ui.core.js,jquery-1.7.1.js
    android设置按钮按下的不同效果图
    a标签阻止跳转的方法
    获取浏览器中选中部分,比如文本等
    Android Studio does not point to a valid jvm
    Drawerlayout
    webviewactivity
    ViewFilpper
    Physik
    Betriebssystem
  • 原文地址:https://www.cnblogs.com/xiao9426926/p/6346756.html
Copyright © 2020-2023  润新知