首页 新闻 会员 周边 捐助

关于连接数据库问题

0
[已解决问题] 解决于 2012-02-17 13:05

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Caiji.Models
{
    public static class DBHelper
    {
        //属性:数据库链接对象
        private static SqlConnection connection;
        public static SqlConnection Connection
        {
            get
            {
                string connectionString = ConfigurationManager.ConnectionStrings["conStr"].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;
            }
        }

        #region--增删改--
        public static int ExecuteCommand(string safeSql)
        {
            SqlCommand cmd = new SqlCommand(safeSql, Connection);
            int result = cmd.ExecuteNonQuery();
            return result;
        }

        public static int ExecuteCommand(string sql, params SqlParameter[] values)
        {
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.Parameters.AddRange(values);
            return cmd.ExecuteNonQuery();
        }
        #endregion

        #region--返回首行首列

        public static int GetScalar(string safeSql)
        {
            SqlCommand cmd = new SqlCommand(safeSql, Connection);
            int result = Convert.ToInt32(cmd.ExecuteScalar());
            return result;
        }

        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;
        }
        #endregion

        #region--方法:查询,DataReader--
        public static SqlDataReader GetReader(string safeSql)
        {
            SqlCommand cmd = new SqlCommand(safeSql, Connection);
            SqlDataReader reader = cmd.ExecuteReader();
            return reader;
        }

        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;
        }

        #endregion

        #region--查询:DataTable--
        public static DataTable GetDataSet(string safeSql)
        {
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand(safeSql, Connection);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            return ds.Tables[0];
        }

        public static DataTable GetDataSet(string sql, params SqlParameter[] values)
        {
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.Parameters.AddRange(values);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            return ds.Tables[0];
        }
        #endregion

    }
}

miloss的主页 miloss | 菜鸟二级 | 园豆:254
提问于:2012-02-16 12:05
< >
分享
最佳答案
0

错误信息里不是说的很明白了吗?

检查数据库连接字符串,检查数据库配置,检查网络是否可用

artwl | 专家六级 |园豆:16736 | 2012-02-16 12:37

 <add name="conStr" connectionString="Data Source=.; Initial catalog=CollectionWebInfo; Integrated Security=true"/>  你看有问题没啊

miloss | 园豆:254 (菜鸟二级) | 2012-02-16 12:50

@miloss: 数据库连接字符串没有什么问题,不过建议加上 providerName="System.Data.SqlClient"

如果加上后还出错就检查数据库配置,网络等问题

artwl | 园豆:16736 (专家六级) | 2012-02-16 12:56
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册