string connectionString =""Data Source=192.168.0.3; User ID=sa; Password=; Initial Catalog=SunYatSite" ;
SqlConn = new  SqlConnection(connectionString);
SqlCommand sqlComm = SqlConn.CreateCommand();
sqlComm.Connection=SqlConn;
sqlComm.CommandText="delete form table where id = '15'";
         sqlComm.ExecuteNonQuery();
SqlConn.Close();

解决方案 »

  1.   

    怎么个按牛删除记录?是datagrid里吗?
      

  2.   

    你这样只能删除id = '15'";的这条记录能否通过一个按钮来控制ID的编号或通过一个按钮来显示
             要删除的编号,谢谢!
      

  3.   

    在BUTTON1_CLICK中加入:dim SqlConnection1 as New SqlConnection("server=localhost;uid=sa;pwd=;database=jianghua")
            SqlConnection1.Open()
            Dim comd1 As SqlCommand
            comd1 = New SqlCommand("insert into zxjb(topic,text,message) values('" & text1.Text & "','" & text2.Text & "','" & text3.Text & "')", SqlConnection1)
            comd1.ExecuteNonQuery()
                 SqlConnection1.Close()
      

  4.   

    在datagrid里只能用一列的删除按钮,我只想用一个删除按钮,点击时便会弹出ID 的编号然后再删除这一个记录
      

  5.   

    你要是放在DATAGRID中进行删除不是可以达到你的目的了吗???代码和楼上的说的差不多..
      

  6.   

    這句話是可變的 我只是寫一個樣板啊!!!
    sqlComm.CommandText="delete form table where id = '"+str+"'";
      

  7.   

    插入:
    只要把這樣改一下就可以
    sqlComm.CommandText("insert into zxjb(topic,text,message) values('" & text1.Text & "','" & text2.Text & "','" & text3.Text & "')",
      

  8.   

    他的删除方法就是利用sql语句完成,通用.其实datagrid里也是这么作的.
      

  9.   

    不好意思,让你等了这么久,我已经用了很多方法,但实现不了。
    public void DataGrid1_Delete(Object sender, DataGridCommandEventArgs e)
    {
    string deleteCmd = "DELETE from qw where q1 = @q1"; SqlCommand sqlCommand1 = new SqlCommand(deleteCmd,sqlConnection1);
    sqlCommand1.Parameters.Add(new SqlParameter("@q1", SqlDbType.Char, 6));
    sqlCommand1.Parameters["@q1"].Value =DataGrid1.DataKeys[(int)e.Item.ItemIndex]; sqlCommand1.Connection.Open();

    sqlCommand1.ExecuteNonQuery();

    sqlCommand1.Connection.Close(); Bindgrid();
    }//public void DataGrid1_Delete(Object sender, DataGridCommandEventArgs e) 
       
    //string deleteCmd = "DELETE from qw where q1=@q1 ";
    //SqlCommand sqlCommand1 = new SqlCommand(deleteCmd, sqlConnection1);
    //sqlCommand1.Parameters.Add(new SqlParameter("@q1", SqlDbType.Char,6));
      //sqlCommand1.Parameters["@q1"].Value = DataGrid1.Columns .ToString ();
    //sqlCommand1.Connection.Open(); //int id=DataGrid1.SelectedIndex;
    //string q11=this.DataGrid1.DataKeys[(int)id+1].ToString();
    //.Item.Cells[1].Text;
    //string sqlstr="delete from qw where q1="+"'"+q11+"'";
    //sqlConnection1.Open();
    //System.Data.SqlClient.SqlCommand sqlDeleteCommand2=new System.Data.SqlClient.SqlCommand(sqlstr,sqlConnection1);
    //sqlDeleteCommand2.ExecuteNonQuery();
    //sqlConnection1.Close();

    //String deleteCmd = "DELETE FROM qw WHERE q1 d='"+datagrid.DataKeys[i].ToString()+"'";
    //SqlCommand sqlCommand1 = new SqlCommand(deleteCmd, sqlConnection1);
    //sqlCommand1.Parameters.Add(new SqlParameter("@q1", 
    //sqlDbType.Char, 6));
    // Initialize the SqlCommand "@Id" parameter to the ID of the row
    // that was clicked.

    //sqlCommand1.Parameters["@q1"].Value = 
    //DataGrid1.DataKeys[(int)e.Item.ItemIndex];
    // Connect to the database and delete the specified row.
    //sqlCommand1.Connection.Open();
    // Test   whether the delete was accomplished and  display the 
    // appropriate message to the user.

    //sqlCommand1.ExecuteNonQuery();
    //private void Button5_click(object sender, System.Web.UI.WebControls.DataGridCommand.EventArgs e)

    //this. sqlDeleteCommand2.Parameters["@q1"].Value=this.TextBox5.Text.ToString();
    //string sqlstr="delete from qw where q1=@q1";
    //sqlConnection1.Open();
    //System.Data.SqlClient.SqlCommand sqlDeleteCommand2=new System.Data.SqlClient.SqlCommand(sqlstr,sqlConnection1);
    //sqlDeleteCommand2.ExecuteNonQuery();
    //sqlConnection1.Close();

    //private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
       
    // int delRow=e.Item.ItemIndex; //获取删除列
    // this.dataSet11.q1.Rows[delRow].Delete();//在数据集中删除数据
    // this.sqlDataAdapter1.Update(this.dataSet11);//将数据集更改提交到数据库
    // this.DataGrid1.DataBind();//刷新DataGrid
      

  10.   

    請你前台button的HTML中加了CommandName="Delete"<Columns>
    <asp:Button id="button1" Text="刪除"  CommandName="Delete"></asp:Button>
    </Columns>
      

  11.   

    我加了CommandName="Delete"
    但还是实现不了,点击删除没有反映
      

  12.   


    非常感谢!

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    try
    {
    table=new DataTable();
    if(!(IsPostBack))
    Bindgrid();
    }
    catch
    {
    }

    try
    {
    col=new DataColumn();
    col.DataType=System.Type.GetType("System.String");
    col.ColumnName="q1";
    col.AutoIncrement=false;
    col.Unique=false;
    col.ReadOnly=false;
    table.Columns.Add(col);
    //***************************************************
    col=new DataColumn();
    col.DataType=System.Type.GetType("System.String");
    col.ColumnName="q2";
    col.AutoIncrement=false;
    col.Unique=false;
    col.ReadOnly=false;
    table.Columns.Add(col);
    //***************************************************
    col=new DataColumn();
    col.DataType=System.Type.GetType("System.String");
    col.ColumnName="q3";
    col.AutoIncrement=false;
    col.Unique=false;
    col.ReadOnly=false;
    table.Columns.Add(col);
    //***************************************************
    col=new DataColumn();
    col.DataType=System.Type.GetType("System.String");
    col.ColumnName="q4";
    col.AutoIncrement=false;
    col.Unique=false;
    col.ReadOnly=false;
    table.Columns.Add(col);
    //***************************************************
    col=new DataColumn();
    col.DataType=System.Type.GetType("System.String");
    col.ColumnName="q5";
    col.AutoIncrement=false;
    col.Unique=false;
    col.ReadOnly=false;
    table.Columns.Add(col);
    //***************************************************
    col=new DataColumn();
    col.DataType=System.Type.GetType("System.String");
    col.ColumnName="q6";
    col.AutoIncrement=false;
    col.Unique=false;
    col.ReadOnly=false;
    table.Columns.Add(col);
    //***************************************************
    col=new DataColumn();
    col.DataType=System.Type.GetType("System.String");
    col.ColumnName="q7";
    col.AutoIncrement=false;
    col.Unique=false;
    col.ReadOnly=false;
    table.Columns.Add(col);
    //***************************************************
    DataGrid1.DataSource=table;
    DataGrid1.DataBind(); }
    catch
    {
    }
    }

    private DataTable table;
            private DataColumn col;
            private DataRow row; public void Bindgrid()
    {    
    string sele1="select * from qw";
    sqlConnection1.Open();
    sqlSelectCommand1=new SqlCommand(sele1,sqlConnection1);
    SqlDataReader reader=sqlSelectCommand1.ExecuteReader();
    while(reader.Read())
    {
    row=table.NewRow();
    row["q1"]=reader.GetString(0);
    row["q2"]=reader.GetString(1);
    row["q3"]=reader.GetString(2);
    row["q4"]=reader.GetString(3);
    row["q5"]=reader.GetString(4);
    row["q6"]=reader.GetString(5);
    row["q7"]=reader.GetString(6);
    table.Rows.Add(row);
    } DataGrid1.DataSource=table;
    DataGrid1.DataBind();
    reader.Close();
    sqlConnection1.Close(); } private void Button1_Click(object sender, System.EventArgs e)
    {
    Bindgrid();
    }
    public void DataGrid1_Delete(Object sender, DataGridCommandEventArgs e)
    {
    string deleteCmd = "DELETE from qw where q1 = @q1"; SqlCommand sqlCommand1 = new SqlCommand(deleteCmd,sqlConnection1);
    sqlCommand1.Parameters.Add(new SqlParameter("@q1", SqlDbType.Char, 6));
    sqlCommand1.Parameters["@q1"].Value =DataGrid1.DataKeys[(int)e.Item.ItemIndex]; sqlCommand1.Connection.Open();

    sqlCommand1.ExecuteNonQuery();

    sqlCommand1.Connection.Close(); Bindgrid();
    }

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 94px; POSITION: absolute; TOP: 47px" runat="server" Width="260px">
    <Columns>
    <asp:ButtonColumn Text="删除" HeaderText="wbbh" CommandName="Delete"></asp:ButtonColumn>
    </Columns>
    </asp:DataGrid>
    <asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 124px; POSITION: absolute; TOP: 235px" runat="server" Text="浏览"></asp:Button>
    <asp:Button id="Button2" style="Z-INDEX: 103; LEFT: 340px; POSITION: absolute; TOP: 241px" runat="server" Text="删除" CommandName="delete"></asp:Button>
    </form>
    </body>
    </HTML>
      

  13.   

    在<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 94px; POSITION:
    absolute; TOP: 47px" runat="server" Width="260px">里添加OnDeleteCommand="Button1_Click"
      

  14.   

    我也试过了OnDeleteCommand="Button1_Click",这样连浏览数据库都不行了。
      

  15.   

    还要设置datagrid的datakeyfiled=ql
      

  16.   

    你把asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 124px; POSITION: absolute; TOP: 235px" runat="server" Text="浏览"></asp:Button>
    <asp:Button id="Button2" style="Z-INDEX: 103; LEFT: 340px; POSITION: absolute; TOP: 241px" runat="server" Text="删除" CommandName="delete"></asp:Button>
    去掉试试
      

  17.   

    你的浏览按钮是干什么用的?也没有添加事件啊?也没有放到datagrid中?什么意思?
      

  18.   

    浏览数据,绑定。放在Button1_Click下。
      

  19.   

    不好意思,我没用过<asp:ButtonColumn>,不过感觉一般用sqlCommand1.Parameters["@q1"].Value =DataGrid1.DataKeys[(int)e.Item.ItemIndex];的话,按钮应该和选定的行在一列上,好象不能在DATAGRID外的按钮用e.item.itemindex去定位选定的行。
      

  20.   

    public void DataGrid1_Delete(Object sender, DataGridCommandEventArgs e)
    改為:public void DataGrid1_DeleteCommand(Object sender, DataGridCommandEventArgs e)
      

  21.   

    连按钮在DATAGRID上都实现不了,更不用说不能在DATAGRID外的按钮。
      

  22.   

    也一样的,没反映,怎么办?或者教我用其他方法,可以考虑按钮不放在DATAGRID上,不用模板列或按钮列。只用一个按钮来取想删除这一行的值,能这样实现吗?
      

  23.   

    通过这个你可以获得点击数据行的信息
    private void Page_Load(object sender, System.EventArgs e)
    {   
    //页面javascript和隐含控件设置
    Page.RegisterStartupScript("","<script> function SetuserValue(theValue) {document.all.theUser.value=theValue;} var obj = null;</script>");
    Response.Write( @"<input type=hidden name='theUser'>" );
    }private void MyDataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if ((int) e.Item.ItemIndex >=0 )
    {    e.Item.Attributes["onclick"]=" SetuserValue('"+e.Item.Cells[0].Text+"'); if (obj == this) return; if (obj != null) obj.style.backgroundColor = ''; this.style.backgroundColor='#cccccc'; obj=this;";
    e.Item.Attributes["onmouseover"]="javascript:this.bgColor='#FFDDAA'";
    e.Item.Attributes["onmouseout"]="javascript:this.bgColor='#FFFFF5'";
    }
    }
      

  24.   

    有问题:
    sqlComm.CommandText="delete form table where id = '15'";  //form??
    form是表单,应该是from吧!
      

  25.   

    改成from,我调试后也不对错误提示倒没有,就是没有实现它的功能。