1

I am trying to create a simple web service which basically takes input parameter A and then according to that parameter it returns result (from query with couple of tables joins) fields from different tables, how to capture that output to use it in rules while binding it with one infopath form field?

My code is below... here am getting them as string but want them as different fields...

public class Data
{
    //Create new method to get data from ** database
    public static List<string> GetData(string ORDNUM_10)
      //public struct GetData(string ORDNUM_10)      
    {
        string PMDES1_01 = "";
        string DESCRPTN_104 = "";
        string PRTNUM_10 = "";
        string ORDREF_10 = "";
        string TNXDTE_01 = "";
        //Create connection
        SqlConnection con = new SqlConnection(@"Data Source=*****;Initial Catalog=EEE;Integrated Security=true;");

        //SQL Command
        SqlCommand cmd = new SqlCommand("SELECT DISTINCT Account_Types.DESCRPTN_104, Part_Master.PMDES1_01,Order_Master.PRTNUM_10,Order_Master.ORDNUM_10,Order_Master.ORDRef_10,Part_Master.TNXDTE_01 FROM.............. (EUM_10 = '"+ ORDNUM_10 + "'", con); 

        //Open connection
        con.Open();
        //to read from SQL Server
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            PMDES1_01 = dr["PMDES1_01"].ToString();
            PRTNUM_10 = dr["PRTNUM_10"].ToString();
            DESCRPTN_104 = dr["DESCRPTN_104"].ToString();                
            ORDREF_10 = dr["ORDREF_10"].ToString();
            TNXDTE_01 = dr["TNXDTE_01"].ToString();            
        }
        
        //close connections
        dr.Close();
        con.Close();

        return new List<string> { PMDES1_01, PRTNUM_10, DESCRPTN_104, ORDEF_10 };
    }
}   

How do I get this string as fields so that I can use them in binding in infopath form field?

4

1 回答 1

0

在 Visual Studio 中,单击File菜单,然后New选择Project. 然后ASP.NET Empty Web ApplicationWeb. 给它一个合理的名称,然后单击OK。在解决方案资源管理器中,右键单击解决方案下的新项目,单击Add并选择New ItemWeb ServiceWeb. 为其命名Data.asmx并单击Add。然后,您应该会看到新 Web 服务Data类背后的代码。

现在你可以声明你自己的类来表示一个数据库记录,class Record如下所示。然后通过Record在. 您需要查看数据库列上的数据类型并调用适当的方法,例如,等等...GetData()SqlDataReaderGetString()GetInt32()

class Data : System.Web.Services.WebService
{
    public class Record
    {
        public string AccountType { get; set; }
        public string PartDescription { get; set; }
        public int PartNumber { get; set; }
        public string OrderRef { get; set; }
        public DateTime TransactionDate { get; set; }
    }

    [WebMethod]
    public static List<Record> GetData(string param)    
    {
        SqlConnection con = new SqlConnection(@"ConnectionString");
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT type,desc,num,ref,date FROM foo", con);
        SqlDataReader dr = cmd.ExecuteReader();

        List<Record> records = new List<Record>();

        while (dr.Read())
        {
            records.Add(new Record() {
                AccountType = dr.GetString(0),
                PartDescription = dr.GetString(1),
                PartNumber = dr.GetInt32(2),
                OrderRef = dr.GetString(3),
                TransactionDate = dr.GetDateTime(4)                
            });
        }

        dr.Close();
        con.Close();
        return records;
    }
}

提示:最好通过cmd.Parameters集合向查询添加参数,而不是使用字符串连接。

现在您应该能够运行或部署您的 Web 服务并获取其 WSDL 的 URL。对于从 InfoPath 调用的示例,这里有一个很好的演练

于 2013-05-09T21:40:04.287 回答