当前位置:编程学习 > C#/ASP.NET >>

为什么有参数没有给定值?


public Rollback(string id,string t,string obj)
        {
            string p;
            string sql;
            HttpContext context;
            getSQL(t, out p, out sql, out context);
            if (sql != ""&&p!="")
            {
                con = new DataBaseConnector(p).CreateConnection();
                OleDbCommand com = new OleDbCommand(sql, con);
                com.Parameters.Add("@ID", int.Parse(obj));
                con.Open();
                com.ExecuteNonQuery();
                con.Close();
                com.Parameters.Clear();
                com.CommandText = "DELECT FROM [operation] WHERE ID=@id";
                com.Parameters.Add("@id", id);
                con = new DataBaseConnector(context.Server.MapPath(@"数据库路径,你懂得")).CreateConnection();
                con.Open();
                com.ExecuteNonQuery();
                com.Dispose();
                con.Close();
            }
        }

        private static void getSQL(string t, out string p, out string sql, out HttpContext context)
        {
            context = System.Web.HttpContext.Current;
            switch (t)
            {
                case "主题":
                    p = context.Server.MapPath(@"数据库路径,你懂得");
                    sql = "UPDATE [article] SET [ADelected]='0' WHERE ID=@id";
                    break;
                case "回复":
                    p = context.Server.MapPath(@"数据库路径,你懂得");
                    sql = "UPDATE [re] SET [Delected]=0 WHERE ID=@id";
                    break;
                case "投票回复":
                    p = context.Server.MapPath(@"数据库路径,你懂得");
                    sql = "UPDATE [re] SET [delected]=0 WHERE ID=@id";
                    break;
                case "投票":
                    p = context.Server.MapPath(@"App_Data\4sdr564gb.mdb");
                    sql = "UPDATE [article] SET [delected]=0 WHERE ID=@id";
                    break;
                case "用户":
                    p = context.Server.MapPath(@"数据库路径,你懂得");
                    sql = "UPDATE [user] SET [valid]='2001/1/1' WHERE ID=@id";
                    break;
                default:
                    p = "";
                    sql = "";
                    break;
            }
        }

调用时:

用户代码未处理 System.Data.OleDb.OleDbException
  Message="至少一个参数没有被指定值。"
  Source="Microsoft JET Database Engine"
  ErrorCode=-2147217904
  StackTrace:
       在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
       在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
       在 System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
       在 System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
       在 System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
       在 System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
       在 CQApplication.classes.Rollback..ctor(String id, String t, String obj) 位置 D:\CQApplication\CQApplication\CQApplication\classes\Rollback.cs:行号 25
       在 CQApplication.delected.liOp_ItemCommand(Object sender, ListViewCommandEventArgs e) 位置 D:\CQApplication\CQApplication\CQApplication\delected.aspx.cs:行号 57
       在 System.Web.UI.WebControls.ListView.OnItemCommand(ListViewCommandEventArgs e)
       在 System.Web.UI.WebControls.ListView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup)
       在 System.Web.UI.WebControls.ListView.OnBubbleEvent(Object source, EventArgs e)
       在 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
       在 System.Web.UI.WebControls.ListViewDataItem.OnBubbleEvent(Object source, EventArgs e)
       在 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
       在 System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
       在 System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
       在 System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       在 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       在 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 

调试发现:
com.Parameters.Count
1
com.Parameters[0]
{@ID}
    base {System.Data.Common.DbParameter}: {@ID}
    DbType: Int32
    Direction: Input
    IsNullable: false
    OleDbType: Integer
    ParameterName: "@ID"
    Precision: 0
    Scale: 0
    Size: 0
    SourceColumn: ""
    SourceColumnNullMapping: false
    SourceVersion: Current
    Value: 4 --------------------编程问答--------------------         if (sql != ""&&p!="")
            {
                con = new DataBaseConnector(p).CreateConnection();
                OleDbCommand com = new OleDbCommand(sql, con);
                com.Parameters.Add("@ID", int.Parse(obj));
改为id不要@, --------------------编程问答-------------------- 改成这样

com.Parameters.AddWithValue("@ID", int.Parse(obj));

com.Parameters.AddWithValue("@id", id);
--------------------编程问答--------------------
引用 2 楼 net_lover 的回复:
改成这样

com.Parameters.AddWithValue("@ID", int.Parse(obj));

com.Parameters.AddWithValue("@id", id);
仍有同样问题。 --------------------编程问答-------------------- com.CommandText = "DELECT FROM [operation] WHERE ID=@id";

你这sql语句是错误的DELECT ?还是select 还是Delete? --------------------编程问答-------------------- 另外你执行
com.ExecuteNonQuery();
之前打印出sql看是什么。
另外,ID=@id也可以写成 [ID]=@id
其他应该没什么错误了 --------------------编程问答--------------------
引用 5 楼 net_lover 的回复:
另外你执行
com.ExecuteNonQuery();
之前打印出sql看是什么。
另外,ID=@id也可以写成 [ID]=@id
其他应该没什么错误了
仍有同样异常。SQL为:
UPDATE [article] SET [ADelected]='0' WHERE [ID]=@id
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,