日期:2014-05-18  浏览次数:20807 次

c# 接口问题
在c# 服务端的Service.cs文件中写的接口,之前是供手机端解析调用的,但现在还想写一个网页的客户端,也是用c#写,可不可以在aspx.cs文件中直接调用这些接口?怎么调用,麻烦详细点说~~~







接口类似这种:
C# code

    [WebMethod]
    protected DataSet tip_details(string username)
    {
        SqlConnection sqlconnection = new SqlConnection(ConnectionString);
        sqlconnection.Open();
        string cmdstring = "select title,details,id,time from [tip] ";
        SqlDataAdapter sqlAdapteruser = new SqlDataAdapter(cmdstring, sqlconnection);
        DataSet ds = new DataSet();
        sqlAdapteruser.Fill(ds, "tip");
        sqlconnection.Close();
        return ds;
    }


    [WebMethod ]
    public DataSet send_tips(string username)               //环保小贴士
    {

        DataSet ds = new DataSet();

        ds = tip_details (username);
        return ds;



    }



还有这种:
C# code

    [WebMethod]
    public Boolean ValidLogin(string username, string password)            //验证登陆名与密码是否匹配
    {

        Boolean flag = false;


        SqlConnection sqlconnection = new SqlConnection(ConnectionString);
        sqlconnection.Open();
        string cmdcount="select count(username) from [user] where username=" + "'" + username + "'";
        SqlCommand cmdd = new SqlCommand(cmdcount, sqlconnection);
        int cc = (int)cmdd.ExecuteScalar();
        if(cc==0)
            return false;
        string cmdString = "select password from [user] where username=" + "'" + username + "'";
        SqlCommand cmd = new SqlCommand(cmdString, sqlconnection);
        string pas = (string)cmd.ExecuteScalar();
        sqlconnection.Close();
        if ((pas.Trim() != password.Trim()) || pas.Trim () == null)
        { flag = false; }
        else
        { flag = true; }

        return flag;
    }



这样的接口文件要怎么直接调用?还有获取的东西还可不可以绑定在detailsview和gridview里了?

------解决方案--------------------
貌似你这个是webservice,发送webservice有特定的方式

这是我曾经写过的连webservice的方法,不知道能不能用~
C# code

    public static object InvokeWebService(string url, string classname, string methodname, object[] args)
    {
        if (classname == null || classname == "")
        {
            classname = WebServiceHelper.GetClassName(url);
        }
        //获取服务描述语言(WSDL)
        WebClient wc = new WebClient();
        Stream stream = wc.OpenRead(url + ComUtil.WSDL);//【1】
        ServiceDescription sd = ServiceDescription.Read(stream);//【2】
        ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();//【3】
        sdi.AddServiceDescription(sd, "", "");
        CodeNamespace cn = new CodeNamespace(WebServiceNS);//【4】

        //生成客户端代理类代码
        CodeCompileUnit ccu = new CodeCompileUnit();//【5】
        ccu.Namespaces.Add(cn);
        sdi.Import(cn, ccu);
        CSharpCodeProvider csc = new CSharpCodeProvider();//【6】
        ICodeCompiler icc = csc.CreateCompiler();//【7】

        //设定编译器的参数
        CompilerParameters cplist = new CompilerParameters();//【8】
        cplist.GenerateExecutable = false;
        cplist.GenerateInMemory = true;
        cplist.ReferencedAssemblies.Add("System.dll");
        cplist.ReferencedAssemblies.Add("System.XML.dll");
        cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
        cplist.ReferencedAssemblies.Add("System.Data.dll");

        //编译代理类
        CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);//【9】
        if (true == cr.Errors.HasErrors)
        {
            System.Text.StringBuilder sb = new StringBuilder();
            foreach