还是不行 帮我看看是什么原因代码 SqlConnection con; if (con.State==ConnectionState.closed) { open(); }private void Open() { // open connection if (con == null) { con = new SqlConnection(ConfigurationSettings.AppSettings["ConnString1"]);
con.Open(); } else { con.Open(); } } 出错代码 未将对象引用设置到对象的实例。 说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 行 108: { 行 109: // make sure connection is open 行 110: if (con.State==ConnectionState.Closed) 行 111: { 行 112: Open();
SqlConnection con; con = new SqlConnection(ConfigurationSettings.AppSettings["ConnString1"]); if (con.State==ConnectionState.closed) { con.open(); } 这样不就得了,干吗写个open函数啊。
SqlConnection con; 你没有实例化就在: if (con.State==ConnectionState.closed)中使用了这个对象改成这样: SqlConnection con; open();private void Open() { if (con.State!=ConnectionState.Closed) return; // open connection if (con == null) { con = new SqlConnection(ConfigurationSettings.AppSettings["ConnString1"]);
con.Open(); } else { con.Open(); } }
SqlConnection con; con = new SqlConnection(ConfigurationSettings.AppSettings["ConnString1"]); if (con.State==ConnectionState.closed) { open(); } private void Open() { // open connection if (con == null) {
SqlConnection con;
if (con.State==ConnectionState.closed)
{
open();
}private void Open()
{
// open connection
if (con == null)
{
con = new SqlConnection(ConfigurationSettings.AppSettings["ConnString1"]);
con.Open();
}
else
{
con.Open();
}
}
出错代码
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 108: {
行 109: // make sure connection is open
行 110: if (con.State==ConnectionState.Closed)
行 111: {
行 112: Open();
con = new SqlConnection(ConfigurationSettings.AppSettings["ConnString1"]);
if (con.State==ConnectionState.closed)
{
con.open();
}
这样不就得了,干吗写个open函数啊。
你没有实例化就在:
if (con.State==ConnectionState.closed)中使用了这个对象改成这样:
SqlConnection con;
open();private void Open()
{
if (con.State!=ConnectionState.Closed)
return;
// open connection
if (con == null)
{
con = new SqlConnection(ConfigurationSettings.AppSettings["ConnString1"]);
con.Open();
}
else
{
con.Open();
}
}
con = new SqlConnection(ConfigurationSettings.AppSettings["ConnString1"]);
if (con.State==ConnectionState.closed)
{
open();
}
private void Open()
{
// open connection
if (con == null)
{
con.Open();
}
else
{
con.Open();
}
}
我想应该先创建一个连接实例
这样可以吗?