如何在C#中从类发送和接收值

时间:2013-07-29 00:59:18

标签: c# class oop

我有2个课程,第一个是连接器

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和Username返回。 我怎么能这样做

3 个答案:

答案 0 :(得分:0)

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); ...

答案 1 :(得分: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,所以您需要选择要显示的属性,例如消息属性。

答案 2 :(得分:0)

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

OT.ConnectTo("(local)", "TestOTLab").ErrorMessage.ToString
OT.ConnectTo("(local)", "TestOTLab").ConnectionStatus.ToString
  1. 您在调用ToString
  2. 时缺少()
  3. 由于您正在呼叫,因此导致与DB建立两个连接 ConnectTo方法两次
  4. 如果连接成功,则尝试访问ToString() null对象的方法。
  5. 试试这个:

        var connector = OT.ConnectTo("(local)", "TestOTLab");
        Console.WriteLine(connector.ConnectionStatus.ToString());
        if (connector.ConnectionStatus == ConnectionStat.Fail)
        {
            Console.WriteLine(connector.ErrorMessage.ToString());
        }