[英]How to change static string value by another class?
我有 class DatabaseObjects有公共 static 字符串字段。 我在这个 class 中有一个filename
和ConnectionStringExcel
。 代码如下-
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 中的文件OpenDialogBox
, filename
字符串正确获取值。 但是filename
字符串值没有在ConnectionStringExcel
中组合。 Data Source
值保持为空。 如何解决这个问题?
如果我从filename
和ConnectionStringExcel
中删除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.