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

本文关键字: | 更新日期: 2023-09-27 18:25:20

我有两个类,第一个是连接器

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

我希望它返回错误值,连接状态,数据库名称,服务器ID和用户名。我该怎么做这个

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

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

我还没有在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类型,您需要选择要显示的属性,例如消息属性。

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

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());
    }
相关文章:
  • 没有找到相关文章