在c#中使用autocompleteextender web服务

时间:2011-10-24 09:22:55

标签: c# web-services autocomplete

  

这是vb正常运行的webservice函数代码

` Public Function GetProducts(ByVal prefixText As String, _
                                  ByVal count As Integer) As String()
        Dim SelectQry = "select * from employee where Ename like '" & prefixText & "%'"
        Dim Results As New ArrayList
        Try
            Using Command As New SqlCommand(SelectQry, Connection)
                Using Reader As SqlDataReader = Command.ExecuteReader()
                    Dim Counter As Integer
                    While Reader.Read
                        If (Counter = count) Then Exit While
                        Results.Add(Reader("Ename").ToString())
                        Counter += 1
                    End While
                End Using
                Dim ResultsArray(Results.Count - 1) As String
                ResultsArray = Results.ToArray(GetType(System.String))
                Return ResultsArray
            End Using
        Catch ex As Exception
            Throw ex
        End Try
    End Function`
  

在Csharp中,我转换了这个vb编码..但是没有工作..我发现在将字符串数组转换为字符串时出现了一些错误..近行resultarray(results.count-1)

 [WebMethod]
    public string[] GetProducts(string prefixText,int count)
    {
        SqlConnection con = new

       SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\WebSite3\\App_Data\\Country.mdf;Integrated Security=True;User Instance=True");

        con.Open();
        ArrayList results = new ArrayList();
        string strQuery = "select * from employee where Ename like '" + prefixText + "%'";
        SqlCommand cmd = new SqlCommand(strQuery, con);
        SqlDataReader dr = cmd.ExecuteReader();
        int Counter;
        while (dr.Read())
        {
            if (Counter == count)
                break;
            results.Add(dr["Ename"].ToString());
            Counter++;
        }
        string resultsarray;
        resultsarray(results .count -1);
        resultsarray =results.ToArray (GetType (System.String ));
        return resultsarray ;
}
  

这是我的Csharp编码在这里我发现resultarray中的错误.last 4行显示一些错误,如方法'Gettype'没有重载,并且不能隐式转换类型字符串到字符串[]。

2 个答案:

答案 0 :(得分:0)

更改您的代码,如下所示:

string[] resultsArray = (string[])results.ToArray(typeof(System.String));
return resultsArray;

您可以从下面的链接将代码从Vb.Net转换为C#:

http://www.developerfusion.com/tools/convert/vb-to-csharp/

答案 1 :(得分:0)