以上是 BLL DAL 和 sqlhelptel
DAL
  public  class testsql
    {      public int add_admin(test tet)
      {
          SQLHelper.SQLHelper sqlhelpter = new SQLHelper.SQLHelper();          SqlParameter[] parmlist ={
              sqlhelpter.CreateInParam("@names",SqlDbType.NVarChar,10,tet.Names),
              sqlhelpter.CreateInParam("@xueli",SqlDbType.NVarChar,10,tet.Xueli)          };
          try
          {
              return (sqlhelpter.RunProc("ceshi", parmlist));
 
          }
          catch(Exception ex)
          {
              throw new Exception(ex.Message, ex);
          }
      }
    }
}
BLL
using System;
using System.Collections.Generic;
using System.Text;
using DAL;
using Model;namespace BLL
{
   public class testsystem
    {        public int add(test tests)
        {
            testsql sql = new testsql();
            return (sql.add_admin(tests));
        }
    }
} private SqlConnection myConnection = null;
private readonly string RETURNVALUE = "RETURNVALUE";
private void Open() 
{
// 打开数据库连接
if (myConnection == null) 
{
                myConnection = new SqlConnection(ConfigurationManager.AppSettings["SQLCONNECTIONSTRING"].ToString());
}
if(myConnection.State == ConnectionState.Closed)
{   
try
{
///打开数据库连接
myConnection.Open();
}
catch(Exception ex)
{
SystemError.CreateErrorLog(ex.Message);
}
finally
{
///关闭已经打开的数据库连接
}
}
}
private SqlCommand CreateProcCommand(string procName, SqlParameter[] prams) 
{
///打开数据库连接
Open();

///设置Command
            
SqlCommand cmd = new SqlCommand(procName, myConnection);
            cmd.CommandType = CommandType.StoredProcedure;            ///添加把存储过程的参数
            if (prams != null)
            {
                foreach (SqlParameter parameter in prams)
                {
                    cmd.Parameters.Add(parameter);
                }
            }            ///添加返回参数ReturnValue
            cmd.Parameters.Add(
                new SqlParameter(RETURNVALUE, SqlDbType.Int, 4, ParameterDirection.ReturnValue,
                false, 0, 0, string.Empty, DataRowVersion.Default, null));
public int RunProc(string procName, SqlParameter[] prams) 
{
SqlCommand cmd = CreateProcCommand(procName, prams);
try
{
///执行存储过程
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
///记录错误日志
SystemError.CreateErrorLog(ex.Message);
}
finally
{
///关闭数据库的连接
Close();
}

///返回存储过程的参数值
            return (int)cmd.Parameters[@RETURNVALUE].Value;
}情况是这样的,我断点调试过,其他位置都接受了到值 但是在SqlCommand cmd = new SqlCommand(procName, myConnection); myConnection没有值

解决方案 »

  1.   

    private SqlConnection myConnection = null;   =>   private static SqlConnection myConnection;
      

  2.   

    不知道是不是ConfigurationManager.AppSettings["SQLCONNECTIONSTRING"].ToString()这句话的原因
    要看配置文件
    我一般是用<connectionStrings><add...></connectionStrings>System.Configuration.ConfigurationManager.ConnectionStrings["strConn"].ToString();
      

  3.   

               if(myConnection.State == ConnectionState.Closed)
                {   
                    try
                    {
                        ///打开数据库连接
                        myConnection.Open();//这句话报错了吗?是不是你连接有问题啊?
                    }
                    catch(Exception ex)
                    {
                        SystemError.CreateErrorLog(ex.Message);
                    }
                    finally
                    {
                        ///关闭已经打开的数据库连接                
                    }
                }
      

  4.   

    public void Open()
            {
                myConnection = new SqlConnection(ConfigurationManager.AppSettings["SQLCONNECTIONSTRING"].ToString());                
                try
                {
                    if (myConnection.State == ConnectionState.Closed)
                    {
                        myConnection.Open();
                    }
                }
                catch (Exception Ex)
                {
                    ErrorLog.WriteLog(Ex);
                }
            }这样试试
      

  5.   

    你的问题很正常。
    不过我给你有个建议。
    把con和cmd保存到一个类里面。
    我给你我写的代码。
    //以下是一个类
    public class ClassDB
        {
            public SqlConnection con=new SqlConnection ();
            public SqlCommand cmd=new SqlCommand ();
            public static string ConfigWebNode = "ConnectionString";
            public string GetConnString()
            {
                return ConfigurationManager.AppSettings[ConfigWebNode].ToString();
            }
            /// <summary>
            /// 打开数据库
            /// </summary>
            /// <returns></returns>
            public bool  OpenCon()
            {
                if (con.State  == ConnectionState.Closed)
                {
                    try
                    {
                        con = new SqlConnection(GetConnString());
                        cmd.Connection = con;
                        con.Open();
                        return true;
                    }
                    catch
                    {
                        return false;
                    }
                }
                return false;
                        }
            public void CloseCon()
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                    cmd.Dispose();
                }
            }
        }
    //以下调用。
    /// <summary>
            /// 执行插入、删除、修改操作(通过存储过程、传入参数)
            /// </summary>
            /// <param name="procName">存储过程名称</param>
            /// <param name="para">参数数组</param>
            public void ExecuteSQLproc(String procName, SqlParameter[] para)
            {
                DB.OpenCon();
                DB.cmd.CommandText = procName;
                DB.cmd.CommandType = CommandType.StoredProcedure;
                DB.cmd.Parameters.Clear ();
                DB.cmd.Parameters.AddRange (para);           
                DB.cmd.ExecuteNonQuery();            
                DB.con.Close();
                DB.cmd.Dispose();
    }
    你试试。
      

  6.   

    private SqlConnection myConnection = null;
    修改成
    protected SqlConnection myConnection = null;看一下
      

  7.   

    要不你按种写法试一试。
    private SqlConnection myConnection = null; 
    private string strCon;//连接字符串   
         #region 构造函数
            public DataBase()
            {
                strCon =ConfigurationSettings.AppSettings["strCon"];
            }
            #endregion        #region 打开数据库连接
            /// <summary>
            /// 打开数据库连接
            /// </summary>
            public void openCon()
            {
                if (myConnection == null)
                {
                    myConnection= new SqlConnection(strCon);
                }
                if (myConnection.State.Equals(ConnectionState.Closed))
                {
                   myConnection.Open();
                }
            }
            #endregion