如何处理异常处理?此代码显示错误SqlDataReader rs = cmd.ExecuteReader(); [英] How to deal with exception handling? this code shows error SqlDataReader rs = cmd.ExecuteReader();

查看:361
本文介绍了如何处理异常处理?此代码显示错误SqlDataReader rs = cmd.ExecuteReader();的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Windows.Forms;
使用 System.Data.SqlClient;
使用 System.Collections;

命名空间 AutoStatus
{


public partial class Form1:Form
{

bool AppAdded = false ;
SqlConnection conn = new SqlConnection( @ 数据源=(LocalDB)\v11.0;);

public Form1()
{
InitializeComponent();

getapp();
}

private void getapp()
{
SqlCommand cmd = new SqlCommand( SELECT app FROM mytable,conn);
this .conn.Open();
SqlDataReader rs = cmd.ExecuteReader();
ArrayList Authors = new ArrayList();

while (rs.Read())
{
Authors.Add( new AddValue(rs.GetString( 0 )));

}
rs.Close();
this .conn.Close();

this .comboBox2.DataSource = Authors;
.comboBox2.DisplayMember = app ;

AppAdded = true ;
}



private void getcomponents()
{
this .comboBox3.Items.Clear();
SqlCommand cmd = new SqlCommand( SELECT组件FROM mytable WHERE app = + this .comboBox2.SelectedValue,conn);
this .conn.Open();
SqlDataReader rs = cmd.ExecuteReader();
while (rs.Read())
{
this .comboBox3.Items.Add(rs.GetString( 2 ));
}
rs.Close();
this .conn.Close();

}

/ * private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add(new ComboBoxItem(item1 text,item1 value));
MessageBox.Show(((ComboBoxItem)comboBox1.Items [0])。Value);
/ *
字典comboSource = new Dictionary< string,>();
comboSource.Add(1,Sunday);
comboBox2.Items.Add(weekdays);
comboBox2.Items.Add(year); * /



private void comboBox2_SelectedIndexChanged( object sender,EventArgs e)
{
if this .AppAdded)
getcomponents();
}
}



public class AddValue
{
private string m_Display;

public AddValue( string 显示)
{
m_Display =显示;

}
public string 显示
{
get { return m_Display; }
}

}
}

解决方案

这可能有所帮助你



http://forums.asp.net/t /1097507.aspx [ ^ ]

1。在该行代码上引发的错误是什么?

2.参考下面的链接MSDN Link

 MSDN SqlCommand.ExecuteReader Method()

- 你可以捕获所有不同类型的异常(口袋妖怪)处理)

- 如果你不打算对特定错误做任何事情,不建议这样做。

3.替换

 SqlDataReader rs = cmd。 ExecuteReader(); 

以下

尝试
{
SqlDataReader rs = cmd.ExecuteReader();
}
catch(InvalidCastException异常)
{
Debug.WriteLine(exception.Message)
}
catch(SqlException异常)
{
Debug.WriteLine(exception.Message)
}
catch(InvalidOperationException异常)//这处理ObjectDisposedException
{
Debug.WriteLine(exception.Message)
}
catch(IOException异常)
{
Debug.WriteLine(exception.Message)
}
catch(异常异常)
{
Debug.WriteLine(exception.Message)
}





4.将其添加到页面顶部

使用System.Diagnostics; 





这将有助于确定异常类型和错误。


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;

namespace AutoStatus
{

    
    public partial class Form1 : Form
    {
        
        bool AppAdded=false;
        SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;");

        public Form1()
        {
            InitializeComponent();
            
            getapp();
        }

        private void getapp()
        {
            SqlCommand cmd = new SqlCommand("SELECT app FROM mytable ", conn);
            this.conn.Open();
            SqlDataReader rs = cmd.ExecuteReader();
            ArrayList Authors = new ArrayList();

            while (rs.Read())
            {
                Authors.Add(new AddValue(rs.GetString(0)));

            }
            rs.Close();
            this.conn.Close();

            this.comboBox2.DataSource = Authors;
            this.comboBox2.DisplayMember = "app";

            AppAdded = true;
        }



        private void getcomponents()
        {
            this.comboBox3.Items.Clear();
           SqlCommand cmd = new SqlCommand("SELECT components FROM mytable WHERE app =" + this.comboBox2.SelectedValue, conn);
            this.conn.Open();
            SqlDataReader rs = cmd.ExecuteReader();
            while (rs.Read())
            {
                this.comboBox3.Items.Add(rs.GetString(2));
            }
            rs.Close();
            this.conn.Close();

        }

        /*private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add(new ComboBoxItem("item1 text", "item1 value"));
            MessageBox.Show(((ComboBoxItem)comboBox1.Items[0]).Value);
            /*
            Dictionary comboSource = new Dictionary<string,>();
            comboSource.Add("1", "Sunday");
            comboBox2.Items.Add("weekdays");
            comboBox2.Items.Add("year");*/
        

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.AppAdded)
                getcomponents();
        }
    }

   

    public class AddValue
    {
        private string m_Display;
        
        public AddValue(string Display)
        {
            m_Display = Display;
            
        }
        public string Display
        {
            get { return m_Display; }
        }
      
    }
}

解决方案

Here is something that might help you

http://forums.asp.net/t/1097507.aspx[^]


1. What is the error that is thrown on that line of code?
2. With reference to the link MSDN Link below

MSDN SqlCommand.ExecuteReader Method ()

- You could catch all the different types of exceptions (Pokemon handling)
- This is not recommended if you are not going to do anything about the specific errors.
3. Replace

SqlDataReader rs = cmd.ExecuteReader();

with the following

try
            {
                SqlDataReader rs = cmd.ExecuteReader();
            }
            catch (InvalidCastException exception)
            {
                Debug.WriteLine(exception.Message)
            }
            catch (SqlException exception)
            {
                Debug.WriteLine(exception.Message)
            }
            catch (InvalidOperationException exception) // This handles ObjectDisposedException
            {
                Debug.WriteLine(exception.Message)
            }
            catch (IOException exception)
            {
                Debug.WriteLine(exception.Message)
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception.Message)
            }



4. Add this to the top of the page

using System.Diagnostics;



This will assist with determining what the exception type is and the error.


这篇关于如何处理异常处理?此代码显示错误SqlDataReader rs = cmd.ExecuteReader();的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆