ConnectionString 属性尚未初始化。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidOperationException: ConnectionString 属性尚未初始化。
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using System.Text;namespace DataAccess
{
public class BaseAccess
{
string conStr = ConfigurationSettings.AppSettings["databaseString"];
string fileUpPath = ConfigurationSettings.AppSettings["FileUpPath"];
//返回字符串
protected string GetSingleValue(string sqlStr,bool IsProc,params SqlParameter[] param)
{
SqlConnection con = new SqlConnection(this.conStr);
SqlCommand cmd = new SqlCommand(sqlStr,con);
if (IsProc)
{
cmd.CommandType = CommandType.StoredProcedure;
}
foreach (SqlParameter p in param)
{
cmd.Parameters.Add(p);
}
try
{
con.Open();
return cmd.ExecuteScalar().ToString();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
} //返回DATASET
protected DataSet GetDataByDS(string sqlStr, string tableName, bool IsProc, params SqlParameter[] param)
{
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand(sqlStr,con);
if (IsProc)
{
cmd.CommandType = CommandType.StoredProcedure;
}
foreach (SqlParameter p in param)
{
cmd.Parameters.Add(p);
}
DataSet ds = new DataSet();
try
{
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(ds,tableName);
return ds;
}
catch (Exception ex)
{
throw ex;//这里报错
}
finally
{
con.Close();
}
}
//返回DATASET
protected DataSet GetMultiTable(string[] sqlStr, string[] tableName, bool IsProc, params SqlParameter[] param)
{
SqlConnection con = new SqlConnection(this.conStr);
SqlCommand cmd1 = new SqlCommand(sqlStr[0], con);
SqlCommand cmd2 = new SqlCommand(sqlStr[1], con);
if (IsProc)
{
cmd1.CommandType = CommandType.StoredProcedure;
cmd2.CommandType = CommandType.StoredProcedure;
}
int num = 0;
foreach (SqlParameter p in param)
{
if (num < 1)
{
cmd1.Parameters.Add(p);
}
else
{
cmd2.Parameters.Add(p);
}
num++;
}
DataSet ds = new DataSet();
try
{
SqlDataAdapter adapter1 = new SqlDataAdapter(cmd1);
SqlDataAdapter adapter2 = new SqlDataAdapter(cmd2);
adapter1.Fill(ds,tableName[0]);
adapter2.Fill(ds,tableName[1]);
return ds;
}
catch (Exception ex)
{
throw ex;
}
}
//执行SQL语句
protected int ExecuteSql(string sqlStr, bool IsProc, params SqlParameter[] param)
{
SqlConnection con = new SqlConnection(this.conStr);
SqlCommand cmd = new SqlCommand(sqlStr, con);
if (IsProc)
{
cmd.CommandType = CommandType.StoredProcedure;
}
foreach (SqlParameter p in param)
{
cmd.Parameters.Add(p);
}
try
{
con.Open();
return cmd.ExecuteNonQuery();
//return cmd.ExecuteScalar().ToString();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
//返回执行的后的最大ID
protected int GetTopId(string TableName) {
SqlConnection con = new SqlConnection(this.conStr);
string sqlStr="select @@indentity as id from "+TableName;
SqlCommand cmd=new SqlCommand(sqlStr,con);
int id =Convert.ToInt32(cmd.ExecuteScalar());
return id;
}
}
}
app.config代码
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections> <appSettings>
<add key="databaseString" value="Data Source=156.1.5.225;Initial Catalog=changweiban;Persist Security Info=True;User ID=sa;Password=jcb1311"/>
<add key="FileUpPath" value="./"/>
</appSettings>
</configuration>
解决方案 »
- 在线等回答
- dotnetcharting求助
- c# 一对一聊天器 未将对象引用设置到对象的实例问题
- 求将时间00:00:00转换为字符串"00:00:00"的方法
- listbox的dispaymember
- 急急!!打开IE自动进入http://4-counter.com/,而且自动增加收藏夹链接位置,修改注册表无效,将注册表恢复还是没有用
- .net StreamReader.ReadToEnd()方法在读取internet资源时会被阻塞的解决方法
- 求大神,如何用C#如何向c++非托管dll函数传递数组或是指针
- 请问,C#.net如何访问xml,在线等待
- 请教:如何在UserControl中提供一个OnClosing事件
- 各路大侠来帮忙
- 我想学C#!!!
那要用web.config
<appSettings>
<add key="databaseString" value="Data Source=156.1.5.225;Initial Catalog=changweiban;Persist Security Info=True;User ID=sa;Password=jcb1311"/>
<add key="FileUpPath" value="./"/>
</appSettings>这段加到Web.config文件中ConfigurationSettings引用的是Web项目下的Web.config
而不是被引用的项目的App.config
System.Configuration.ConfigurationManager.AppSettings["databaseString"];
con.Open();
放在
SqlCommand cmd = new SqlCommand(sqlStr,con);
前面
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="testTreeView.Properties.Settings.cwbase1ConnectionString"
connectionString="Data Source=.;Initial Catalog=cw;Persist Security Info=True;User ID=sa;Password=aaaaaa"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
调用
String connstring = global::testTreeView.Properties.Settings.Default.cwbase1ConnectionString;
SqlConnection conn = new SqlConnection(connstring);
<add name="ConnectionString" connectionString="" providerName="System.Data.SqlClient"/>
</connectionStrings>推荐这种写法
用
ConfigurationManager.AppSettings["databaseString"]