Code Bye

ConnectionString 属性尚未初始化问题

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.Common;
namespace Application
{
    class Program
    {
        static void Main(string[] args)
        {
            var name = Console.ReadLine(); 
            var name2 = string.Empty;
            //Console.WriteLine("Hello {0} and {1} and ",name2,name);
            Console.WriteLine("Hello"+"  "+name);
            SqlConnection con = new SqlConnection();
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings.Get("SchoolConnectionString");
            string sqlstr = "select * from bfEMP";
            SqlCommand cmd = new SqlCommand(sqlstr, con);
            cmd.Connection = con;
            con.Open();
            var Num =cmd.ExecuteNonQuery();
            con.Close();
           // Console.WriteLine(Num); 
        }
    }
}



这个ConnectionString 字符串明明写了 怎么调试的时候没数据呀?求高手,本人菜鸟一枚,请详细帮忙解释一下

解决方案

10

断点在去appconfig值那里,估计没取到值

20

ConfigurationManager.ConnectionStrings

你这是配置在ConnectionStrings节点的,不是AppSettings节点

10

 con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings[“SchoolConnectionString”].ConnectionString;

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明ConnectionString 属性尚未初始化问题