2011-09-23 54 views
0

这是我的网络服务代码:无法从Web服务返回我的SQL连接对象asp.net项目

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Web; 
    using System.Web.Services; 
    using System.Data.Sql; 
    using System.Data.SqlClient; 

    namespace DBwebService 
    { 
     /// <summary> 
     /// Summary description for WebService1 
     /// </summary> 
     [WebService(Namespace = "http://kse.org/")] 
     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
     [System.ComponentModel.ToolboxItem(false)] 
     // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
     // [System.Web.Script.Services.ScriptService] 
     public class WebService1 : System.Web.Services.WebService 
     { 

      string ConnectionString = "Data Source=Shumaila-PC;Initial Catalog=kse;Persist Security Info=True;User ID=sa;Password=sa"; 
      public SqlConnection Conn; 

      [WebMethod] 

      public void SqlConn() 
      { 

        Conn = new SqlConnection(ConnectionString); 
        // Conn.Open(); 

       } 
       //catch (SqlException ex) 
       //{ 

       // //Console.WriteLine("Connection Unsuccessful " + ex.Message); 

       //} 

     } 
    } 

我需要回到我的SQL连接对象,这样我可以在我的asp.net叫它pid roject。但是当我做了

public SqlConnection SqlConn() 

return.Conn(); 

这给了我下面的错误

Server Error in '/' Application. Parser Error Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: Could not create type 'DBwebService.WebService1'.

Source Error:

Line 1: <%@ WebService Language="C#" CodeBehind="WebService1.asmx.cs" Class="DBwebService.WebService1" %>

Source File: /WebService1.asmx Line: 1

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1 --. Metadata contains a reference that cannot be resolved: 'http://localhost:50387/WebService1.asmx'. The content type text/html; charset=utf-8 of the response message does not match the content type of the binding (application/soap+xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 1024 bytes of the response were: ' Server Error in '/' Application. Parser Error '. The remote server returned an error: (500) Internal Server Error. If the service is defined in the current solution, try building the solution and adding the service reference again.

+0

只返回康恩,不康恩() – mslliviu

+0

这是错字。我已经编码康涅狄格州 –

回答

0

您的Web服务应该公开接受注册数据作为方法参数的方法。然后该服务可以将该数据提交到数据库,然后向用户界面返回Ack/Nack响应。

2

天哪你是认真的?你甚至不应该考虑从服务中返回一个连接。您应该使用该连接执行的查询返回您加载的数据。也就是说,移动DAL类库中调用代码中的连接的所有逻辑并仅返回结果。

+0

我是新来的.net 但我想发送我的注册页面数据到数据库。注册页面是另一个asp.net项目。请建议我如何实现它 –

+0

这不是.net,但基本的设计意义。 Web服务是完全不合适的应用程序边界来传递这些类型的对象。它只是考虑到我的头发。 –

0

HMM开始想起它连接对象没有被序列化,你必须声明你的对象是序列化的 - 只能做上面的任务,只有原始类型是可以自动序列化的。

+1

虽然序列化可能是跨越边界的解决方案,但是没有理由跨数据库连接实例传递数据库连接实例,即使是序列化。 –