Windows 2008 server+iis7 发布asp.net网站ODBC连接数据库,调用ODBC无法连接数据库

时间:2022-08-22 21:53:36
客户要求把应用移植到新的环境中。原来的环境是windows 2003 server +iis6 +sqlserver2008。数据库和应用都在一台server上。server上创建了odbc数据源,即系统DSN。程序运行没有任何问题。

现在把程序移植到新的环境中,windows 2008 server + iis7 ,远程访问数据库sqlserver 2012.在应用即IIS7所在的服务器备置了ODBC 并测试连接成功,部署完应用后,返回如图所示错误。

Server Error in '/' Application.
--------------------------------------------------------------------------------

Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   TrackingTool.databaseOpr.selectDatabase(String str) in E:\TrackingTool2\rbs\TrackingTool\TrackingTool\databaseOpr.cs:100
   TrackingTool.LoginCheck.loginTimeCheck() in E:\TrackingTool2\rbs\TrackingTool\TrackingTool\LoginCheck.cs:42
   TrackingTool.Login.loginCt1_LoggingIn(Object sender, LoginCancelEventArgs e) in E:\TrackingTool2\rbs\TrackingTool\TrackingTool\Login.aspx.cs:30
   System.Web.UI.WebControls.Login.OnLoggingIn(LoginCancelEventArgs e) +108
   System.Web.UI.WebControls.Login.AttemptLogin() +76
   System.Web.UI.WebControls.Login.OnBubbleEvent(Object source, EventArgs e) +101
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +166
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.5466; ASP.NET Version:2.0.50727.5456 





web.config 配置




<appSettings>
     <add key="CrystalImageCleaner-AutoStart" value="true" />
     <add key="CrystalImageCleaner-Sleep" value="60000" />
     <add key="CrystalImageCleaner-Age" value="120000" />
 </appSettings>
<connectionStrings>
<!---  下面注释 掉的连接字符串是用sqlserver id方式连接,可正常连接上数据库--->
<!--<add name="ConnStr" connectionString="Data Source=***;Initial Catalog=TrackingTool;Persist Security Info=True;User ID=****;Password=****"
   providerName="System.Data.SqlClient" />-->
<!--- 下面是调用odbc的连接字符串,创建odbc测试能连接上数据库。但此处调用却无法连接上数据库。但是在windows 2003上可以--->
                <add connectionString="Data Source=appconnection;Dsn=appconnection;" name="ConnStr" />
</connectionStrings>

  
<system.web>
    
<!-- 
            设置 compilation debug="true" 可将调试符号插入
            已编译的页面中。但由于这会 
            影响性能,因此只在开发过程中将此值 
            设置为 true。
        -->


返回的错误是操作数据库异常。。。即数据库返回的空的对象 ,没有连接上数据库。。。。。。求大虾们帮忙解决。。。在线等====== 谢谢  。。。

此类错误关于iis7 的配置 我已经按照网上类似的错误修改过,启用32位程序的集成 ,iis运行32位程序,启用父类神马的,还有各种权限的设置都试过了。。。但是还不行 。。。。。。

3 个解决方案

#1


<add connectionString="Data Source=.;Dsn=appconnection;" name="ConnStr" /> 

上面的连接字符窜。Data Source是数据源地址。Dsn是ODBC名字。这样的配置在windows server 2003+iis6上没有问题 。在现在的windows server 2008 R2 + IIS7上就出现上述的错误。

#2



using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text;

namespace TrackingTool
{
    public class databaseOpr
    {
        public SqlCommand cmd;
        public SqlConnection objConnection;
        public SqlTransaction trans;
        public SqlDataAdapter objDataAdapter;

        public databaseOpr()
        {
            
        }
        /// <summary>
        /// 操作数据库
        /// </summary>
        public void updateDatabase(string str)
        {
            try
            {
                string strConnection = System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
                objConnection = new SqlConnection(strConnection);
                int x = objConnection.ConnectionTimeout;
                objConnection.Open();

                trans = objConnection.BeginTransaction();

                cmd = objConnection.CreateCommand();
                cmd.CommandTimeout = 100000;
                cmd.Transaction = trans;
                cmd.CommandText = str;
                cmd.ExecuteNonQuery();
                cmd.Dispose();

                trans.Commit();

                objConnection.Close();
            }
            catch (Exception ex)
            {
                cmd.Dispose();
                objConnection.Close();
                throw new Exception(ex.Message.ToString().Trim());
            }
        }

        /// <summary>
        /// 查询数据库,返回DataAdapter
        /// </summary>
        public SqlDataAdapter selectDatabase(string str)
        {
            try
            {
                string strConnection = System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
                objConnection = new SqlConnection(strConnection);
                int x = objConnection.ConnectionTimeout;
                objConnection.Open();

                trans = objConnection.BeginTransaction();

                cmd = objConnection.CreateCommand();
                cmd.CommandTimeout = 100000;
                cmd.Transaction = trans;
                cmd.CommandText = str;
                cmd.ExecuteNonQuery();
                cmd.Dispose();

                trans.Commit();

                objConnection.Close();

                objDataAdapter = new SqlDataAdapter(cmd.CommandText, objConnection);

                return objDataAdapter;
            }
            catch (Exception ex)
            {
                cmd.Dispose();
                objConnection.Close();
                throw new Exception(ex.Message.ToString().Trim());
                //return null;
            }
        }
    }
}

#3



using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text;
namespace TrackingTool
{
    public class databaseOpr
    {
        public OdbcCommand cmd;
        public OdbcConnection objConnection;
        public OdbcTransaction trans;
        public OdbcDataAdapter objDataAdapter;

        public databaseOpr()
        {

        }
        /// <summary>
        /// 操作数据库
        /// </summary>
        public void updateDatabase(string str)
        {
            try
            {
                string strConnection = System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
                objConnection = new OdbcConnection(strConnection);
                int x = objConnection.ConnectionTimeout;
                objConnection.Open();

                trans = objConnection.BeginTransaction();

                cmd = objConnection.CreateCommand();
                cmd.CommandTimeout = 100000;
                cmd.Transaction = trans;
                cmd.CommandText = str;
                cmd.ExecuteNonQuery();
                cmd.Dispose();

                trans.Commit();

                objConnection.Close();
            }
            catch (Exception ex)
            {
                cmd.Dispose();
                objConnection.Close();
                throw new Exception(ex.Message.ToString().Trim());
            }
        }

        /// <summary>
        /// 查询数据库,返回DataAdapter
        /// </summary>
        public OdbcDataAdapter selectDatabase(string str)
        {
            try
            {
                string strConnection = System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
                objConnection = new OdbcConnection(strConnection);
                int x = objConnection.ConnectionTimeout;
                objConnection.Open();

                trans = objConnection.BeginTransaction();

                cmd = objConnection.CreateCommand();
                cmd.CommandTimeout = 100000;
                cmd.Transaction = trans;
                cmd.CommandText = str;
                cmd.ExecuteNonQuery();
                cmd.Dispose();

                trans.Commit();

                objConnection.Close();

                objDataAdapter = new OdbcDataAdapter(str, objConnection);
                return objDataAdapter;
            }
            catch (Exception ex)
            {
                cmd.Dispose();
                objConnection.Close();
                throw new Exception(ex.Message.ToString().Trim());
                //return null;
            }
            finally
            {

                cmd.Dispose();
                objConnection.Close();
            }
        }
    }
}

#1


<add connectionString="Data Source=.;Dsn=appconnection;" name="ConnStr" /> 

上面的连接字符窜。Data Source是数据源地址。Dsn是ODBC名字。这样的配置在windows server 2003+iis6上没有问题 。在现在的windows server 2008 R2 + IIS7上就出现上述的错误。

#2



using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text;

namespace TrackingTool
{
    public class databaseOpr
    {
        public SqlCommand cmd;
        public SqlConnection objConnection;
        public SqlTransaction trans;
        public SqlDataAdapter objDataAdapter;

        public databaseOpr()
        {
            
        }
        /// <summary>
        /// 操作数据库
        /// </summary>
        public void updateDatabase(string str)
        {
            try
            {
                string strConnection = System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
                objConnection = new SqlConnection(strConnection);
                int x = objConnection.ConnectionTimeout;
                objConnection.Open();

                trans = objConnection.BeginTransaction();

                cmd = objConnection.CreateCommand();
                cmd.CommandTimeout = 100000;
                cmd.Transaction = trans;
                cmd.CommandText = str;
                cmd.ExecuteNonQuery();
                cmd.Dispose();

                trans.Commit();

                objConnection.Close();
            }
            catch (Exception ex)
            {
                cmd.Dispose();
                objConnection.Close();
                throw new Exception(ex.Message.ToString().Trim());
            }
        }

        /// <summary>
        /// 查询数据库,返回DataAdapter
        /// </summary>
        public SqlDataAdapter selectDatabase(string str)
        {
            try
            {
                string strConnection = System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
                objConnection = new SqlConnection(strConnection);
                int x = objConnection.ConnectionTimeout;
                objConnection.Open();

                trans = objConnection.BeginTransaction();

                cmd = objConnection.CreateCommand();
                cmd.CommandTimeout = 100000;
                cmd.Transaction = trans;
                cmd.CommandText = str;
                cmd.ExecuteNonQuery();
                cmd.Dispose();

                trans.Commit();

                objConnection.Close();

                objDataAdapter = new SqlDataAdapter(cmd.CommandText, objConnection);

                return objDataAdapter;
            }
            catch (Exception ex)
            {
                cmd.Dispose();
                objConnection.Close();
                throw new Exception(ex.Message.ToString().Trim());
                //return null;
            }
        }
    }
}

#3



using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text;
namespace TrackingTool
{
    public class databaseOpr
    {
        public OdbcCommand cmd;
        public OdbcConnection objConnection;
        public OdbcTransaction trans;
        public OdbcDataAdapter objDataAdapter;

        public databaseOpr()
        {

        }
        /// <summary>
        /// 操作数据库
        /// </summary>
        public void updateDatabase(string str)
        {
            try
            {
                string strConnection = System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
                objConnection = new OdbcConnection(strConnection);
                int x = objConnection.ConnectionTimeout;
                objConnection.Open();

                trans = objConnection.BeginTransaction();

                cmd = objConnection.CreateCommand();
                cmd.CommandTimeout = 100000;
                cmd.Transaction = trans;
                cmd.CommandText = str;
                cmd.ExecuteNonQuery();
                cmd.Dispose();

                trans.Commit();

                objConnection.Close();
            }
            catch (Exception ex)
            {
                cmd.Dispose();
                objConnection.Close();
                throw new Exception(ex.Message.ToString().Trim());
            }
        }

        /// <summary>
        /// 查询数据库,返回DataAdapter
        /// </summary>
        public OdbcDataAdapter selectDatabase(string str)
        {
            try
            {
                string strConnection = System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
                objConnection = new OdbcConnection(strConnection);
                int x = objConnection.ConnectionTimeout;
                objConnection.Open();

                trans = objConnection.BeginTransaction();

                cmd = objConnection.CreateCommand();
                cmd.CommandTimeout = 100000;
                cmd.Transaction = trans;
                cmd.CommandText = str;
                cmd.ExecuteNonQuery();
                cmd.Dispose();

                trans.Commit();

                objConnection.Close();

                objDataAdapter = new OdbcDataAdapter(str, objConnection);
                return objDataAdapter;
            }
            catch (Exception ex)
            {
                cmd.Dispose();
                objConnection.Close();
                throw new Exception(ex.Message.ToString().Trim());
                //return null;
            }
            finally
            {

                cmd.Dispose();
                objConnection.Close();
            }
        }
    }
}