数据删除问题uniqueidentifier数据类型
请给我大虾帮我看看是什么问题,如何解决,谢谢!本人刚开始学习数据库,基础不是很好。我在删除数据时没有反应。
1.我的数据表admin_news(ID,Name,Class)的字段的ID的数据类型为uniqueidentifier,其他的字段为varchar.
2.我定义了个数据操作类名称为Admin_News.cs,代码如下:
using System;
using System.Data;
using System.Configuration;
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 interface IAdmin_News
{
/// <summary>
///
/// </summary>
/// <param name= "nID "> </param>
/// <returns> </returns>
int Dell_News(int nID);
}
/// <summary>
/// Admin_News 的摘要说明
/// </summary>
public class Admin_News:IAdmin_News
{
/// <summary>
///
/// </summary>
private static readonly string DELL_NEWS = "Delete admin_news Where ID= ";
public Admin_News()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public int Dell_News(int nID)
{
///创建链接
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[ "Carera "].ConnectionString);
///定义SQL语句
string cmdText = DELL_NEWS
+ " ' " + nID.ToString() + " ' ";
///创建Command
SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
///定义返回值
int nResult = -1;
try
{
///打开链接
myConnection.Open();
///执行SQL语句
nResult = myCommand.ExecuteNonQuery();
}
catch (SqlException ex)
{
///抛出异常
throw new Exception(ex.Message, ex);
}
finally
{ ///关闭链接
myConnection.Close();
}
///返回nResult
return nResult;
}
}
3.aspx页的代码为
<asp:GridView ID= "View_News_List " runat= "server " Width= "100% " AutoGenerateColumns= "false " OnRowDataBound= "View_News_List_RowDataBound " OnRowDeleting= "View_News_List_RowDeleting ">
<Columns>
<asp:TemplateField HeaderText= "操作 " HeaderStyle-Width= "10% ">
<ItemTemplate>
<asp:ImageButton ID= "BT_Del " runat= "server " CommandName= "delete " AlternateText= "删除该信息 " CommandArgument= ' <%# DataBinder.Eval(Container.DataItem, "ID ") %> ' Height= "12px " ImageUrl= "~/Admin/Images/delete.gif " Width= "12px " />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
aspx.cs的代码为
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 Admin_admin_news_add_manage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
///数据绑定接口
Bind_News();
}
}
private void Bind_News()
{
Admin_News BindNews = new Admin_News();
SqlDataReader dr = BindNews.Get_News();
View_News_List.DataSource = dr;
View_News_List.DataBind();
dr.Close();
}
protected void View_News_List_RowDataBound(object sender, GridViewRowEventArgs e)
{
int i;
//执行循环,保证每条数据都可以更新
for (i = -1; i < View_News_List.Rows.Count; i++)
{
//首先判断是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//当鼠标停留时更改背景色
e.Row.Attributes.Add( "onmouseover ", "c=this.style.backgroundColor;this.style.backgroundColor= '#00A9FF ' ");
//当鼠标移开时还原背景色
e.Row.Attributes.Add( "onmouseout ", "this.style.backgroundColor=c ");
}
}
}
protected void View_News_List_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "delete ")
{
try
{ ///删除数据
Admin_News DellNews = new Admin_News();
DellNews.Dell_News(Int32.Parse(e.CommandArgument.ToString()));
///重新绑定控件的数据
Bind_News();
Response.Write( " <script> alert( ' " + "删除数据成功,请妥善保管好你的数据! " + " '); </script> ");
}
catch (Exception ex)
{ ///跳转到异常错误处理页面
// Response.Redirect( "ErrorPage.aspx?ErrorMsg= " + ex.Message.Replace( " <br> ", " ").Replace( "\n ", " ") + "&ErrorUrl= " + Request.Url.ToString().Replace( " <br> ", " ").Replace( "\n ", " "));
}
}
}
protected void View_News_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
///
}
}
[解决办法]
分是不多,主要是到.net板块问更合适,楼下的解答
[解决办法]
uniqueidentifier 在.NET中对应的数据类型是System.Guid
你最后拼接的语句应该是这样的:
Delete admin_news Where ID= 'xxxxxxxx-xxx-xxx-xxx-xxxxxxxx '