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

时间:2023-03-09 01:35:23
.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里面的代码:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
string sql="Insert into student values('"+TextBox1.Text+"','"+TextBox2.Text+"')";
Class1 CS=new Class1();
int num=CS.Execute(sql);
if(num>)
Response.Write("<script>alert('提交成功')</script>");
else
Response.Write("<script>alert('提交失败')</script>");
}
}
   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 = ; i < read.FieldCount; i++)
{
TextBox3.Text += read[i].ToString()+"\r\n";
} } }