Gridview - 为什么我没有正确结束SQL命令

时间:2017-04-03 13:20:27

标签: c# asp.net oracle gridview

我正在尝试创建一个简单的gridview,它显示我的Oracle DB中的一个表,并允许我删除一行。我目前收到错误' SQL命令未正确结束'在此处查看之后,似乎Oracle不允许在更新语句上加入。但我没有加入或做更新声明。任何人都可以看到问题的来源吗?提前谢谢。

Error

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Configuration;

using System.Data.OracleClient;

namespace DatabaseTest
{
public partial class AvatarView : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }      
    protected void Gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string avatarID = GridView1.DataKeys[e.RowIndex].Value.ToString();
        string deleteSql = "DELETE FROM Avatar WHERE AvatarID = :AvatarID; ";

        using (var con = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionString3"].ConnectionStr‌​ing))
        using (var cmd = new OracleCommand(deleteSql, con))
        {
            cmd.Parameters.Add(":AvatarID", OracleType.VarChar).Value = avatarID;
            con.Open();
            int deleted = cmd.ExecuteNonQuery();
        }

        GridView1.DataSource = SqlDataSource1;
        GridView1.DataBind();
    }
}
}

源代码 -

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AvatarView.aspx.cs" Inherits="DatabaseTest.AvatarView" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

    <asp:GridView ID="GridView1" runat="server" OnRowDeleting="Gridview1_RowDeleting" AllowSorting="True" AutoGenerateColumns="False" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" DataKeyNames="AVATARID" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="None" AllowPaging="True" Height="422px" Width="1020px">
        <AlternatingRowStyle BackColor="PaleGoldenrod" />
        <Columns>
            <asp:BoundField DataField="AVATARID" HeaderText="AVATARID" SortExpression="AVATARID" ReadOnly="True" />
            <asp:BoundField DataField="AVATARNAME" HeaderText="AVATARNAME" SortExpression="AVATARNAME" />
            <asp:BoundField DataField="DOB" HeaderText="DOB" SortExpression="DOB" />
            <asp:BoundField DataField="STRENGTH_CURR" HeaderText="STRENGTH_CURR" SortExpression="STRENGTH_CURR" />
            <asp:BoundField DataField="GENDER" HeaderText="GENDER" SortExpression="GENDER" />
            <asp:BoundField DataField="HOARD" HeaderText="HOARD" SortExpression="HOARD" />
            <asp:BoundField DataField="SPECIESID" HeaderText="SPECIESID" SortExpression="SPECIESID" />
            <asp:BoundField DataField="USERID" HeaderText="USERID" SortExpression="USERID" />             
            <asp:CommandField ShowDeleteButton="True" />


        </Columns>
        <FooterStyle BackColor="Tan" />
        <HeaderStyle BackColor="Tan" Font-Bold="True" />
        <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
        <SortedAscendingCellStyle BackColor="#FAFAE7" />
        <SortedAscendingHeaderStyle BackColor="#DAC09E" />
        <SortedDescendingCellStyle BackColor="#E1DB9C" />
        <SortedDescendingHeaderStyle BackColor="#C2A47B" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString3 %>" ProviderName="<%$ ConnectionStrings:ConnectionString3.ProviderName %>" SelectCommand="SELECT * FROM &quot;AVATAR&quot;"></asp:SqlDataSource>

</div>
</form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

从此行的SQL语句中删除分号:

string deleteSql = "DELETE FROM Avatar WHERE AvatarID = :AvatarID; ";