m0_64700631 2022-06-27 18:17 采纳率: 80%
浏览 69
已结题

C#中asp.net添加数据遇到的错误

问题遇到的现象和发生背景

C# asp.net中添加数据遇到的问题
报的异常是必须声明必须声明标量变量 "@id"。

问题相关代码,请勿粘贴截图
```c#
  protected void AddTKButton_Click(object sender, EventArgs e)
        {
            //获取界面数据
            Models.tkInfo tk = new Models.tkInfo();
          //  string id = Request.QueryString["id"];
            tk.title = TKMC.Text;
            tk.tklx = TKLX.SelectedValue;
            tk.cs1 = CS1.Text;
            tk.cs2 = CS2.Text;
            tk.cs3 = CS3.Text;
            tk.cs4 = CS4.Text;
            tk.score =int.Parse(Score.Text);
            tk.result = Result.Text;
            //保存数据
            Models.BLLResult result = new BLL.TKBLL().AddTK(tk);
            if (result.code == 1)
            {
                Response.Write("<script>alert('新增成功');window.location.href='ListTK.aspx';</script>");
            }
            else
            {
               string msg = Server.HtmlEncode(result.msg.Replace("\r", "").Replace("\n", ""));
               Response.Write("<script>alert('" + msg + "');window.location.href='ListTK.aspx';</script>");
            }
        }
DAL层相关的代码
 public int AddTK(Models.tkInfo tk)
        {
            return DBA.Insert(tk, new string[] { "id" });
        }

```c#

BLL层相关代码
 public Models.BLLResult AddTK(Models.tkInfo tk)
        {
            Models.BLLResult result = new Models.BLLResult();
            //常规校验
            if (string.IsNullOrEmpty(tk.title))
            {
                result.SetError("请输入题库标题");
                return result;
            }
            if (string.IsNullOrEmpty(tk.tklx))
            {
                result.SetError("请选择题库类型");
                return result;
            }
            if (string.IsNullOrEmpty(tk.cs1))
            {
                result.SetError("请输入选项一");
                return result;
            }

            if (string.IsNullOrEmpty(tk.cs2))
            {
                result.SetError("请输入选项二");
                return result;
            }
            if (tk.tklx != "判断题")
            {
                if (string.IsNullOrEmpty(tk.cs3))
                {
                    result.SetError("请输入选项三");
                    return result;
                }
                if (string.IsNullOrEmpty(tk.cs4))
                {
                    result.SetError("请输入选项四");
                    return result;
                }
            }
            else
            {
                tk.cs3 = null;
                tk.cs4 = null;
            }
            if (tk.score <= 0)
            {
                result.SetError("请输入分值");
                return result;
            }
            if (string.IsNullOrEmpty(tk.result))
            {
                result.SetError("请输入答案");
                return result;
            }

            //新增数据
            try
            {
                new DAL.TKDAL().AddTK(tk);
                result.SetData(null);
                return result;
            }
            catch (Exception ex)
            {
                result.SetError("系统异常:" + ex.Message);
                return result;
            }

```c#
DATA层的相关方法代码
  public int Insert<T>(T t,string[] identitiyCols) where T:class,new()
        {
            //获取泛型数据的属性数组
           PropertyInfo[] pi_arr = t.GetType().GetProperties();        
            //定义参数集合
            List<SqlParameter> parameters = new List<SqlParameter>();
            //获取表名(我们约定表明跟类名一样)
            string tableName = t.GetType().Name;
            //定义储存列的可变字符串
            StringBuilder sColName = new StringBuilder();
            //定义储存列值的可变字符串
            StringBuilder sColValue = new StringBuilder();
            foreach (PropertyInfo pi in pi_arr)
            {
                //获取列
                string colName = pi.Name;
                //设置列
                if (sColName.Length > 0) sColName.Append(",");                               
                sColName.Append(colName);              
                //设置值
                if (sColValue.Length > 0) sColValue.Append(",");                                        
                sColValue.Append("@"  + colName);              
                //获取列的值(获取列的值一般是object类型)
                object colValue = pi.GetValue(t, null);
                //自增长的主键不允许显示插入,如果存在则continue执行下一个属性
                if (identitiyCols != null)
                {
                    if (ArrayUtil.CheckArrayHasData(identitiyCols, colName)) continue;
                }
                //当colValue=null时则将参数的值设置为DBValue.null,否则用获取得到的值
                if (colValue == null)
                {
                    SqlParameter parameter = new SqlParameter("@" + colName, colValue);
                    //将参数加入到参数集合
                    parameters.Add(parameter);
                }
                else
                {
                    SqlParameter parameter = new SqlParameter("@" + colName, colValue);
                    //将参数加入到参数集合
                    parameters.Add(parameter);
                }
            }
            string sql = string.Format(" insert into {0} ({1}) values ({2}) ", tableName, sColName.ToString(), sColValue.ToString());
            Console.WriteLine(sql);
            return ExecuteNonQuery(sql, parameters.ToArray());
        }


运行结果及报错内容

系统异常:必须声明标量变量 "@"。

我的解答思路和尝试过的方法

此窗口没用到datagridview控件,网上搜的方法没有用

我想要达到的结果
  • 写回答

4条回答 默认 最新

  • 一只皮卡皮卡丘 2022-06-28 14:00
    关注

    试一下把id去掉,sql server主键自增应该不用写id的,或者值改为0

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 6月28日
  • 已采纳回答 6月28日
  • 修改了问题 6月27日
  • 修改了问题 6月27日
  • 展开全部

悬赏问题

  • ¥150 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装