
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace 二班
{
public partial class 增刪改查 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string XH = TextBox1.Text;
string XM = TextBox2.Text;
string XB = RadioButtonList1.SelectedItem.Text;
string ZY = DropDownList1.SelectedItem.Text;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=DESKTOP-NIPVNUT;database=11111;Trusted_Connection=SSPI";
conn.Open();
string sql = "INSERT INTO TB_STU ( XH , XM , XB , ZY ) " + "VALUES ( '" + XH + "', '" + XM + "' , '" + XB + "' , '" + ZY + "')";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
Label1.Text = "資料添加成功!";
conn.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
string XH = TextBox1.Text;
string XM = TextBox2.Text;
string XB = RadioButtonList1.SelectedItem.Text;
string ZY = DropDownList1.SelectedItem.Text;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=DESKTOP-NIPVNUT;database=11111;Trusted_Connection=SSPI";
conn.Open();
string sql2 = "update TB_STU set XM='" + XM + "'where XH = '" + XH + "'";
string sql3 = "update TB_STU set XB='" + XB + "' where XH = '" + XH + "'";
string sql4 = "update TB_STU set ZY='" + ZY + "' where XH = '" + XH + "'";
SqlCommand cmd2 = new SqlCommand(sql2, conn);
cmd2.ExecuteNonQuery();
SqlCommand cmd3 = new SqlCommand(sql3, conn);
cmd3.ExecuteNonQuery();
SqlCommand cmd4 = new SqlCommand(sql4, conn);
cmd4.ExecuteNonQuery();
Label1.Text = "資料修改成功!";
conn.Close();
}
protected void Button3_Click(object sender, EventArgs e)
{
string id = Request.QueryString["id"].ToString();
Label1.Text = "學號為" + id + "學生資訊已洗掉";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=DESKTOP-NIPVNUT;database=11111;Trusted_Connection=SSPI";
conn.Open();
string sql = "Delete FROM TB_STU Where XH = '" + id + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
conn.Close();
}
protected void Button4_Click(object sender, EventArgs e)
{
string XH = TextBox1.Text;
string XM = TextBox2.Text;
string XB = RadioButtonList1.SelectedItem.Text;
string ZY = DropDownList1.SelectedItem.Text;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=DESKTOP-NIPVNUT;database=11111;Trusted_Connection=SSPI";
conn.Open();
string sql = "INSERT INTO TB_STU ( XH , XM , XB , ZY) " + "VALUES ( '" + XH + "', '" + XM + "' , '" + XB + "' , '" + ZY + "')";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
Label1.Text = "資料查找成功!";
conn.Close();
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
uj5u.com熱心網友回復:
你的資料庫名是11111,沒填錯嗎?如果資料庫真的支持也建議庫名首字母用英文字母。這個資料庫,庫里有TB_STU這個表嗎?
uj5u.com熱心網友回復:
資料庫名沒錯 但是沒有那個表
uj5u.com熱心網友回復:
那先建個表再試試吧uj5u.com熱心網友回復:
建完表了 可以了 但是只能執行添加成功 別的按鈕點了也全都是添加成功 我該咋辦呢 CS檔案的代碼再上邊
uj5u.com熱心網友回復:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace 二班
{
public partial class 增刪改查 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string 學號 = TextBox1.Text;
string 姓名 = TextBox2.Text;
string 性別 = RadioButtonList1.SelectedItem.Text;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=DESKTOP-NIPVNUT;database=11111;Trusted_Connection=SSPI";
conn.Open();
string sql = "INSERT INTO TB_STU ( 學號 , 姓名 , 性別 ) " + "VALUES ( '" + 學號 + "', '" + 姓名 + "' , '" + 性別 + "')";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
Label1.Text = "資料添加成功!";
conn.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
string 學號 = TextBox1.Text;
string 姓名 = TextBox2.Text;
string 性別 = RadioButtonList1.SelectedItem.Text;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=DESKTOP-NIPVNUT;database=11111;Trusted_Connection=SSPI";
conn.Open();
string sql2 = "update TB_STU set 姓名='" + 姓名 + "'where 學號 = '" + 學號 + "'";
string sql3 = "update TB_STU set 性別='" + 性別 + "' where 學號 = '" + 學號 + "'";
string sql4 = "update TB_STU set 姓名='" + 姓名 + "' where 性別 = '" + 性別 + "'";
SqlCommand cmd2 = new SqlCommand(sql2, conn);
cmd2.ExecuteNonQuery();
SqlCommand cmd3 = new SqlCommand(sql3, conn);
cmd3.ExecuteNonQuery();
SqlCommand cmd4 = new SqlCommand(sql4, conn);
cmd4.ExecuteNonQuery();
Label1.Text = "資料修改成功!";
conn.Close();
}
protected void Button3_Click(object sender, EventArgs e)
{
string id = Request.QueryString["id"].ToString();
Label1.Text = "學號為" + id + "學生資訊已洗掉";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=DESKTOP-NIPVNUT;database=11111;Trusted_Connection=SSPI";
conn.Open();
string sql = "Delete FROM TB_STU Where 學號 = '" + id + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
conn.Close();
}
protected void Button4_Click(object sender, EventArgs e)
{
string 學號 = TextBox1.Text;
string 姓名 = TextBox2.Text;
string 性別 = RadioButtonList1.SelectedItem.Text;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=DESKTOP-NIPVNUT;database=11111;Trusted_Connection=SSPI";
conn.Open();
string sql = "INSERT INTO TB_STU ( 學號 , 姓名 , 性別 , 專業) " + "VALUES ( '" + 學號 + "', '" + 姓名 + "' , '" + 性別 + "')";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
Label1.Text = "資料查找成功!";
conn.Close();
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
uj5u.com熱心網友回復:
你的其他按鈕是通過復制粘貼生成的吧,應該是按鈕上的事件系結的不對,檢查下事件系結的地方吧,找不到的話再截下圖發出來uj5u.com熱心網友回復:


有點懵 無從下手
uj5u.com熱心網友回復:
點開“登錄功能頁面.aspx.designer.cs”的源代碼看看uj5u.com熱心網友回復:
//------------------------------------------------------------------------------
// <自動生成>
// 此代碼由工具生成。
//
// 對此檔案的更改可能會導致不正確的行為,并且如果
// 重新生成代碼,這些更改將會丟失。
// </自動生成>
//------------------------------------------------------------------------------
namespace 二班 {
public partial class 增刪改查 {
/// <summary>
/// Head1 控制元件。
/// </summary>
/// <remarks>
/// 自動生成的欄位。
/// 若要進行修改,請將欄位宣告從設計器檔案移到代碼隱藏檔案。
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlHead Head1;
/// <summary>
/// form1 控制元件。
/// </summary>
/// <remarks>
/// 自動生成的欄位。
/// 若要進行修改,請將欄位宣告從設計器檔案移到代碼隱藏檔案。
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlForm form1;
/// <summary>
/// TextBox1 控制元件。
/// </summary>
/// <remarks>
/// 自動生成的欄位。
/// 若要進行修改,請將欄位宣告從設計器檔案移到代碼隱藏檔案。
/// </remarks>
protected global::System.Web.UI.WebControls.TextBox TextBox1;
/// <summary>
/// TextBox2 控制元件。
/// </summary>
/// <remarks>
/// 自動生成的欄位。
/// 若要進行修改,請將欄位宣告從設計器檔案移到代碼隱藏檔案。
/// </remarks>
protected global::System.Web.UI.WebControls.TextBox TextBox2;
/// <summary>
/// RadioButtonList1 控制元件。
/// </summary>
/// <remarks>
/// 自動生成的欄位。
/// 若要進行修改,請將欄位宣告從設計器檔案移到代碼隱藏檔案。
/// </remarks>
protected global::System.Web.UI.WebControls.RadioButtonList RadioButtonList1;
/// <summary>
/// Button1 控制元件。
/// </summary>
/// <remarks>
/// 自動生成的欄位。
/// 若要進行修改,請將欄位宣告從設計器檔案移到代碼隱藏檔案。
/// </remarks>
protected global::System.Web.UI.WebControls.Button Button1;
/// <summary>
/// Button2 控制元件。
/// </summary>
/// <remarks>
/// 自動生成的欄位。
/// 若要進行修改,請將欄位宣告從設計器檔案移到代碼隱藏檔案。
/// </remarks>
protected global::System.Web.UI.WebControls.Button Button2;
/// <summary>
/// Button3 控制元件。
/// </summary>
/// <remarks>
/// 自動生成的欄位。
/// 若要進行修改,請將欄位宣告從設計器檔案移到代碼隱藏檔案。
/// </remarks>
protected global::System.Web.UI.WebControls.Button Button3;
/// <summary>
/// Button5 控制元件。
/// </summary>
/// <remarks>
/// 自動生成的欄位。
/// 若要進行修改,請將欄位宣告從設計器檔案移到代碼隱藏檔案。
/// </remarks>
}
}
uj5u.com熱心網友回復:
你剛剛的截圖上有三個Button_Click1uj5u.com熱心網友回復:
前一次的第一個圖uj5u.com熱心網友回復:
OK了 很完美 感謝感謝
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/77843.html
標籤:C#
上一篇:如何實作跨網段掃描IP地址
