.net视频教程代码之《提交注册内容》

看我的视频之后感觉代码太多不好打或者容易打错的话可以来看我的这里的代码。我的视频地址是

https://www.bilibili.com/video/av12727717/

 

类里面的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;

/// <summary>
///Class1 的摘要说明
/// </summary>
public class Class1
{
    
        
        public SqlConnection connect()
        {   
             string rode=@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";

            SqlConnection con = new SqlConnection(rode);
            con.Open();
            return con;
        }
        public SqlCommand command(string sql)
        {
        
            SqlCommand cmd=new SqlCommand(sql,connect());
            return  cmd;
        
        }

        public int Execute(string sql)
        {
           return command(sql).ExecuteNonQuery();    //这个方法对付增删改
        
        }


        public SqlDataReader read(string sql)
        {
            return command(sql).ExecuteReader();        //这个方法对付查询
        }


     
}

 

Default里面的代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 
 8 public partial class _Default : System.Web.UI.Page
 9 {
10     protected void Page_Load(object sender, EventArgs e)
11     {
12 
13     }
14     protected void Button1_Click(object sender, EventArgs e)
15     {
16         string sql="Insert into student values('"+TextBox1.Text+"','"+TextBox2.Text+"')";
17         Class1 CS=new Class1();
18         int num=CS.Execute(sql);
19         if(num>0)
20             Response.Write("<script>alert('提交成功')</script>");
21         else
22             Response.Write("<script>alert('提交失败')</script>");
23     }
24 }

 

   protected void Button4_Click(object sender, EventArgs e)
    {
        TextBox3.Text = null;
        string sql = "select * from student";
        Class1 CS = new Class1();
        // num = CS.Execute(sql);

        SqlDataReader read=CS.read(sql);
        while (read.Read())
        {
            for (int i = 0; i < read.FieldCount; i++)
            {
                TextBox3.Text += read[i].ToString()+"\r\n";               
            }

        }      
       
    }

 

posted @ 2016-01-03 16:48  蜀云泉  阅读(313)  评论(0编辑  收藏  举报