日期:2014-05-20  浏览次数:20883 次

LinQ执行一条计算查询结果语句,返回查询结果
LinQ执行一条计算查询结果语句,返回查询结果

也就是我现在需要一个方法:插入一条数据返回ID再继续插入另一张表

以前是这样写的:
C# code

/// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Five8.Model.Agency model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Agency(");
            strSql.Append("district,Area,Name,NetCode,NumberID)");
            strSql.Append(" values (");
            strSql.Append("@district,@Area,@Name,@NetCode,@NumberID)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
     new SqlParameter("@district", SqlDbType.VarChar,50),
     new SqlParameter("@Area", SqlDbType.VarChar,50),
     new SqlParameter("@Name", SqlDbType.VarChar,50),
     new SqlParameter("@NetCode", SqlDbType.VarChar,50),
     new SqlParameter("@NumberID", SqlDbType.VarChar,50)};
            parameters[0].Value = model.district;
            parameters[1].Value = model.Area;
            parameters[2].Value = model.Name;
            parameters[3].Value = model.NetCode;
            parameters[4].Value = model.NumberID;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
 
 
 [color=#FF0000]/// <summary>
        /// 执行一条计算查询结果语句,返回查询结果(object)。
        /// </summary>
        /// <param name="SQLString">计算查询结果语句</param>
        /// <returns>查询结果(object)</returns>
        public static object GetSingle(string SQLString, params SqlParameter[] cmdParms)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    try
                    {
                        PrepareCommand(cmd, connection, null, SQLString, cmdParms);
                        object obj = cmd.ExecuteScalar();
                        cmd.Parameters.Clear();
                        if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                        {
                            return null;
                        }
                        else
                        {
                            return obj;
                        }
                    }
                    catch (System.Data.SqlClient.SqlException e)
                    {
                        throw e;
                    }
                    finally
                    {
                        cmd.Dispose();
                        connection.Close();
                    }
                }
            }
        }[/color]




现在我想知道再LinQ下的GetSingle(string SQLString, params SqlParameter[] cmdParms)方法应该怎么写,也就是SQL操作层方法怎么写
在线等待
最好发QQ:523983274 注明CSDN

------解决方案--------------------
C# code

  public static string Insert(Pt_Vender Obj)
    {
        try
        {
            DatabaseHelper.Insert<Pt_Vender>(Obj);
            return Obj.ID.ToString();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }