簡體   English   中英

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

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

我有 class DatabaseObjects有公共 static 字符串字段。 我在這個 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;'";
}

我想通過按鈕Import Click 事件上的frmStudents class 中的OpenDialogBox設置filename 並且用戶給出的這個filename應該添加到ConnectionStringExcel (在其他類中)。

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 中的文件OpenDialogBoxfilename字符串正確獲取值。 但是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;'"; 
   } 
}

您可以創建 function 來構建GetConnectionStringExcel 另外,我建議您使用OpenFileDialog object 中的CheckFileExists ,它會檢查所選文件是否存在。 這是一個代碼片段。

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