| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 809 人关注过本帖
标题:为什么我在调试登陆时老是出现下面的错误了??求解??各位大神
只看楼主 加入收藏
万光海
Rank: 3Rank: 3
来 自:桂林
等 级:论坛游侠
帖 子:92
专家分:133
注 册:2012-10-21
结帖率:68.75%
收藏
已结贴  问题点数:20 回复次数:5 
为什么我在调试登陆时老是出现下面的错误了??求解??各位大神
图片附件: 游客没有浏览图片的权限,请 登录注册
2013-04-27 17:14
lantian8134
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:8
帖 子:115
专家分:789
注 册:2013-1-11
收藏
得分:20 
查看连接字符串是否写错了
2013-04-27 20:12
万光海
Rank: 3Rank: 3
来 自:桂林
等 级:论坛游侠
帖 子:92
专家分:133
注 册:2012-10-21
收藏
得分:0 
回复 2楼 lantian8134
using System.Collections.Generic;
using System.
using System.Data;
using System.Globalization;
using System.Configuration;

namespace 员工考勤管理系统
{
    #region 委托
    internal delegate object CommandDelegate(DbCommand dbCommand);

    /// <summary>
    /// DbDataReader命令委托
    /// </summary>
    /// <param name="dbDataReader">DbDataReader</param>
    internal delegate void DbDataReadDelegate(DbDataReader dbDataReader);

    #endregion

    /// <summary>
    /// 数据库操作辅助类
    /// </summary>
    internal class DbHelper
    {
        #region 字段

        /// <summary>
        /// 数据库操作辅助类实例对象
        /// </summary>
        private static DbHelper _Instance;

        /// <summary>
        /// 表示连接字符串配置文件节中的单个命名连接字符串
        /// </summary>
        private ConnectionStringSettings _ConnectionStringSettings;

        /// <summary>
        /// 表示一组方法,这些方法用于创建提供程序对数据源类的实现的实例。
        /// </summary>
        private DbProviderFactory _DbProviderFactory;

        /// <summary>
        /// 保存数据库链接字符串
        /// </summary>
        public static string DataConnectionString = "ConnectionString";

        #endregion

        #region 构造函数

        /// <summary>
        /// 无参构造函数
        /// </summary>
        public DbHelper()
        {
            _ConnectionStringSettings = ConfigurationManager.ConnectionStrings[DataConnectionString];

            // 如果连接字符串对象不为空,则创建数据库操作工厂
            if (_ConnectionStringSettings != null)
            {
                if (!string.IsNullOrEmpty(_ConnectionStringSettings.ProviderName) && !string.IsNullOrEmpty(_ConnectionStringSettings.ConnectionString))
                {
                    _DbProviderFactory = DbProviderFactories.GetFactory(_ConnectionStringSettings.ProviderName);
                }
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbConnectionSettingName">连接字符串名称</param>
        public DbHelper(string dbConnectionSettingName)
        {
            //如果连接字符串为NULL或空串,则将连接字符串名称赋值为_DataConnectionString
            if (string.IsNullOrEmpty(dbConnectionSettingName))
            {
                dbConnectionSettingName = DataConnectionString;
            }

            // 如果连接字符串对象不为空,则创建数据库操作工厂
            _ConnectionStringSettings = ConfigurationManager.ConnectionStrings[dbConnectionSettingName];
            if (_ConnectionStringSettings != null)
            {
                if (!string.IsNullOrEmpty(_ConnectionStringSettings.ProviderName) && !string.IsNullOrEmpty(_ConnectionStringSettings.ConnectionString))
                {
                    _DbProviderFactory = DbProviderFactories.GetFactory(_ConnectionStringSettings.ProviderName);
                }
            }
        }

        #endregion

        #region 类属性

        /// <summary>
        /// 单实例
        /// </summary>
        public static DbHelper Instance
        {
            get
            {
                _Instance = _Instance ?? new DbHelper();
                return _Instance;
            }
        }

        #endregion

        #region 实例属性

        /// <summary>
        /// 获得数据库链接字符串
        /// </summary>
        public string ConnectionString
        {
            get
            {
                return _ConnectionStringSettings.ConnectionString;
            }
        }

        /// <summary>
        /// 获得工厂
        /// </summary>
        public DbProviderFactory ProviderFactory
        {
            get
            {
                return _DbProviderFactory;
            }
        }

        /// <summary>
        /// 获得驱动程序名称
        /// </summary>
        public string ProviderName
        {
            get
            {
                return _ConnectionStringSettings.ProviderName;
            }
        }

        #endregion

        #region 方法

        #region CreateConnection

        /// <summary>
        /// 创建一个数据库连接
        /// </summary>
        /// <returns>数据库连接对象</returns>
        public DbConnection CreateConnection()
        {
            if (_DbProviderFactory == null)
            {
                return null;
            }
            else
            {
                DbConnection dbConnection = _DbProviderFactory.CreateConnection();
                dbConnection.ConnectionString = _ConnectionStringSettings.ConnectionString;
                return dbConnection;
            }
        }

        #endregion

        #region ExecuteNonQuery

        /// <summary>
        /// 执行无结果集Sql
        /// </summary>
        /// <param name="sql">Sql语句</param>
        /// <param name="para">占位符参数</param>
        /// <returns>影响的记录数</returns>
        public int ExecuteNonQuery(string sql, params DbParameter[] para)
        {
            return ExecuteNonQuery(CommandType.Text, sql, para);
        }

        /// <summary>
        /// 执行无结果集Sql
        /// </summary>
        /// <param name="commandType">命令类型</param>
        /// <param name="sql">Sql语句</param>
        /// <param name="para">占位符参数</param>
        /// <returns>影响的记录数</returns>
        public int ExecuteNonQuery(CommandType commandType, string sql, params DbParameter[] para)
        {
            CommandDelegate cd = delegate(DbCommand cmd)
            {
                return cmd.ExecuteNonQuery();
            };
            return (int)ExecuteCmdCallback(commandType, sql, cd, para);
        }

        /// <summary>
        /// 执行无结果集Sql
        /// </summary>
        /// <param name="sql">Sql语句</param>
        /// <param name="para">占位符参数</param>
        /// <returns>影响的记录数</returns>
        public int ExecuteNonQuery(string sql, IList<DbParameter> para)
        {
            return ExecuteNonQuery(CommandType.Text, sql, para);
        }

        /// <summary>
        /// 执行无结果集Sql
        /// </summary>
        /// <param name="commandType">命令类型</param>
        /// <param name="sql">Sql语句</param>
        /// <param name="para">占位符参数</param>
        /// <returns>影响的记录数</returns>
        public int ExecuteNonQuery(CommandType commandType, string sql, IList<DbParameter> para)
        {
            CommandDelegate cd = delegate(DbCommand cmd)
            {
                return cmd.ExecuteNonQuery();
            };
            return (int)ExecuteCmdCallback(commandType, sql, cd, para);
        }

        #endregion

        #region ReadData

        /// <summary>
        /// 通过DbDataReader来读取数据
        /// </summary>
        /// <param name="sql">Sql语句</param>
        /// <param name="readDelegate">DbDataReadDelegate委托</param>
        /// <returns></returns>
        public bool ReadData(string sql, DbDataReadDelegate readDelegate)
        {
            CommandDelegate cd = delegate(DbCommand cmd)
            {
                using (DbDataReader dbReader = cmd.ExecuteReader())
                {
                    readDelegate(dbReader);
                    return true;
                }
            };
            return (bool)ExecuteCmdCallback(CommandType.Text, sql, cd);
        }

        #endregion

        #region CreateDataTable

        /// <summary>
        /// 根据Sql创建一个DataTable的结果集
        /// </summary>
        /// <param name="commandType">命令类型</param>
        /// <param name="sql">Sql语句</param>
        /// <param name="para">查询参数</param>
        /// <returns>DataTable</returns>
        public DataTable CreateDataTable(string sql,  IList<DbParameter> para)
        {
            CommandDelegate cd = delegate(DbCommand cmd)
            {
                using (DbDataReader dr = cmd.ExecuteReader())
                {
                    DataTable dt = new DataTable();
                    dt.Locale = CultureInfo.InvariantCulture;
                    dt.Load(dr);
                    return dt;
                }
            };
            return (DataTable)ExecuteCmdCallback(CommandType.Text, sql, cd, para);
        }
        /// <summary>
        /// 根据Sql创建一个DataTable的结果集
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public DataTable CreateDataTable(string sql, params DbParameter[] para)
        {
            CommandDelegate cd = delegate(DbCommand cmd)
            {
                using (DbDataReader dr = cmd.ExecuteReader())
                {
                    DataTable dt = new DataTable();
                    dt.Locale = CultureInfo.InvariantCulture;
                    dt.Load(dr);
                    return dt;
                }
            };
            return (DataTable)ExecuteCmdCallback(CommandType.Text, sql, cd, para);
        }

        #endregion

        #region CreateDataSet

        /// <summary>
        /// 根据Sql语句创建一个DataSet类型的结果集
        /// </summary>
        /// <param name="sql">Sql语句</param>
        /// <param name="para">查询参数</param>
        /// <returns>DataSet结果集</returns>
        public DataSet CreateDataSet(string sql, params DbParameter[] para)
        {
            return CreateDataSet(CommandType.Text, sql, para);
        }

        /// <summary>
        /// 根据Sql语句创建一个DataSet类型的结果集
        /// </summary>
        /// <param name="commandType">命令类型</param>
        /// <param name="sql">Sql语句</param>
        /// <param name="para">查询参数</param>
        /// <returns>DataSet结果集</returns>
        public DataSet CreateDataSet(CommandType commandType, string sql, params DbParameter[] para)
        {
            CommandDelegate cd = delegate(DbCommand cmd)
            {
                using (DbDataAdapter da = _DbProviderFactory.CreateDataAdapter())
                {
                    DataSet ds = new DataSet();
                    ds.Locale = CultureInfo.InvariantCulture;
                    da.SelectCommand = cmd;
                    da.Fill(ds);
                    return ds;
                }
            };
            return (DataSet)ExecuteCmdCallback(commandType, sql, cd, para);
        }

        #endregion

        #region ExecuteScalar

        /// <summary>
        /// 执行查询,并返回查询所返回的结束集中的第一行的第一列
        /// </summary>
        /// <param name="sql">Sql语句</param>
        /// <param name="para">查询参数</param>
        /// <returns>string类型的字符串</returns>
        public string GetValue(string sql, params DbParameter[] para)
        {
            CommandDelegate cd = delegate(DbCommand cmd)
            {
                return cmd.ExecuteScalar();
            };
            object value = ExecuteCmdCallback(CommandType.Text, sql, cd, para);
            if (value == null)
            {
                value = string.Empty;
            }
            return value.ToString();
        }

        /// <summary>
        /// 执行查询,并返回查询所返回的结束集中的第一行的第一列
        /// </summary>
        /// <param name="sql">Sql语句</param>
        /// <param name="para">查询参数</param>
        /// <returns>对象object</returns>
        public object GetObject(string sql, IList<DbParameter> para)
        {
            CommandDelegate cd = delegate(DbCommand cmd)
            {
                return cmd.ExecuteScalar();
            };
            return ExecuteCmdCallback(CommandType.Text, sql, cd, para);
        }
        /// <summary>
        ///执行查询,并返回查询所返回的结束集中的第一行的第一列
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="para"></param>
        /// <returns></returns>
        public object GetObject(string sql, params DbParameter[] para)
        {
            CommandDelegate cd = delegate(DbCommand cmd)
            {
                return cmd.ExecuteScalar();
            };
            return ExecuteCmdCallback(CommandType.Text, sql, cd, para);
        }
        #endregion

        #region ExecuteCmdCallback

        /// <summary>
        /// 执行带参数与委托命令的查询语句,并返回相关的委托命令
        /// </summary>
        /// <param name="commandType">命令类型</param>
        /// <param name="sql">Sql语句</param>
        /// <param name="commandDelegate">委托类型</param>
        /// <param name="para">参数集合</param>
        /// <returns>委托的命令</returns>
        private object ExecuteCmdCallback(CommandType commandType, string sql, CommandDelegate commandDelegate, params DbParameter[] para)
        {
            using (DbConnection dbCnn = CreateConnection())
            {
                using (DbCommand cmd = _DbProviderFactory.CreateCommand())
                {
                     = commandType;
                     = sql;
                    cmd.Connection = dbCnn;
                    foreach (DbParameter dp in para)
                    {
                        cmd.Parameters.Add(dp);
                    }
                    dbCnn.Open();
                    return commandDelegate(cmd);
                }
            }
        }

        /// <summary>
        /// 执行带参数与委托命令的查询语句,并返回相关的委托命令
        /// </summary>
        /// <param name="commandType">命令类型</param>
        /// <param name="sql">Sql语句</param>
        /// <param name="commandDelegate">委托类型</param>
        /// <param name="para">参数集合</param>
        /// <returns>委托的命令</returns>
        private object ExecuteCmdCallback(CommandType commandType, string sql, CommandDelegate commandDelegate, IList<DbParameter> para)
        {
            using (DbConnection dbCnn = CreateConnection())
            {
                using (DbCommand cmd = _DbProviderFactory.CreateCommand())
                {
                     = commandType;
                     = sql;
                    cmd.Connection = dbCnn;
                    foreach (DbParameter dp in para)
                    {
                        cmd.Parameters.Add(dp);
                    }
                    dbCnn.Open();
                    return commandDelegate(cmd);
                }
            }
        }

        #endregion

        #endregion
    }
}
2013-04-27 22:13
万光海
Rank: 3Rank: 3
来 自:桂林
等 级:论坛游侠
帖 子:92
专家分:133
注 册:2012-10-21
收藏
得分:0 
回复 2楼 lantian8134
private object ExecuteCmdCallback(CommandType commandType, string sql, CommandDelegate commandDelegate, params DbParameter[] para)
        {
            using (DbConnection dbCnn = CreateConnection())
            {
                using (DbCommand cmd = _DbProviderFactory.CreateCommand())
                {
                     = commandType;
                     = sql;
                    cmd.Connection = dbCnn;
                    foreach (DbParameter dp in para)
                    {
                        cmd.Parameters.Add(dp);
                    }
                    dbCnn.Open();
                    return commandDelegate(cmd);
                }
            }
        }
版主,他老是说 dbCnn.Open()有错,小的不知道错在哪!求解
2013-04-27 22:14
lantian8134
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:8
帖 子:115
专家分:789
注 册:2013-1-11
收藏
得分:0 
dbCnn.Open()有错就是数据库连接字符串写的不对,所以连接打不开
你看看_ConnectionStringSettings.ConnectionString这行代码的连接字符串写了什么,一般情况下都是写在配置文件里的。
2013-04-28 09:28
万光海
Rank: 3Rank: 3
来 自:桂林
等 级:论坛游侠
帖 子:92
专家分:133
注 册:2012-10-21
收藏
得分:0 
回复 5楼 lantian8134
版主,该怎么改了?我是初学者,我改了好久好久了都没改好,都是这个问题,求版主指点
2013-05-10 11:42
lantian8134
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:8
帖 子:115
专家分:789
注 册:2013-1-11
收藏
得分:0 
那把连接字符串贴出来看看
2013-05-10 11:58
快速回复:为什么我在调试登陆时老是出现下面的错误了??求解??各位大神
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030009 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved