第一篇:asp.net 刪除數據 學習資料
刪除數據
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection conn = new
SqlConnection(ConfigurationManager.ConnectionStrings[“newsConnectionString”].ConnectionString);SqlCommand cmd = new SqlCommand();
cmd.CommandText=“delete from login where 編號='”+this.TextBox1.Text+“'”;cmd.Connection=conn;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Response.Write(“”);Response.Redirect(“Default.aspx”);
}
第二篇:asp.net 更新數據 學習資料
更新數據
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings[“newsConnectionString”].ConnectionString);SqlCommand cmd = new SqlCommand();
cmd.CommandText=“update login set 用戶名='”+this.TextBox2.Text+“',密碼
='”+this.TextBox3.Text+“',年齡='”+this.TextBox4.Text+“',性別='”+this.TextBox5.Text+“',備注='”+this.TextBox6.Text+“'where 編號=”+this.TextBox1.Text+“";
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
Response.Redirect(”Default.aspx");
con.Close();
}
第三篇:asp.Net 登陸功能模塊 學習資料
前臺代碼
<%@ Page Language=“C#” AutoEventWireup=“true” CodeFile=“login.aspx.cs” Inherits=“login” %>
“http://1/DTD/xhtml1-transitional.dtd”>
Web.config
登陸按鈕后臺代碼
protected void Button1_Click(object sender, EventArgs e)
{
//創建連接對象
SqlConnection conn = new
SqlConnection(ConfigurationManager.ConnectionStrings[“connection”].ConnectionString);//創建查詢,用戶名是否存在數據對象
SqlCommand cmd = new SqlCommand(“select * from login where 用戶名='” + TextBox1.Text + “'”, conn);
try
{
//如果存在則打開數據庫
conn.Open();
SqlDataReader sdr = cmd.ExecuteReader();
//如果用戶名輸入正確
if(sdr.Read())
{
//判斷密碼是否正確
if(sdr[“密碼”].ToString()==TextBox2.Text)
{
//如果用戶名和密碼都正確就關閉數據庫連接
conn.Close();
//將用戶名存放到session中
Session[“用戶名”] = TextBox1.Text.Trim();
//并進入后臺頁面
Response.Redirect(“admin_index.aspx”);
}
else//否則
{
//彈出對話框,提示密碼錯誤
Response.Write(“”);
}
}
else//否則
{
//彈出對話框提示用戶名錯誤
Response.Write(“”);
}
}
catch(System.Exception ee)//異常處理
{
Response.Write(“”);}
finally
{
conn.Close();//關閉數據庫
}
}
第四篇:asp.net 添加新聞功能模塊 學習資料
前臺代碼
<%@ Page Language=“C#” AutoEventWireup=“true”CodeFile=“Default.aspx.cs” Inherits=“_Default” %>
“http://1/DTD/xhtml1-transitional.dtd”>
Web.config
提交新聞后臺代碼
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new
SqlConnection(ConfigurationManager.ConnectionStrings[“connection”].ConnectionString);
SqlCommand insertcmd = new SqlCommand(“insert into news(新聞標題,新聞內容,作者)values(@新聞標題,@新聞內容,@作者)”, conn);
//為每個數據庫字段設置參數
insertcmd.Parameters.Add(“@新聞標題”, SqlDbType.VarChar, 200);
insertcmd.Parameters.Add(“@新聞內容”, SqlDbType.VarChar);
insertcmd.Parameters.Add(“@作者”, SqlDbType.VarChar, 20);
//為每個參數賦值
insertcmd.Parameters[“@新聞標題”].Value = TextBox1.Text;
insertcmd.Parameters[“@新聞內容”].Value = TextBox2.Text;
insertcmd.Parameters[“@作者”].Value = TextBox3.Text;
try
{
conn.Open();//打開數據庫
int flag = insertcmd.ExecuteNonQuery();//執行添加
if(flag > 0)//如果添加成功
{
Response.Write(“”);
this.TextBox1.Text = “";
this.TextBox2.Text = ”“;
this.TextBox3.Text = ”“;
}
else// 如果添加失敗
{
Response.Write(”“);
this.TextBox1.Text = ”“;
this.TextBox2.Text = ”“;
this.TextBox3.Text = ”“;
}
}
catch(System.Exception ee)//錯誤處理
{
Response.Write(”");
}
finally
{
conn.Close();//關閉數據庫連接
}
}
第五篇:asp.net 檢查用戶名是否存在 學習資料
檢查用戶名是否存在功能模塊
protected void Button1_Click(object sender, EventArgs e)
{
usernamevalidate();
}
private int usernamevalidate()
{
SqlConnection conn = new
SqlConnection(ConfigurationManager.ConnectionStrings[“connection”].ConnectionString);SqlCommand selectcmd = new SqlCommand(“select * from login where 用戶名='” + TextBox1.Text.Trim()+ “'”, conn);
int i = 0;
try
{
conn.Open();
SqlDataReader sdr = selectcmd.ExecuteReader();
if(sdr.Read())
{
i = 1;
Label1.Text = “此用戶已存在,請輸入其他用戶名!”;
}
else
{
Label1.Text = “此用戶可使用!”;
}
}
catch(System.Exception ee)
{
Response.Write(“”);
}
finally
{
conn.Close();
}
return i;
}