使用网络服务引用从该方法返回的数据时出现错误

时间:2018-11-07 20:23:27

标签: asp.net web-services

我将Web服务(asmx)编写为

[WebMethod]
//public List<Ahlam> GetBloodGroup(String gblood)
public DataSet GetBloodGroup(string gblood)
{
        string bloodgroup = gblood.ToString();
        string ApoloConn = ConfigurationManager.ConnectionStrings["ConnSApolo"].ConnectionString;

        SqlConnection con = new SqlConnection(ApoloConn);

        SqlCommand cmd = new SqlCommand("select name as 'NAME', blood_group as 'BLOOD GROUP', gender as 'GENDER' , city as 'CITY' , mobile as 'MOBILE' " +
            "from dontable where blood_group = @bloodgroup", con);
        cmd.Parameters.AddWithValue("@bloodgroup", bloodgroup);
        SqlDataAdapter da = new SqlDataAdapter();

        da.SelectCommand = cmd;
        DataSet ds1 = new DataSet();
        da.Fill(ds1);

        return ds1;
 }

这是在我的Web服务中按下以调用该函数时的buttonAction

protected void search_bloodgroup_Click(object sender, EventArgs e)
{ 
      string bg = bloodlist.SelectedItem.Text;
      AhlamQueryGroup.AhlamQueryBloodSoapClient client = new 
      AhlamQueryGroup.AhlamQueryBloodSoapClient();

      DataSet set = client.GetBloodGroup(bg);
      gridview_Donate.DataSource = set;
}

这是在我项目的web.config

<connectionStrings>
    <add name="ConnSApolo" 
         connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\ApoloDB.mdf;Integrated Security=True;" 
         providerName="System.Data.SqlClient" />
</connectionStrings>

这是我的Web服务项目中的web.config

<connectionStrings>
    <add name="ConnSApolo" 
         connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\ApoloDB.mdf;Integrated Security=True;" 
         providerName="System.Data.SqlClient" />
</connectionStrings>

当我按下按钮时出现此错误

  

System.Web.Services.Protocols.SoapException:服务器无法处理请求。

     

System.Data.SqlClient.SqlException:尝试为文件E:\ Ahlam \ BloodHospital_project \ Blood_Hospital_banks_project \ WebServices \ WebServices \ App_Data \ ApoloDB.mdf附加自动命名数据库的尝试失败。存在具有相同名称的数据库,或者无法打开指定的文件,或者该数据库位于UNC共享上。

     在System.Data.SqlClient.SqlInternalConnectionTds..ctor中的

(DbConnectionPoolIdentity身份,SqlConnectionString connectionOptions,SqlCredential凭据,对象providerInfo,String newPassword,SecureString newSecurePassword,布尔型redirectedUserInstance,SqlConnectionString userConnectionOptions,SessionData reconnectSessionData,DbConnectionPientFool,
  在System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions选项,DbConnectionPoolKey poolKey,对象poolGroupProviderInfo,DbConnectionPool池,DbConnection owningConnection,DbConnectionOptions userOptions)
  在System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection处(DbConnectionPool池,DbConnection owningObject,DbConnectionOptions选项,DbConnectionPoolKey poolKey,DbConnectionOptions userOptions)
  在System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject,DbConnectionOptions userOptions,DbConnectionInternal oldConnection)
  在System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject,DbConnectionOptions userOptions,DbConnectionInternal oldConnection)
  在System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,UInt32 waitForMultipleObjectsTimeout,布尔allowCreate,布尔onlyOneCheckConnection,DbConnectionOptions userOptions,DbConnectionInternal&连接)   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection   owningObject,TaskCompletionSource 1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource 1重试,DbConnectionOptions   userOptions,DbConnectionInternal oldConnection,DbConnectionInternal&   连接)   System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection   externalConnection,DbConnectionFactory connectionFactory,   TaskCompletionSource 1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource 1重试,DbConnectionOptions userOptions)位于   System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource 1 retry) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource 1   重试)在System.Data.SqlClient.SqlConnection.Open()在   System.Data.Common.DbDataAdapter.FillInternal(DataSet数据集,   DataTable []数据表,Int32 startRecord,Int32 maxRecords,字符串   srcTable,IDbCommand命令,CommandBehavior行为)位于   System.Data.Common.DbDataAdapter.Fill(数据集dataSet,Int32   startRecord,Int32 maxRecords,String srcTable,IDbCommand命令,   CommandBehavior行为)   System.Data.Common.DbDataAdapter.Fill(数据集dataSet)位于   WebServices.AhlamQueryBlood.GetBloodGroup(String gblood)在   E:\ Ahlam \ BloodHospital_project \ Blood_Hospital_banks_project \ WebServices \ WebServices \ AhlamQueryBlood.asmx.cs:line   40 ---内部异常堆栈跟踪的结尾---描述:   当前Web执行期间发生未处理的异常   请求。请查看堆栈跟踪以获取有关   错误及其在代码中的起源。

     

异常详细信息:System.ServiceModel.FaultException:   System.Web.Services.Protocols.SoapException:服务器无法   处理要求。 ---> System.Data.SqlClient.SqlException:尝试   附加文件的自动命名数据库   E:\ Ahlam \ BloodHospital_project \ Blood_Hospital_banks_project \ WebServices \ WebServices \ App_Data \ ApoloDB.mdf   失败了存在具有相同名称的数据库,或者指定的文件不能   被打开,或者它位于UNC共享上。在   System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity   身份,SqlConnectionString connectionOptions,SqlCredential   凭证,对象providerInfo,字符串newPassword,SecureString   newSecurePassword,布尔型redirectedUserInstance,SqlConnectionString   userConnectionOptions,SessionData reconnectSessionData,   DbConnectionPool池,字符串accessToken,布尔值   applyTransientFaultHandling)   System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions   选项,DbConnectionPoolKey poolKey,对象poolGroupProviderInfo,   DbConnectionPool池,DbConnection owningConnection,   DbConnectionOptions userOptions)   System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool   池,DbConnection owningObject,DbConnectionOptions选项,   DbConnectionPoolKey poolKey,DbConnectionOptions userOptions)位于   System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection   owningObject,DbConnectionOptions,userOptions,DbConnectionInternal   oldConnection)在   System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection   owningObject,DbConnectionOptions,userOptions,DbConnectionInternal   oldConnection)在   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection   owningObject,UInt32 waitForMultipleObjectsTimeout,布尔值   allowCreate,仅布尔值OneCheckConnection,DbConnectionOptions   userOptions,DbConnectionInternal&连接),位于   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection   owningObject,TaskCompletionSource 1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource 1重试,DbConnectionOptions   userOptions,DbConnectionInternal oldConnection,DbConnectionInternal&   连接)   System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection   externalConnection,DbConnectionFactory connectionFactory,   TaskCompletionSource 1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource 1重试,DbConnectionOptions userOptions)位于   System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource 1 retry) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource 1   重试)在System.Data.SqlClient.SqlConnection.Open()在   System.Data.Common.DbDataAdapter.FillInternal(DataSet数据集,   DataTable []数据表,Int32 startRecord,Int32 maxRecords,字符串   srcTable,IDbCommand命令,CommandBehavior行为)位于   System.Data.Common.DbDataAdapter.Fill(数据集dataSet,Int32   startRecord,Int32 maxRecords,String srcTable,IDbCommand命令,   CommandBehavior行为)   System.Data.Common.DbDataAdapter.Fill(数据集dataSet)位于   WebServices.AhlamQueryBlood.GetBloodGroup(String gblood)在   E:\ Ahlam \ BloodHospital_project \ Blood_Hospital_banks_project \ WebServices \ WebServices \ AhlamQueryBlood.asmx.cs:line   40 ---内部异常堆栈跟踪的结尾---

     

源错误:

     

第53行:第54行:公共System.Data.DataSet   GetBloodGroup(字符串gblood){第55行:返回   base.Channel.GetBloodGroup(gblood);第56行:}第57行:

     

源文件:   E:\ Ahlam \ BloodHospital_project \ Blood_Hospital_banks_project \ BloodBank \ BloodBank \ Connected   Services \ AhlamQueryGroup \ Reference.cs行:55

     

堆栈跟踪:

     

[FaultException:System.Web.Services.Protocols.SoapException:服务器   无法处理请求。 ->   System.Data.SqlClient.SqlException:尝试附加自动命名   文件数据库   E:\ Ahlam \ BloodHospital_project \ Blood_Hospital_banks_project \ WebServices \ WebServices \ App_Data \ ApoloDB.mdf   失败了存在具有相同名称的数据库,或者指定的文件不能   被打开,或者它位于UNC共享上。在   System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity   身份,SqlConnectionString connectionOptions,SqlCredential   凭证,对象providerInfo,字符串newPassword,SecureString   newSecurePassword,布尔型redirectedUserInstance,SqlConnectionString   userConnectionOptions,SessionData reconnectSessionData,   DbConnectionPool池,字符串accessToken,布尔值   applyTransientFaultHandling)   System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions   选项,DbConnectionPoolKey poolKey,对象poolGroupProviderInfo,   DbConnectionPool池,DbConnection owningConnection,   DbConnectionOptions userOptions)   System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool   池,DbConnection owningObject,DbConnectionOptions选项,   DbConnectionPoolKey poolKey,DbConnectionOptions userOptions)位于   System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection   owningObject,DbConnectionOptions,userOptions,DbConnectionInternal   oldConnection)在   System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection   owningObject,DbConnectionOptions,userOptions,DbConnectionInternal   oldConnection)在   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection   owningObject,UInt32 waitForMultipleObjectsTimeout,布尔值   allowCreate,仅布尔值OneCheckConnection,DbConnectionOptions   userOptions,DbConnectionInternal&连接),位于   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection   owningObject,TaskCompletionSource 1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource 1重试,DbConnectionOptions   userOptions,DbConnectionInternal oldConnection,DbConnectionInternal&   连接)   System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection   externalConnection,DbConnectionFactory connectionFactory,   TaskCompletionSource 1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource 1重试,DbConnectionOptions userOptions)位于   System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource 1 retry) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource 1   重试)在System.Data.SqlClient.SqlConnection.Open()在   System.Data.Common.DbDataAdapter.FillInternal(DataSet数据集,   DataTable []数据表,Int32 startRecord,Int32 maxRecords,字符串   srcTable,IDbCommand命令,CommandBehavior行为)位于   System.Data.Common.DbDataAdapter.Fill(数据集dataSet,Int32   startRecord,Int32 maxRecords,String srcTable,IDbCommand命令,   CommandBehavior行为)   System.Data.Common.DbDataAdapter.Fill(数据集dataSet)位于   WebServices.AhlamQueryBlood.GetBloodGroup(String gblood)在   E:\ Ahlam \ BloodHospital_project \ Blood_Hospital_banks_project \ WebServices \ WebServices \ AhlamQueryBlood.asmx.cs:line   40 ---内部异常堆栈跟踪的结尾---]
  System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage   reqMsg,IMessage retMsg)+153
  System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&   msgData,Int32类型)+336
  BloodBank.AhlamQueryGroup.AhlamQueryBloodSoap.GetBloodGroup(String   gblood)+0
  BloodBank.AhlamQueryGroup.AhlamQueryBloodSoapClient.GetBloodGroup(String   gblood)在   E:\ Ahlam \ BloodHospital_project \ Blood_Hospital_banks_project \ BloodBank \ BloodBank \ Connected   Services \ AhlamQueryGroup \ Reference.cs:55
  BloodBank.FilterDonor.search_bloodgroup_Click(对象发送者,EventArgs   e)在   E:\ Ahlam \ BloodHospital_project \ Blood_Hospital_banks_project \ BloodBank \ BloodBank \ FilterDonor.aspx.cs:40   System.Web.UI.WebControls.Button.OnClick(EventArgs e)+9669714
  System.Web.UI.WebControls.Button.RaisePostBackEvent(String   eventArgument)+108
  System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String   eventArgument)+12
  System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler   sourceControl,String eventArgument)+15
  System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)   +35 System.Web.UI.Page.ProcessRequestMain(布尔值includeStagesBeforeAsyncPoint,布尔值包括StagesAfterAsyncPoint)   + 3562at

这是我的项目中我的Web引用的引用 enter image description here 这是相同解决方案中webservices项目中的Web服务 enter image description here

我试图从我的webservices项目中的web.config中删除connectionString,但是又遇到另一个错误 enter image description here 当我按继续时,我会遇到相同的第一个错误

1 个答案:

答案 0 :(得分:0)

模拟问题:

您要做的第一件事是。

创建一个新的普通项目(不是Web服务项目),然后运行您的函数(如果它不起作用)。

public DataSet GetBloodGroup(string gblood)
{
        string bloodgroup = gblood.ToString();
        string ApoloConn = ConfigurationManager.ConnectionStrings["ConnSApolo"].ConnectionString;

        SqlConnection con = new SqlConnection(ApoloConn);

        SqlCommand cmd = new SqlCommand("select name as 'NAME', blood_group as 'BLOOD GROUP', gender as 'GENDER' , city as 'CITY' , mobile as 'MOBILE' " +
            "from dontable where blood_group = @bloodgroup", con);
        cmd.Parameters.AddWithValue("@bloodgroup", bloodgroup);
        SqlDataAdapter da = new SqlDataAdapter();

        da.SelectCommand = cmd;
        DataSet ds1 = new DataSet();
        da.Fill(ds1);

        return ds1;
 }

类似的东西:

DataSet dsSimulate = new DataSet();
dsSimulate = GetBloodGroup("YourbloodValueHere");

然后在调试模式下运行它以获取并检查错误。

您的错误发生,并显示(You already has a database name)。尝试检查它是否存在。

提醒:

在使用和运行Web Service之前,必须确保它是否在正常调试模式下工作。

有两种可能的问题。.要么是您的函数有错误,要么是调用Web Service时的部署本身。

将Web服务添加到项目参考中时:

Web.Config文件(Project Calling the Web Service) 在项目中添加已部署服务参考的链接后,它必须看起来像这样。

<client>
      <endpoint address="http://localhost:1750/Service1.asmx" binding="basicHttpBinding"
        bindingConfiguration="Service1Soap" contract="Service1.Service1Soap"
        name="Service1Soap" />
      <endpoint address="http://localhost:1750/Service1.asmx" binding="customBinding"
        bindingConfiguration="Service1Soap12" contract="Service1.Service1Soap"
        name="Service1Soap12" />
    </client>

在您后面的代码中:

 Service1.Service1SoapClient cli;

 protected void Page_Load(object sender, EventArgs e)
 {
    cli = new Service1.Service1SoapClient("Service1Soap12");
    cli.GetBloodGroup("BloodGourHere");//this line returns your DataSet
 }

希望有帮助。