首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

,“/Web”应用程序中的服务器异常, 必须声明标量变量 "@IDENTITY"

2013-08-25 
求助,“/Web”应用程序中的服务器错误, 必须声明标量变量 @IDENTITY最近一直在照着课本做一个求职网站,但

求助,“/Web”应用程序中的服务器错误, 必须声明标量变量 "@IDENTITY"
最近一直在照着课本做一个求职网站,但是现在出现问题是,能调试成功页面,但是在页面上进行操作就处错误了!
错误代码如下:
异常详细信息: System.Data.SqlClient.SqlException: 必须声明标量变量 "@IDENTITY"。

源错误: 


行 31:            {
行 32:                Console.WriteLine(e.Message);
行 33:                throw e;
行 34:            }
行 35:        }

源文件: D:\HR\HRDAL\UserService.cs    行: 33 


下边我附上UserService.cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HRModels;
using System.Data.SqlClient;
using System.Data;
namespace HRDAL
{
   public static partial class UserService
    {
       //添加User
       public static User AddUser(User user)
       {
           string sql = "INSERT userdetails (userName,userPass,role,regTime,logNum)" + "VALUES (@userName,@userPass,@role,@regTime,@logNum)";
           sql += " ;SELECT @IDENTITY";
           try
           {
               SqlParameter[] para = new SqlParameter[]
               {
                   new SqlParameter("@userName",user.UserName),
                   new SqlParameter("@userPass",user.UserPass),
                   new SqlParameter("@role",user.Role),
                   new SqlParameter("@regTime",user.RegTime),
                   new SqlParameter("@logNum",user.LogNum)


               };
               int newId = DBhelper.GetScalar(sql, para);
               return GetUserByUserId(newId);
           }
           catch (Exception e)
           {
               Console.WriteLine(e.Message);
               throw e;
           }
       }
       //删除User
       public static void DeleteUser(User user)
       {
           DeleteUserByUserId(user.UserId);
       }
       //根据userId删除用户
       public static void DeleteUserByUserId(int userId)
       {
           string sql = "DELETE userdetails WHERE UserId=@UserId";
           try
           {
               SqlParameter[] para = new SqlParameter[]
              {
                 new SqlParameter("@UserId",userId)
              };
               DBhelper.ExecuteCommand(sql, para);
           }
           catch (Exception e)
           {
               Console.WriteLine(e.Message);
               throw e;
           }


       }
       //修改用户
       public static void ModifyUser(User user)
       {
           string sql = "UPDATE userdetails" + "SET"+
               "userName=@userName,"+
               "userPass=@userPass,"+
               "role=@role,"+
               "regTime=@regTime,"+
               "logNum=@logNum"+
               "WHERE userId=@userId";
           try
           {
               SqlParameter[] para = new SqlParameter[]
               {
                   new SqlParameter("@userId",user.UserId),
                   new SqlParameter("@userName",user.UserName),
                   new SqlParameter("@userPass",user.UserPass),
                   new SqlParameter("@role",user.Role),
                   new SqlParameter("@regTime",user.RegTime),
                   new SqlParameter("@logNum",user.LogNum)
               };
               DBhelper.ExecuteCommand(sql, para);
           }
           catch (Exception e)
           {
               Console.WriteLine(e.Message);


               throw e;
           }
       }
       //获取所有用户
       public static IList<User> GetAllUsers()
       {
           string sqlAll = "SELECT * from uesrdetails";
           return GetUserBySql(sqlAll);
       }
       //根据userId获取用户信息
       public static User GetUserByUserId(int userId)
       {
           string sql = "select * from userdetails where UserId=@UserId";
           try
           {
               SqlDataReader reader=DBhelper.GetReader(sql,
                   new SqlParameter("@UserId",userId));
               if(reader.Read())
               {
                   User user=new User();
                   user.UserId=(int) reader["userId"];
                   user.UserName=(string)reader["userName"];
                   user.UserPass=(string)reader["userPass"];
                   user.Role=(int)reader["role"];
                   user.RegTime=(DateTime)reader["regTime"];
                   user.LogNum=(int)reader["logNum"];
                   reader.Close();
                   return user;


               }
               else
               {
                  reader.Close();
                  return null;
               }
           }
           catch (Exception e)
           {
               Console.WriteLine(e.Message);
               throw e;
           }
       }
           //根据name获取用户信息
       public static User GetUserByUserName(string name)
       {
           string sql = "select * from userdetails where userName=@UserName";
           try
           {
               SqlDataReader reader = DBhelper.GetReader(sql,
                   new SqlParameter("@UserName", name));
               if (reader.Read())
               {
                   User user = new User();
                   user.UserId = (int)reader["userId"];
                   user.UserName = (string)reader["userName"];
                   user.UserPass = (string)reader["userPass"];


                   user.Role = (int)reader["role"];
                   user.RegTime = (DateTime)reader["regTime"];
                   user.LogNum = (int)reader["logNum"];
                   reader.Close();
                   return user;
               }
               else
               {
                   reader.Close();
                   return null;
               }
           }
           catch (Exception e)
           {
               Console.WriteLine(e.Message);
               throw e;
           }
       }
           //根据sql获取用户列表
       private static IList<User> GetUserBySql(string safeSql)
       {
           List<User> list=new List<User>();
           try
           {
               DataTable table=DBhelper.GetDataSet(safeSql);
               foreach (DataRow row in table.Rows)
               {
                   User user=new User();


                   user.UserId=(int) row["userId"];
                   user.UserName=(string)row["userName"];
                   user.UserPass=(string)row["userPass"];
                   user.Role=(int)row["role"];
                   user.RegTime=(DateTime)row["regTime"];
                   user.LogNum=(int)row["logNum"];
                   list.Add(user);
               }
               return list;
           }
           catch (Exception e)
           {
               Console.WriteLine(e.Message);
               throw e;
           }
       }
    }
}


还有用到的DBhelp.cs代码(字数超限制,我省去一部分):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace HRDAL
{
   //数据库访问基础类
    public static class DBhelper
    {
       private static SqlConnection connection;
        //连接属性
       public static SqlConnection Connection
       {
           get
           {
               //从配置文件中获取连接数据库的连接字符串
               string connectionString =ConfigurationManager.ConnectionStrings["HRConStr"].ConnectionString;


               if (connection == null)
               {
                   connection = new SqlConnection(connectionString);
                   connection.Open();
               }
               else if(connection.State==System.Data.ConnectionState.Closed)
               {
                   connection.Open();
               }
               else if(connection.State==System.Data.ConnectionState.Broken)
               {
                   connection.Close();
                   connection.Open();
               }
               return connection;
           }
       }
        //不带参数的执行命令
       public static int ExecuteCommand(string safeSql)
       {
           SqlCommand cmd = new SqlCommand(safeSql, Connection);
           int result = cmd.ExecuteNonQuery();
           return result;
           connection.Close();
            
       }
        //带参数的执行命令
       public static int ExecuteCommand(string sql, params SqlParameter[] values)


       {
           SqlCommand cmd = new SqlCommand(sql, Connection);
           cmd.Parameters.AddRange(values);
           return cmd.ExecuteNonQuery();
           connection.Close();
       }
       public static int GetScalar(string safeSql)
       {
           SqlCommand cmd = new SqlCommand(safeSql, Connection);
           int result = Convert.ToInt32(cmd.ExecuteScalar());
           return result;
           connection.Close();
           
       }
       public static int GetScalar(string sql, params SqlParameter[] values)
       {
           SqlCommand cmd = new SqlCommand(sql, Connection);
           cmd.Parameters.AddRange(values);
           int result = Convert.ToInt32(cmd.ExecuteScalar());
           return result;
           connection.Close();
           
       }
       public static SqlDataReader GetReader(string safeSql)
       {
           SqlCommand cmd = new SqlCommand(safeSql, Connection);
           SqlDataReader reader = cmd.ExecuteReader();
           return reader;
           connection.Close();
           
       }
       public static SqlDataReader GetReader(string sql, params SqlParameter[] values)


       {
           SqlCommand cmd = new SqlCommand(sql, Connection);
           cmd.Parameters.AddRange(values);
           SqlDataReader reader = cmd.ExecuteReader();
           return reader;
           connection.Close(); 
       }
    }
}


麻烦高手能帮我解答一下,谢谢大家了! 异常 应用 ASP.NET? SQL 服务器
[解决办法]
 @IDENTITY  
=》
 @@IDENTITY
[解决办法]
SELECT @@IDENTITY
[解决办法]
楼上正解,@@identity
[解决办法]

引用:
谢谢,谢谢!我刚开始加上了双@  但是忘记空格了,今天又重新加的  没有问题了  
我是初学者 想问一下 这个双@是语法吗?谢谢ls各位  结贴必给分!!!论坛大神真多!!!


是System Functions

参考
http://technet.microsoft.com/en-us/library/ms187786.aspx

热点排行