首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

【求解】连续输入几十次异常的数据后提示“未知的异常”,再输入正确数据的都显示“未知的异常”

2012-03-21 
【求解】连续输入几十次错误的数据后提示“未知的错误”,再输入正确数据的都显示“未知的错误”C# codeusing Sys

【求解】连续输入几十次错误的数据后提示“未知的错误”,再输入正确数据的都显示“未知的错误”

C# code
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.OleDb;using System.Data.SqlClient;namespace WindowsFormsApplication1{    public partial class IMEI查找工具 : Form    {        public IMEI查找工具()        {            InitializeComponent();        }                private const int WM_LBUTTONDOWN = 0x201;        private const int WM_LBUTTONUP = 0x202;        private void TextEnable(bool a_Enable)        {            textBox1.Enabled = a_Enable;            if (a_Enable)            {                textBox1.Focus();                textBox1.SelectAll();                textBox1.Text = "";                           }        }        private void label1_Click(object sender, EventArgs e)        {        }                private void button1_Click(object sender, EventArgs e)        {            try            {                               string strpath;                strpath = textBox2.Text;                OleDbConnection myConn = new OleDbConnection();                myConn.ConnectionString = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + strpath;                                myConn.Open();                label3.Text = "表" + strpath + "连接成功!";                label3.BackColor = Color.LawnGreen;                textBox2.Enabled = false;                           }            catch (Exception ex)            {                               label3.Text = ( ex.Message + "\n      FAIL");                label3.BackColor = Color.Red;            }        }        private void textBox1_TextChanged(object sender, EventArgs e)        {        }        private void textBox1_KeyPress(object sender, KeyPressEventArgs ex)        {            if (ex.KeyChar == 13)            {                               this.button2_Click(sender, ex);                            }                   }        private void button2_Click(object sender, EventArgs e)        {                        try            {                string a;                string strsql;                string IMEI;                               a = textBox1.Text;                int count;                              string strpath;                strpath = textBox2.Text;                OleDbConnection myConn = new OleDbConnection();                myConn.ConnectionString = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + strpath;                                myConn.Open();                strsql = "select * from ZTE_MBC_BARCODE_IMEI where IMEI='" + a + "'";                OleDbCommand mycmd = myConn.CreateCommand();                mycmd.CommandText = strsql;                OleDbDataReader mydr = mycmd.ExecuteReader();                mydr.Read();                                count = 0;                               while (count == 0)                {                    count += 1;                    IMEI = mydr["IMEI"].ToString();                                            label2.Text = "IMEI号:" + IMEI + ",\n            PASS";                        label2.BackColor = Color.LawnGreen;                                                             break;                }                               TextEnable(true);                textBox2.Enabled = false;                myConn.Close();            }            catch (Exception ex)            {                               label2.Text=("IMEI:" + textBox1.Text +","+"\n"+ ex.Message+"\n      FAIL");                label2.BackColor = Color.Red;                textBox1.Focus();                textBox1.SelectAll();            }        }        private void textBox2_TextChanged(object sender, EventArgs e)        {                             }                   }} 



这个软件是做的一个小软件,通过数据库里去调出IMEI号,核对扫描进去的IMEI号是否存在在数据库中。

在使用中发现,如果连续输入几十个错误的IMEI号都不在数据库里时,就提示“未知的错误”,并且后面再输正确的IMEI号都是提示“未知的错误”,点击链接正确的数据库,也是提示“未知的错误”。

请大侠们帮忙看看是哪里出现了问题。

是因为try……catch没有用 finally 的原因吗?

[解决办法]
那你关一下framework和vs从开后测试一下
[解决办法]
感觉 是你的数据库 myConn 连接开关的问题

还有你这里 好像最好重写
 count = 0;



while (count == 0)
{
count += 1;
IMEI = mydr["IMEI"].ToString();


label2.Text = "IMEI号:" + IMEI + ",\n PASS";
label2.BackColor = Color.LawnGreen;


break;

}

热点排行