2011-09-23 57 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

雖然序列化可能是跨越邊界的解決方案,但是沒有理由跨數據庫連接實例傳遞數據庫連接實例,即使是序列化。 –