简体   繁体   English

如何用另一个 class 更改 static 字符串值?

[英]How to change static string value by another class?

I have class DatabaseObjects which have public static string fields.我有 class DatabaseObjects有公共 static 字符串字段。 I have a filename and ConnectionStringExcel in this class.我在这个 class 中有一个filenameConnectionStringExcel Code is as-代码如下-

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;'";
}

I want to set the filename by the OpenDialogBox in frmStudents class on button Import Click event.我想通过按钮Import Click 事件上的frmStudents class 中的OpenDialogBox设置filename And this filename given by user should be added to the ConnectionStringExcel (Which is in other class).并且用户给出的这个filename应该添加到ConnectionStringExcel (在其他类中)。

Code of frmStudents 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();
            }
        }
    }
}

When the user select the file in OpenDialogBox , the filename string get the value correctly.当用户 select 中的文件OpenDialogBoxfilename字符串正确获取值。 But filename string value is not combining in ConnectionStringExcel .但是filename字符串值没有在ConnectionStringExcel中组合。 The Data Source value remains empty. Data Source值保持为空。 How to resolve this?如何解决这个问题?

If I remove static keyword from filename and ConnectionStringExcel , it is giving error that filename A field initializer cannot reference the non-static field .如果我从filenameConnectionStringExcel中删除static关键字,则会给出错误,即filename A field initializer cannot reference the non-static field What now?现在怎么办?

ConnectionStringExcel is initialized using filename but it won't track future changes. ConnectionStringExcel使用filename初始化,但不会跟踪未来的更改。

You can convert ConnectionStringExcel into a readonly property with this getter您可以使用此 getter 将ConnectionStringExcel转换为只读属性

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

This will cause the string to be constructed with every call to ConnectionStringExcel这将导致在每次调用ConnectionStringExcel时构造字符串

EDIT编辑

If you're using an old version of the .net framework you can use如果您使用的是旧版本的 .net 框架,您可以使用

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

You can create a function to build the GetConnectionStringExcel .您可以创建 function 来构建GetConnectionStringExcel Also, I recommend you to use the CheckFileExists in OpenFileDialog object, it will check if the selected file exist.另外,我建议您使用OpenFileDialog object 中的CheckFileExists ,它会检查所选文件是否存在。 Here is a code snip.这是一个代码片段。

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();
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM