当前位置:编程学习 > C#/ASP.NET >>

获取webconfig连接字符串,坐等回复

现在做二次开发,人家写的webconfig是这样的
configSections>

    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
         <section name="Naes.Web.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
     </sectionGroup>
 </configSections>
<appSettings>
<add key="ConnectionString" value="Provider=sqloledb; Data Source=10.10.0.222;Initial Catalog=MEAL;User Id=sa; Password=sa;Pooling=false"/>
</appSettings>
<connectionStrings>
     <add name="HomepageMain_topCode" connectionString="00101" />
     <add name="HomepageMain_topName" connectionString="营养指导" />
    
    
 </connectionStrings>

求问一下获取连接字符串该怎么写
        SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());

我这样写是错误的,求知道,求专业网站 --------------------编程问答--------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Asiastar.NRDAL
{
    public class PubConstant
    {
        /// <summary>
        /// 获取连接字符串
        /// </summary>
        public static string ConnectionString
        {
            get
            {
                string _connectionString = ConfigurationManager.AppSettings["ConnectionString"];
                string ConStringEncrypt = ConfigurationManager.AppSettings["ConStringEncrypt"];
                return _connectionString;
            }
        }

        /// <summary>
        /// 得到web.config里配置项的数据库连接字符串。
        /// </summary>
        /// <param name="configName"></param>
        /// <returns></returns>
        public static string GetConnectionString(string configName)
        {
            string connectionString = ConfigurationManager.AppSettings[configName];
            string ConStringEncrypt = ConfigurationManager.AppSettings["ConStringEncrypt"];
            return connectionString;
        }
    }
}

--------------------编程问答-------------------- ConfigurationManager.ConnectionStrings["HomepageMain_topCode"] --------------------编程问答-------------------- string s = ConfigurationManager.AppSettings["ConnectionString"].ToString();
没问题
appSettings在configuration下 --------------------编程问答-------------------- http://www.connectionstrings.com

你用的是oledb数据驱动啊,把数据驱动Provider=sqloledb去掉吧。 --------------------编程问答--------------------
ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString
--------------------编程问答-------------------- //Database connection strings
          public static readonly string SQLConnString = ConfigurationManager.ConnectionStrings["SQLConnString"].ConnectionString;
    
            // Hashtable to store cached parameters
            private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());

            /// <summary>
            /// Execute a SqlCommand (that returns no resultset) against the database specified in the connection string 
            /// using the provided parameters.
            /// </summary>
            /// <remarks>
            /// e.g.:  
            ///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
            /// </remarks>
            /// <param name="connectionString">a valid connection string for a SqlConnection</param>
            /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
            /// <param name="commandText">the stored procedure name or T-SQL command</param>
            /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
            /// <returns>an int representing the number of rows affected by the command</returns>
            public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
            {

                SqlCommand cmd = new SqlCommand();

                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
                    int val = cmd.ExecuteNonQuery();
                    cmd.Parameters.Clear();
                    return val;
                }
            }
--------------------编程问答--------------------
引用 4 楼 dalmeeme 的回复:
http://www.connectionstrings.com

你用的是oledb数据驱动啊,把数据驱动Provider=sqloledb去掉吧。

不能去掉的,去掉的话就会报错,整个网站就登录不进去,说是什么未加载sqloledb 程序,
我想着肯定是获取连接出问题了。 --------------------编程问答-------------------- 学习。。。。。。。。。。。 --------------------编程问答-------------------- 2.0方式

<connectionStrings>
    <add name="connectionString2.0" connectionString="Data Source=.;DataBase=myblog;uid=sa;pwd=;" />
  </connectionStrings>


public static string connstr = ConfigurationManager.ConnectionStrings["connectionString2.0"].ConnectionString;
--------------------编程问答--------------------

////1.0方式
        //private static string connectionString = System.Configuration.ConfigurationManager.AppSettings["connectionString1.0"].ToString();
--------------------编程问答-------------------- 关键是他有个Provider=sqloledb,如果去掉 传统的连接字符串就可以运行了, 不去掉的话,就会报   
不支持关键字Provider的错误。 --------------------编程问答-------------------- 建议去下个微软官方DBHelper看看。

 /// <summary>
    /// 获取连接字符串
    /// </summary>
    public static string connectionString
    {
        get
        {
            string _connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            return _connectionString;
        }
    }
--------------------编程问答-------------------- 以上大家写的都是正确滴 我就不再重复了  --------------------编程问答-------------------- string _connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
--------------------编程问答-------------------- 这个要发帖坐等么?百度谷歌吧,伤不起啊…… --------------------编程问答-------------------- string _connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; --------------------编程问答-------------------- ConfigurationManager.ConnectionStrings["HomepageMain_topCode"] --------------------编程问答-------------------- 你的看上去是没有问题的 --------------------编程问答-------------------- Data Source=10.10.0.222
是对的、?
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,