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日
  • 展开全部

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵