简体   繁体   English

Webservice使用C#从数据库检索数据

[英]webservice to retrive data from a database using C#

**I have created a webservice to retrieve list of owners for a particular company code. **我创建了一个Web服务来检索特定公司代码的所有者列表。 When i run this webservice ...it is working fine . 当我运行此webservice时...工作正常。 Kindly help to me to execute this with a web application as when i am doing the same the result shown is "SYSTEM.TOSTRING[]" 请帮助我使用Web应用程序执行此操作,因为当我执行相同的操作时,显示的结果是“ SYSTEM.TOSTRING []”
THE web application consists of a text box for input for company code and a button to fetch ownername in a dropdownlist 该Web应用程序包括一个用于输入公司代码的文本框和一个用于在下拉列表中获取所有者名称的按钮。

 public class OwnerWebService : System.Web.Services.WebService
{     
             string ConnectionString = "Data Source=localdb" + ";" + "Initial Catalog=Prod" + ";" +
             "Persist Security Info=True" + ";" +
             "User ID=User1" + ";" +
             "Password=User1" + ";" +
             "enlist=false";

 [WebMethod]
    public string[] dtFetch(string strCompanyCode)
    {
        List<string> messages = new List<string>();
        SqlConnection cnMySQL = new SqlConnection(ConnectionString );
        cnMySQL.Open();
        string sqlquery = string.Format("SELECT OwnerName FROM tbl_Owner WHERE CompanyCode='{0}'", strCompanyCode);
        SqlCommand com = new SqlCommand(sqlquery, cnMySQL);
        SqlDataReader sqlReader = com.ExecuteReader();

        while (sqlReader.Read())
        {
            messages.Add(sqlReader.GetString(0));
        }


        cnMySQL.Close();
        return messages.ToArray();

    }

} }

do not return a string[] instead try to return a comma separated string. 不要返回string [],而是尝试返回逗号分隔的字符串。

for returning a comma separated string modify the last line from 为了返回逗号分隔的字符串,请从

return messages.ToArray();

to

return String.Join(',',messages.ToArray());

also change the return type to string Hope it helps... 还将返回类型更改为string希望对您有所帮助...

You cannot put a collection of strings (string[]) into one single textbox, which can show only one string. 您不能将一组字符串(string [])放到一个只能显示一个字符串的文本框中。

Your webservice is fine: it returns a collection of owner names. 您的网络服务很好:它返回所有者名称的集合。 So the user interfacce has to be modified. 因此,必须修改用户权限。

There are several solutions: 有几种解决方案:

  1. In your user interface, concatinate all strings into one string (String.Join(", ", messages)). 在用户界面中,将所有字符串合并为一个字符串(String.Join(“,”,消息))。 Do not do this in your webservice! 不要在您的Web服务中执行此操作! A webserivce should not be allowed to do UI-formatting! Webserivce不应进行UI格式化!
  2. In your user interface, replace the textbox by a listbox, show you can show multiple owner names in that listbox. 在用户界面中,将文本框替换为列表框,显示可以在该列表框中显示多个所有者名称。

One last (off-topic) suggestion: using the keyword "using" for your SqlConnection and SqlDataReader. 最后(离题)建议:为您的SqlConnection和SqlDataReader使用关键字“ using”。 This ensures that they will be closed, even if an exception occurs. 这样可以确保即使发生异常也将关闭它们。

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

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