WebService仍返回“连接成功".甚至SQL Server也已关闭.请帮忙. [英] The WebService still returns "Connection Successful" even SQL Server is off. Please Help.

查看:114
本文介绍了WebService仍返回“连接成功".甚至SQL Server也已关闭.请帮忙.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的成员和管理员,


大家好,愉快的一天.

我已经学习C#一段时间了,现在我正在做一个非常简单的Web应用程序和一个Web Service,其中该Web Service将处理所有数据库命令,例如打开,关闭,添加记录,删除和编辑.到目前为止,我仍然停留在打开数据库中.

这是我的Web服务的代码,文件名为 Service.asmx :

Dear members and administrators,


Hello everyone, a pleasant day.

I''ve been studying C# for quite sometime and now I am doing a very simple Web application and a Web Service in which this Web Service will handle all database commands such as open, close, add a record, delete, and edit. So far, I am still stuck in opening a database.

This is the code of my Web Service, the file name is Service.asmx:

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{

    private const string connectionString ="Server=MARK\\SQLEXPRESS2005;Database=SampleDatabase;Trusted_Connection=True;";
    
    string sqlQuery;
    DataSet dataSet;
    bool isConnectionFailed;
    SqlConnection sqlConnection;    
    
    public Service () {
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public void connectToDatabase(){
        sqlConnection = new SqlConnection(connectionString);
        try {
            sqlConnection.Open();
            isConnectionFailed = false;
        }
        catch (Exception exception) {
            isConnectionFailed = true;
        }
        finally {  }
        
    }//end connectToDatabase()


    [WebMethod]
    public string getConnectionMessage() {
        string message="";
        if (isConnectionFailed)
            message = "Error while connecting to Microsoft SQL Server.";
        else
            message = "Connection SUCCESSFUL!!!";

        return message;
    }    
}//end class




我测试了它,效果很好.


然后,我创建了一个新的解决方案/项目,即C#中的Web应用程序.之后,我将WebService作为该Web项目(或解决方案)的Web引用添加.这是
的代码 Default.aspx.cs :




I tested it and it works fine.


Then I created a new Solution/Project, a Web Application in C#. Afterwards, I added my WebService as a Web Reference to this Web Project(or Solution). This is the code of
Default.aspx.cs:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace WebProject
{
    public partial class _Default : System.Web.UI.Page
    {
        
WebServiceDatabase.Service service = new WebProject.WebServiceDatabase.Service();//My Web Service

        protected void Page_Load(object sender, EventArgs e)
        {
            service.connectToDatabase();
            lblConnectionMessage.Text = service.getConnectionMessage();
        }
    }
}



我测试了它,并且效果很好. Web浏览器(IE)显示字符串:
连接成功!!!

但是,当我尝试断开/停止SQL Server的服务并尝试再次运行 Default.aspx 时,我仍然收到相同的消息(即 Connection SUCCESSFUL !!! ).不是因为我的SQL Server已停止,我在连接到Microsoft SQL Server时应该收到 Error 吗?


谁能帮我更改我的Web服务代码?

谢谢您,还有更大的力量.

致以诚挚的问候,

标记



I tested it and it works fine. The web browser(IE) displays the string:
Connection SUCCESSFUL!!!

BUT, when I tried to disconnect/stop the service of my SQL Server and try to run again the Default.aspx, I am still recieving the same message (i.e. Connection SUCCESSFUL!!!). Is not that I should be receiving Error while connecting to Microsoft SQL Server because my SQL Server has stopped?


Can anyone help me how would I change my webservice code?

Thank you and more power.

Warm regards,

Mark

推荐答案

您可以执行以下操作.

something like this you can do.

public bool connectToDatabase(){
    sqlConnection = new SqlConnection(connectionString);
    try {
        sqlConnection.Open();
        return false;
    }
    catch (Exception exception) {
        return true;
    }
    finally {  }

}//end connectToDatabase()


[WebMethod]
public string getConnectionMessage() {
    string message="";
     bool isConnectionFailed = connectToDatabase();
    if (isConnectionFailed)
        message = "Error while connecting to Microsoft SQL Server.";
    else
        message = "Connection SUCCESSFUL!!!";

    return message;
}


这篇关于WebService仍返回“连接成功".甚至SQL Server也已关闭.请帮忙.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆