VS连接增删改查求助

POSSESSIVENESS. 2020-01-11 10:07:53

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)
{

}


}
}
...全文
194 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
冰风漫天 2020-01-12
  • 打赏
  • 举报
回复
前一次的第一个图
冰风漫天 2020-01-12
  • 打赏
  • 举报
回复
你刚刚的截图上有三个Button_Click1
POSSESSIVENESS. 2020-01-12
  • 打赏
  • 举报
回复
引用 8 楼 冰风漫天 的回复:
点开“登录功能页面.aspx.designer.cs”的源代码看看
//------------------------------------------------------------------------------ // <自动生成> // 此代码由工具生成。 // // 对此文件的更改可能会导致不正确的行为,并且如果 // 重新生成代码,这些更改将会丢失。 // </自动生成> //------------------------------------------------------------------------------ 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> } }
冰风漫天 2020-01-12
  • 打赏
  • 举报
回复
点开“登录功能页面.aspx.designer.cs”的源代码看看
POSSESSIVENESS. 2020-01-12
  • 打赏
  • 举报
回复
引用 6 楼 冰风漫天 的回复:
你的其他按钮是通过复制粘贴生成的吧,应该是按钮上的事件绑定的不对,检查下事件绑定的地方吧,找不到的话再截下图发出来
有点懵 无从下手
冰风漫天 2020-01-12
  • 打赏
  • 举报
回复
你的其他按钮是通过复制粘贴生成的吧,应该是按钮上的事件绑定的不对,检查下事件绑定的地方吧,找不到的话再截下图发出来
POSSESSIVENESS. 2020-01-12
  • 打赏
  • 举报
回复
引用 3 楼 冰风漫天 的回复:
那先建个表再试试吧
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) { } } }
POSSESSIVENESS. 2020-01-12
  • 打赏
  • 举报
回复
引用 3 楼 冰风漫天 的回复:
那先建个表再试试吧
建完表了 可以了 但是只能执行添加成功 别的按钮点了也全都是添加成功 我该咋办呢 CS文件的代码再上边
冰风漫天 2020-01-12
  • 打赏
  • 举报
回复
那先建个表再试试吧
POSSESSIVENESS. 2020-01-12
  • 打赏
  • 举报
回复
引用 1 楼 冰风漫天的回复:
你的数据库名是11111,没填错吗?如果数据库真的支持也建议库名首字母用英文字母。 这个数据库,库里有TB_STU这个表吗?
数据库名没错 但是没有那个表
冰风漫天 2020-01-12
  • 打赏
  • 举报
回复
你的数据库名是11111,没填错吗?如果数据库真的支持也建议库名首字母用英文字母。 这个数据库,库里有TB_STU这个表吗?
POSSESSIVENESS. 2020-01-12
  • 打赏
  • 举报
回复
引用 10 楼 冰风漫天的回复:
你刚刚的截图上有三个Button_Click1
OK了 很完美 感谢感谢

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧