asp.net c# 删除数据

2024-12-15 08:47:24
推荐回答(4个)
回答1:

你这里有2点错误
1,首先先解决提示错误
报此错误的原因是在PageLoad事件里面没加if(!IsPostBack)
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
///创建SqlConnection
SqlConnection myConnection = new SqlConnection(myConnectionString);

string cmdText = "SELECT * FROM ss";
SqlCommand myCommand = new SqlCommand(cmdText, myConnection);

///打开连接
myConnection.Open();

GridView1.DataSource = myCommand.ExecuteReader();
GridView1.DataBind();

///关闭数据库的链接
myConnection.Close();
}

}
2.删除用户时,SQL语句有误
string cmdText = "SELECT * FROM ss where id="+"'"+ID.ToString()+"'";
改成
string cmdText = "DELETE FROM ss where id="+"'"+ID.ToString()+"'";

回答2:

///删除选择的用户
SqlConnection myConnection = new SqlConnection(myConnectionString);
string cmdText = "SELECT * FROM ss where id="+"'"+ID.ToString()+"'";
SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
GridView1.DataBind();

break;
}
你删除数据部分用的是selsct啊,怎么可能删除

回答3:

aspx代码第一行加上或修改成EnableEventValidation="false"
<%@ Page EnableEventValidation="false" %>

回答4:

case "delete":
{
///删除选择的用户
SqlConnection myConnection = new SqlConnection(myConnectionString);
string cmdText = "SELECT * FROM ss where id="+"'"+ID.ToString()+"'";
SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
GridView1.DataBind();

break;
}

///

string cmdText = "SELECT * FROM ss where id="+"'"+ID.ToString()+"'";
改成

string cmdText = "delete FROM ss where id="+"'"+ID.ToString()+"'";