首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c# 接口有关问题

2012-04-13 
c# 接口问题在c# 服务端的Service.cs文件中写的接口,之前是供手机端解析调用的,但现在还想写一个网页的客

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 (CompilerError ce in cr.Errors)            {                sb.Append(ce.ToString());                sb.Append(System.Environment.NewLine);            }            throw new Exception(sb.ToString());        }        //生成代理实例,并调用方法        System.Reflection.Assembly assembly = cr.CompiledAssembly;        Type t = assembly.GetType(WebServiceNS + "." + classname, true, true);        object obj = Activator.CreateInstance(t);//【10】        System.Reflection.MethodInfo mi = t.GetMethod(methodname);//【11】        return mi.Invoke(obj, args);    } 

热点排行