我需要通过app.config
文件连接connect database,存储过程应该得到优先,结果应该存储在数据表中
App.config
档案:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Test"
connectionString="Data Source=servername; Initial Catalog=DBname; UserID=xx;password=xxx"
providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
C#
SqlConnection DB = new SqlConnection();
DB.ConnectionString=ConfigurationManager.ConnectionStrings["Test"].ConnectionString;
SqlCommand cmd = new SqlCommand("[SP_active_user_profiles]", DB);
cmd.CommandType = CommandType.StoredProcedure;
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dt);
我的问题是:我无法连接到数据库本身,它抛出空引用异常,并将存储过程结果存储在数据表中。
我的错误在哪里?
答案 0 :(得分:1)
像这样使用它:
var menus= MenuRepo.GetAll().Where(x => x.Name.Contains("license") == false)
答案 1 :(得分:1)
连接字符串应该与此<add name="Test"
connectionString="Data Source=servername;Initial Catalog=DBname;
User ID=xx;Password=xxx;Integrated Security=SSPI;
"
providerName="System.Data.SqlClient"/>