datagrid 删除了整个数据
<asp:ButtonColumn Text="删除" HeaderText="删除" CommandName="Delete"><HeaderStyle Font-Size="Smaller" Font-Names="Arial" Font-Bold="True" HorizontalAlign="Justify"
Width="35px" BackColor="DarkGray"> </HeaderStyle>
<ItemStyle HorizontalAlign="Justify"> </ItemStyle>
</asp:ButtonColumn>
.cs中事件:
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int index = e.Item.ItemIndex;
string strID=this.DataGrid1.Items[index].Cells[0].Text;
int ID=Convert.ToInt16(strID);
String sql = "delete from shipin where id=ID";
Db.execSql(sql);
sql ="select * from shipin ";
DataSet ds=Db.selectDS(sql);
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
}
经测试index ,strID,ID都正确
就是点击删除时,整个表都删除了
数据库连接操作类 Db.cs内容如下
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace zgcydd
{
/// <summary>
/// Db 的摘要说明。
/// </summary>
public class Db
{
public Db()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private static SqlConnection conn;
public static DataSet selectDS(string sql){
conn=new SqlConnection(ConfigurationSettings.AppSettings["connString"]);
conn.Open();
SqlDataAdapter sda=new SqlDataAdapter(sql,conn);
DataSet ds=new DataSet();
try
{
sda.Fill(ds);
}
catch(SqlException e)
{
throw new Exception(e.Message);
}
return ds;
}
public static SqlDataReader selectDR(string sql){
conn=new SqlConnection(ConfigurationSettings.AppSettings["connString"]);
SqlDataReader reader;
SqlCommand cmd=null;
try
{
conn.Open();
cmd=conn.CreateCommand();
cmd.CommandText=sql;
reader=cmd.ExecuteReader();
}
catch(SqlException e)
{
throw new Exception(e.Message);
}
finally
{
cmd.Dispose();
}
return reader;
}
public static int execSql(string sql){
int rows=-1;//受到影响的行数
conn=new SqlConnection(ConfigurationSettings.AppSettings["connString"]);
SqlCommand cmd=null;
try
{
conn.Open();
cmd=conn.CreateCommand();
cmd.CommandText=sql;
rows=cmd.ExecuteNonQuery();
}
catch(SqlException e)
{
throw new Exception(e.Message);
}
finally
{
cmd.Dispose();
conn.Close();
}
return rows;
}
public static void close(){
conn.Close();
}
}
}
谢谢了!
--------------------编程问答-------------------- String sql = "delete from shipin where id=ID";
改成
String sql = "delete from shipin where id=" + ID.ToString();
试试 --------------------编程问答-------------------- "delete from shipin where id=ID"; 肯定有问题的
至少应该写为:
"delete from shipin where id= "+ID; --------------------编程问答-------------------- 真的谢谢了。
行的。
发现自己太笨了,这个都没看出来的
补充:.NET技术 , C#