简体   繁体   English

Combobox动态添加行并从SQL Server存储过程下拉

[英]Combobox add rows dynamically and dropdown from SQL Server stored procedure

This is basically a search tool. 这基本上是一种搜索工具。 When I type some thing in a combobox, the combobox drops down and will show me suggestions (something like Google search bar) 当我在组合框中输入一些东西时,组合框会下降并向我显示建议(类似Google搜索栏)

I created a procedure which does some complex calculations, which take one parameter and returns some rows. 我创建了一个执行一些复杂计算的过程,它接受一个参数并返回一些行。 Then I created a combobox event (On Update Text). 然后我创建了一个组合框事件(On Update Text)。

And in the event handler I wrote this code: 在事件处理程序中我写了这段代码:

private void combobox_TextUpdate(object sender, EventArgs e)
{
    this.combobox.Items.Clear();
    DataTable List = new DataTable();
    if (this.combobox.Text.Length > 0)
    {
        List = searchIt(combobox.text);
        foreach (DataRow Row in List.Rows)
        {
            this.combobox.Items.Add(Row.ItemArray.GetValue(0).ToString());
        }
        this.combobox.DroppedDown = true;
    }
}

static public DataTable searchIt(string STR)
{
    string connectionString = McFarlaneIndustriesPOSnamespace.Properties.Settings.Default.McFarlane_IndustriesConnectionString;
    SqlConnection con = new SqlConnection(connectionString);
    DataTable DT = new DataTable();
    con.Open();
    SqlDataAdapter DA = new SqlDataAdapter("USE [McFarlane Industries] " +
                                               "EXEC search " + 
                                                STR, connectionString);
    DA.Fill(DT);
    con.Close();
    return DT;
}

The function searchIt executes the stored procedure and it returns a DataTable . 函数searchIt执行存储过程并返回DataTable The stored procedure is working fine in SQL Server Management Studio. 存储过程在SQL Server Management Studio中正常运行。

But in the application it is not working correctly in some cases. 但在应用程序中,它在某些情况下无法正常工作。

When I type [space] , then it throws an exception and it says stored procedure needs parameter which is not provided. 当我输入[space]时 ,它会抛出一个异常,它表示存储过程需要未提供的参数。

There are many other characters when I type them it throws exception that invalid character at end of string "my string". 当我输入它们时还有许多其他字符,它会抛出字符串“my string”末尾的无效字符异常。

Any suggestion how could I achieve my goal. 任何建议我怎样才能实现我的目标。

Call your stored procedure with sqlcommand to fill your datatable 使用sqlcommand调用存储过程以填充数据表

using (SqlConnection scn = new SqlConnection(connect)
{    
    SqlCommand spcmd = new SqlCommand("search", scn);

    spcmd.Parameters.Add("@blah", SqlDbType.VarChar, -1); //or SqlDbType.NVarChar

    spcmd.CommandType = System.Data.CommandType.StoredProcedure;

    using (SqlDataAdapter da = new SqlDataAdapter(spcmd)) 
    { 
        da.Fill(dt); 
    } 
}
static public DataTable searchIt(string STR)
{
    string connectionString =  McFarlaneIndustriesPOSnamespace.Properties.Settings.Default.McFarlane_IndustriesConnectionString;
    SqlConnection con = new SqlConnection(connectionString);
    DataTable DT = new DataTable();
    con.Open();
    SqlCommand command = new SqlCommand("Name_of_Your_Stored_Procedure",con);
    command.CommandType=CommandType.StoredProcedure;
    command.Parameters.Add(new SqlParameter("@parameter_name",SqlDbType.NVarChar));
    command.Parameters[0].Value="Your Value in this case STR";
    SqlDataAdapter DA = new SqlDataAdapter(command);
    DA.Fill(DT);
    con.Close();
    return DT;
}

Important : 'parameter_Name' and 'Name_of_Your_Stored_Procedure' should be replaced by yours which you have in database. 重要提示: 'parameter_Name'和'Name_of_Your_Stored_Procedure'应该替换为您在数据库中拥有的那些。 And value of parameter could be like "abc" (combox.Text) 参数值可以像“abc”(combox.Text)

Command and its type, its text are necessary. 命令及其类型,其文本是必要的。 Adding parameters depends upon your stored procedure. 添加参数取决于您的存储过程。 They can be 0,1 or more but once they are added their values must be given. 它们可以是0或更多但是一旦添加它们必须给出它们的值。 conn(connection) can be passed to new SqlCmmand() or new SqlDataAdapter() conn(连接)可以传递给新的SqlCmmand()或新的SqlDataAdapter()

No need of things like 'use' and 'exec' 不需要'使用'和'exec'之类的东西

Following me and this link might be helpful in future for stored procedures http://www.codeproject.com/Articles/15403/Calling-Stored-procedures-in-ADO-NET 关注我和这个链接将来可能对存储过程有帮助http://www.codeproject.com/Articles/15403/Calling-Stored-procedures-in-ADO-NET

Two optional Suggestions for you 两个可选的建议

  1. use variable name 'list' instead of 'List' (you used) however you will not get problem with this name until you add a namespace using System.Collections.Generic; 使用变量名'list'而不是'List'(您使用过),但是在使用System.Collections.Generic添加名称空间之前,您不会遇到此名称的问题。 but you may need to use this namespace in future. 但是您将来可能需要使用此命名空间。

  2. Use only list.Rows[0].ToString(); 仅使用list.Rows [0] .ToString(); no need to get itemarray then get value when you are working with data in strings; 在处理字符串中的数据时,不需要获取itemarray然后获取值;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM