我正在尝试使用数据库中的值自动完成textBox。当我进行子弹测试时,代码将一直走到下面函数中的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
答案 0 :(得分: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上放置一个消息框或断点,看看发生了什么。