我有一个GridView动态地从Oracle数据库中提取数据。
<asp:GridView ID="gvData" runat="server" DataSourceID="DS" CellPadding="4"
ForeColor="#333333" AllowSorting="True" AllowPaging="True"
ShowFooter="True" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True" AutoGenerateSelectButton="True"
OnRowDataBound="gvData_RowDataBound"
OnSelectedIndexChanged="gvData_SelectedIndexChanged"
OnRowUpdating="gvData_RowUpdating" OnRowUpdated="gvData_RowUpdated"
OnRowDeleting="gvData_RowDeleting" OnRowDeleted="gvData_RowDeleted">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True"
ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#CCCCCC" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:SqlDataSource ID="DS" runat="server">
</asp:SqlDataSource>
对于SELECT/INSERT/DELETE语句可以正常工作,但是当涉及到UPDATE语句时,它返回一个错误ORA-01036:非法变量名/数
protected void gvData_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
double number;
List<string> text = new List<string>();
List<string> pkParam = new List<string>();
TextBox tb = new TextBox();
LinkButton lb = new LinkButton();
for (int i = 1; i < gvData.HeaderRow.Cells.Count; i++)
{
try
{
lb = (LinkButton)gvData.HeaderRow.Cells[i].Controls[0];
tb = (TextBox)gvData.Rows[e.RowIndex].Cells[i].Controls[0];
if (tb.Text.Length > 0 && !Settings.PK.Contains(lb.Text))
if(Double.TryParse(tb.Text.ToText(), out number))
text.Add(lb.Text + " = " + number);
else
text.Add(lb.Text + " = " + "'" + tb.Text.ToText() + "'");
}
catch { }
}
foreach (string pk in Settings.PK)
{
pkParam.Add(pk + " = :" + pk);
DS.UpdateParameters.Add(
new Parameter(pk, TypeCode.String, e.Keys[pk].ToString()));
}
DS.UpdateCommand = string.Format("UPDATE {0} SET {1} WHERE {2}",
Settings.TABLE, string.Join(",", text.ToArray()),
string.Join(" AND ", pkParam.ToArray()));
}
RowUpdate的代码并不重要。我会写任何更新语句,它会返回相同的错误。大家有什么想法吗?事先谢谢你!
示例查询:DS.UpdateCommand = "UPDATE ATDB.CTD_PROBLEM_EDIT_V SET CUTTING_COMMENTS = 'rrr'";
PK:
public static string[] PK = new string[] { "PROBLEM_DEPTH_MD", "API", "BUS_AREA_ID" };
因为dataKeyNames将自动作为参数添加到updateCommand,所以command_text和dataSource之间的参数计数将不相等。你需要在do update
我家里没有开发环境设置,所以我不能检查我的建议。看看将它放在sqlDataSource上是否适合您。ConflictDetection = " OverwriteChanges "
例子:
<asp:SqlDataSource ID="DS" ConflictDetection="OverwriteChanges" runat="server">
</asp:SqlDataSource>
更新:
另一种可能是参数名太长。长度限制为31或32个字符。在过去,当我动态创建参数列表时,我只会给相应的参数名称一个通用名称(例如。参数[x]),并随着循环增加参数名称(例如:Param1、Param2、Param3,依此类推,用于语句中需要的每个参数)。其他一些建议是验证您的头使用的参数名称是有效的(即。没有空格)。
我最后的想法是SqlDataSource不兼容。您使用的是System.Data.OracleClient还是Oracle.DataAccess.Client?尝试使用不同的提供商,看看是否会有所不同。
请参阅这些文章以获得更多帮助。我就知道这么多。对不起!
Oracle.DataAccess。ASP中的客户端和SqlDataSource。净
更新gridview产生ORA-01036:非法变量名称/编号
更新:如上所述,尝试切换您的数据提供程序,在本例中,尝试ODP.NET。您必须安装它。它不是一个系统库。在Oracle中使用ODBC可能是另一种选择。在任何情况下,您可能没有选择,只能不使用System.Data.OracleClient。
固定!Oracle 11G和SqlDataSource:丑陋的"ORA-01036:非法变量名称/编号"问题
Oracle数据提供程序
我找到了解决问题的方法。不是最好的,但可以接受。而不是DS.UpdateCommand
,我使用OracleCommand简单地忽略异常关于设置UpdateCommand。
protected void gvData_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
if (e.Exception != null)
e.ExceptionHandled = true;
}