如何通过另一个 class 更改静态字符串值?

How to change static string value by another class?

我有 class DatabaseObjects,其中有 public 个静态字符串字段。我在这个 class 中有一个 filenameConnectionStringExcel。代码为-

class DatabaseObjects
{
    public static string filename = "";
    public static string ConnectionStringExcel = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                                       filename + "; Extended Properties= 'Excel 12.0 Xml;HDR=YES;'";
}

我想通过 frmStudents class 按钮 Import[=43 中的 OpenDialogBox 设置 filename =] 点击事件。用户给出的 filename 应该添加到 ConnectionStringExcel(在其他 class 中)。

frmStudents代码Class

 public partial class frmStudents : Form
{
private void btnImport_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileExcel = new OpenFileDialog();
        openFileExcel.Filter = "Excel Files | *.xlsx; *.xls; *.xlsm";
        openFileExcel.Title = "Select an Excel File";
        if (openFileExcel.ShowDialog() == DialogResult.Cancel || openFileExcel.FileName.Equals(""))
            return;
        DatabaseObjects.filename = openFileExcel.FileName;
        using(OleDbConnection connExcel = new OleDbConnection(DatabaseObjects.ConnectionStringExcel))
        {
            string queryExcel = "SELECT * FROM [Six$]";
            using (OleDbCommand commandExcel = new OleDbCommand(queryExcel,connExcel))
            {
                connExcel.Open();
            }
        }
    }
}

当用户select文件在OpenDialogBox时,filename字符串正确获取值。但是 filename 字符串值没有合并到 ConnectionStringExcel 中。 Data Source 值保持为空。如何解决?

如果我从 filenameConnectionStringExcel 中删除 static 关键字,则会出现 filename A field initializer cannot reference the non-static field 错误。现在怎么办?

ConnectionStringExcel 使用 filename 初始化,但它不会跟踪未来的更改。

您可以使用此 getter

ConnectionStringExcel 转换为只读 属性
public static string ConnectionStringExcel => @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                                       filename + "; Extended Properties= 'Excel 12.0 Xml;HDR=YES;'";

这将导致在每次调用 ConnectionStringExcel

时构造字符串

编辑

如果您使用的是旧版本的 .net 框架,您可以使用

public static string ConnectionStringExcel 
{
   get 
   { 
      return @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                                       filename + "; Extended Properties= 'Excel 12.0 Xml;HDR=YES;'"; 
   } 
}

您可以创建一个函数来构建 GetConnectionStringExcel。此外,我建议您使用 CheckFileExists in OpenFileDialog 对象,它会检查所选文件是否存在。这是一个代码片段。

public static class DatabaseObjects
{
    public static string FileName;

    public static string GetConnectionStringExcel()
    {
        return GetConnectionStringExcel(FileName);
    }

    public static string GetConnectionStringExcel(string filename)
    {
        return @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
               filename + "; Extended Properties= 'Excel 12.0 Xml;HDR=YES;'";
    }
}

public partial class frmStudents : Form
{
    private void btnImport_Click(object sender, EventArgs e)
    {
        var openFileExcel = new OpenFileDialog
        {
            Filter = "Excel Files | *.xlsx; *.xls; *.xlsm",
            Title = "Select an Excel File",
            CheckFileExists = true
        };
        if (openFileExcel.ShowDialog() == DialogResult.Cancel)
            return;

        DatabaseObjects.FileName = openFileExcel.FileName;
        using (var connExcel = new OleDbConnection(DatabaseObjects.GetConnectionStringExcel()))
        {
            string queryExcel = "SELECT * FROM [Six$]";
            using (var commandExcel = new OleDbCommand(queryExcel, connExcel))
            {
                connExcel.Open();
            }
        }
    }
}