[使用C#连接数据库

问题描述 投票:0回答:1

我正在尝试从数据库中获取数据,并且始终遇到下一个错误:

没有控制权-System.NullReferenceException HResult = -2147467261邮件=参考书目Objeto。来源= ImeApps StackTrace:zh-cn ComunCD.getConexionIRATI()zh-cn C:\ Desarrollo \ VisualStudio2010 \ ImeApps \ ImeApps \ ComunCD.cs:lneazh_cn EmpresasCD.getEmpresas()zh-cn C:\ Desarrollo \ VisualStudio2010 \ ImeApps \ ImeApps \ EmpresasCD.cs:lneazh-cn ImeApps.Principal.abrirSeleccionEmpresaEspera()zh-cn C:\ Desarrollo \ VisualStudio2010 \ ImeApps \ ImeApps \ Principal.cs:linea 92zh-cn ImeApps.Principal.abrirSeleccionEmpresa()zh-cn C:\ Desarrollo \ VisualStudio2010 \ ImeApps \ ImeApps \ Principal.cs:lineazh_cn ImeApps.Principal.menuItem4_Click(Object sender,EventArgs e)zh_cnC:\ Desarrollo \ VisualStudio2010 \ ImeApps \ ImeApps \ Principal.cs:línea77zh-cn System.Windows.Forms.ToolStripItem.RaiseEvent(对象键,EventArgs e)zh-cn System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)zh-cn System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)zh-cn System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)zh-CN System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e,符合ToolStripItemEventType)zh-cn System.Windows.Forms.ToolStripItem.FireEvent(符合EventArgs e,ToolStripItemEventType)zh-cn System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)zh-cn System.Windows.Forms.Control.WmMouseUp(消息和m,MouseButtons按钮,Int32单击)zh-cn System.Windows.Forms.Control.WndProc(Message&m)zh-cn System.Windows.Forms.ScrollableControl.WndProc(Message&m)zh-cn System.Windows.Forms.ToolStrip.WndProc(Message&m)zh-cn System.Windows.Forms.MenuStrip.WndProc(Message&m)zh-cn System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)zh-cn System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)zh-cn System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)zh-cn System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&msg)zh-cn System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtrdwComponentID,Int32原因,Int32 pvLoopData)zh-cn System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32原因,ApplicationContext上下文)zh-cn System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32原因,ApplicationContext上下文)zh-cn System.Windows.Forms.Application.Run(窗体mainForm)zh-cn ImeApps.Program.Main()zh-CN C:\ Desarrollo \ VisualStudio2010 \ ImeApps \ ImeApps \ Program.cs:línea16zh-cn System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String [] args)zh-cn System.AppDomain.nExecuteAssembly(RuntimeAssembly程序集,String [] args)zh-cn System.Runtime.Hosting.ManifestRunner.Run(布尔checkAptModel)zh-cn System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()zh-cn System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContextActivationContext,String [] activationCustomData)zh-cn System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContextActivationContext)zh-cn System.Activator.CreateInstance(ActivationContext activationContext)zh-cn Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()zh-cn System.Threading.ThreadHelper.ThreadStart_Context(对象状态)zh-cn System.Threading.ExecutionContext.RunInternal(ExecutionContextexecutionContext,ContextCallback回调,对象状态,布尔值reserveSyncCtx)zh-cn System.Threading.ExecutionContext.Run(ExecutionContext executeContext,ContextCallback回调,对象状态,布尔reserveSyncCtx)zh-cn System.Threading.ExecutionContext.Run(ExecutionContext执行上下文,ContextCallback回调,对象状态)zh-cn System.Threading.ThreadHelper.ThreadStart()InnerException:

我正在做的是以下事情:

public class ComunCD
    {
    private static SqlConnection _sqlConnectionIRATI;

    public static void conectarBDIRATI()
        {
            _sqlConnectionIRATI = new SqlConnection(ConfigurationSettings.AppSettings["CONEXION_IRATI"].ToString());
            _sqlConnectionIRATI.Open();
        }

    public static SqlConnection getConexionIRATI()
    {
        if (_sqlConnectionIRATI == null)
        {
            //conectarBDIRATI();
        }
        if (_sqlConnectionIRATI.State != ConnectionState.Open) // HERE I GET THE ERROR
        {
            _sqlConnectionIRATI.Open();
        }
        return _sqlConnectionIRATI;
    }

我可能做错了什么?预先感谢!

c# sql database sqlconnection
1个回答
1
投票

如果_sqlConnectionIRATInull,则不应测试State:添加else

    if (_sqlConnectionIRATI == null)
    {
        //conectarBDIRATI();
    }
    else if (_sqlConnectionIRATI.State != ConnectionState.Open) 
    {
        _sqlConnectionIRATI.Open();
    }

或者您应该创建实例:

    // If _sqlConnectionIRATI is null, we create it 
    if (_sqlConnectionIRATI == null)
      conectarBDIRATI();

    // _sqlConnectionIRATI is guaranteed to be not null: 
    if (_sqlConnectionIRATI.State != ConnectionState.Open)  
      _sqlConnectionIRATI.Open();
© www.soinside.com 2019 - 2024. All rights reserved.