asp.net c# 删除数据

来源:百度知道 编辑:UC知道 时间:2024/06/01 22:52:17
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class manage : System.Web.UI.Page
{
/// <summary>
/// 获取链接字符串
/// </summary>
private string myConnectionString = ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString;

protected void Page_Load(object sender, EventArgs e)
{

///创建SqlConnection
SqlConnection myConnection = new SqlConnection(myConnectionString);

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

///打开连接
my

你这里有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()+"'";

///删除选择的用户
S