1

我有 2 个课程,第一个Connector课程

public class Connector
{
    private SqlConnection sqlCon = new SqlConnection();

    public ErrorProvider ConnectTo(string serverID, string databaseName)
    {
        ErrorProvider errorProv = new ErrorProvider();
        const string defineString = "Data Source={0};Initial Catalog={1};Integrated Security=True;Asynchronous Processing=true;MultipleActiveResultSets=True";

        try
        {
            if (sqlCon.State == ConnectionState.Open && sqlCon.ConnectionString == string.Format(defineString, serverID, databaseName))
            {
                errorProv.ConnectionStatus = ConnectionStat.Success;
            }
            sqlCon.ConnectionString = string.Format(defineString, serverID, databaseName);
            sqlCon.Open();
            sqlCon.Close();
            errorProv.ConnectionStatus = ConnectionStat.Success;

        }
        catch (SqlException ex)
        {
            errorProv.ConnectionStatus = ConnectionStat.Fail;
            errorProv.ErrorMessage = ex;
        }
        return errorProv;
    }
}

现在我想将值发送给其他类调用ErrorProvider

public class ErrorProvider
{    
    public ConnectionStat ConnectionStatus { get; set; }
    public SqlException ErrorMessage { get; set; }
    public ConnectionDetils ConnectionDet;
}

public enum ConnectionStat
{
    Success,
    Fail
}

public class ConnectionDetils
{
    public string ServerID { get; set; }
    public string DatabaseName { get; set; }
    public string UserName { get; set; }
}

当我尝试在这段代码中使用它时,它什么也没返回

OT.ConnectTo("(local)", "TestOTLab").ErrorMessage.ToString
OT.ConnectTo("(local)", "TestOTLab").ConnectionStatus.ToString

我希望它按错误值、连接状态、DatabaseName、ServerID 和用户名返回。我怎么能这样做

4

3 回答 3

0

我没有在 Visual Studio 中尝试过代码,但这不只是添加以下内容吗?

var errorProvider = OT.ConnectTo("(local)", "TestOTLab");
if (errorProvider != null)
{
   var errorMessage = errorProvider.ErrorMessage;
   var connectionStatus = errorProvider.ConnectionStat;
   var connectionDetails = errorProvider.ConnectionDetails;

   if (connectionDetails != null)
   {
       var serverID = connectionDetails.ServerID;
       \\insert other variables here...
   }
}

因为ErrorMessage 是SqlException 类型,所以您需要选择要显示的属性,例如消息属性。

于 2013-08-01T17:19:18.940 回答
0

Line在类public ConnectionDetils ConnectionDet;中定义了一个字段。ErrorProvider您可以设置和读取该字段以及ErrorMessage字段。

ErrorProv.ConnectionStatus = Error.ErrorProvider.ConnectionStat.Success;
ErrorProv.ErrorMessage = "";   // Don't forget this field in 'success' scenario
var details = new Error.ConnectionDetils();
details.ServerID = ServerID;
details. ...the rest of data ...
ErrorProv.ConnectionDet = details;

并阅读它

var status = OT.ConnectTo("(local)", "TestOTLab");
Console.WriteLine(status.details.ServerID); ...
于 2013-07-29T01:06:21.553 回答
0

我发现您的代码存在以下问题:

OT.ConnectTo("(local)", "TestOTLab").ErrorMessage.ToString
OT.ConnectTo("(local)", "TestOTLab").ConnectionStatus.ToString
  1. 您在对 ToString 的调用中缺少 ()
  2. 您正在导致与数据库的两个连接,因为您两次调用 ConnectTo 方法
  3. 如果连接成功,您将尝试访问空对象上的 ToString() 方法。

尝试这个:

    var connector = OT.ConnectTo("(local)", "TestOTLab");
    Console.WriteLine(connector.ConnectionStatus.ToString());
    if (connector.ConnectionStatus == ConnectionStat.Fail)
    {
        Console.WriteLine(connector.ErrorMessage.ToString());
    }
于 2013-08-01T18:48:38.390 回答