0

我正在尝试使用数据库中的值自动完成文本框。当我进行子弹测试时,代码将一直运行,直到if下面函数中的语句。有人可以告诉我为什么我的代码在那之后不会执行if吗?

我正在尝试运行以下命令,但它一直在之前跳出if

public AutoCompleteStringCollection AutoCompleate(string dataBase, string procedure)
{
        AutoCompleteStringCollection namesCollection =
            new AutoCompleteStringCollection();

        SqlDataReader dReader;
        SqlCommand cmd = new SqlCommand(procedure, con);
        cmd.CommandType = CommandType.StoredProcedure;
        con.Open();
        dReader = cmd.ExecuteReader(); // This is the last place my bullet check 
                                       // gets before it hop's out!
        if (dReader.HasRows == true)
        {
            while (dReader.Read())
                namesCollection.Add(dReader["SystemUser"].ToString());
        }
        con.Close();
        dReader.Close();
        return namesCollection;
    }

附加信息,但请关注以上内容!


请询问您是否需要任何信息。:)


对上述的呼吁:

textBoxUser.AutoCompleteMode = AutoCompleteMode.Suggest;
textBoxUser.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBoxUser.AutoCompleteCustomSource = 
    DatabaseService.Instance.AutoCompleate("AuditIT", "AutoCompleate");

存储过程:

USE [AuditIT]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--Allow textboxs to have a autocomplete
 ALTER procedure [dbo].[AutoCompleate] 
 as

 Select distinct SystemUser from SchemaAudit
             order by SystemUser asc
4

1 回答 1

3

尝试访问数据库时出现未处理的异常。尝试将代码重构为:

try{

if (dataBase.Length > 0) { procedure = dataBase + ".." + procedure; } //Set procedure to DBNAME..ProcedureName

SqlDataReader dReader;
            SqlCommand cmd = new SqlCommand(procedure, con);
            cmd.CommandType = CommandType.StoredProcedure;
            con.Open();
            dReader = cmd.ExecuteReader(); // This is the last place my bullet check gets before it hop's out!
            if (dReader.HasRows == true)
            {
                while (dReader.Read())
                    namesCollection.Add(dReader["SystemUser"].ToString());
            }
            con.Close();
            dReader.Close();
}
catch(Exception e)
{
    // handle the exception better than me :)
    Console.WriteLine(e.Message);
}

在 console.WriteLine 上放置一个消息框或断点,看看会发生什么。

于 2012-06-15T08:48:41.380 回答