日期:2014-05-17  浏览次数:20482 次

.NET调用存储过程
用的是三层,实现注册功能。
这是DAL的SQLHelper
C# code

public static int ExcuteNonQuery(string sqlcmd, SqlParameter[] Parameter)
        {
            SqlConnection conn = new SqlConnection(SqlConn);
            if(conn.State!=ConnectionState.Open)
            {
                conn.Open();
            }
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "st_register";
            cmd.CommandType = CommandType.StoredProcedure;
            if (Parameter != null)
            {
                foreach (SqlParameter item in Parameter)
                {
                    cmd.Parameters.Add(item);
                }
            }
            return cmd.ExecuteNonQuery();
        }


BLL层中的注册功能
C# code

public int CreateStudent(M_Student m_student)
        {
            return SQLHelper.ExcuteNonQuery("st_register", new SqlParameter[] { 
            new SqlParameter("@uname",m_student.Uname),
            new SqlParameter("@password",m_student.Password),
            new SqlParameter("@tel",m_student.Tel),
            new SqlParameter("@sex",m_student.Sex),
            new SqlParameter("@address",m_student.Address)
            });
        }


创建的存储过程
SQL code

create procedure st_register
@uname varchar(50),
@password varchar(50),
@tel varchar(50),
@sex bit,
@address varchar(50)
as
insert into Stu(uname,password,tel,sex,address)values(@uname,@password,@tel,@sex,@address)



为什么每次运行注册的时候会有:“超出了存储过程、函数、触发器或视图的最大嵌套层数(最大层数为 32)。”

------解决方案--------------------
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER procedure [dbo].[st_register]
@uname varchar(50),
@password varchar(50),
@tel varchar(50),
@sex bit,
@address varchar(50)
as
insert into Stu(uname,password,tel,sex,address)values(@uname,@password,@tel,@sex,@address)



go
--go一下
------解决方案--------------------
死循环了,你把BLL中的参数写到DAL中去,直接调用DAL函数就行了