因此,我正在编写一个Windows窗体应用程序,用户将在其中从计算机中选择一个xml文件(使用文件对话框),然后单击“保存”按钮,该文件应保存在数据库中。但是我对如何将所选项目保存到保存按钮方法有些迷惑。以下是我的btnChooseFile_click
private void btnChooseFile_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Multiselect = false;
dlg.Filter = "XML documents (*.xml)|*.xml|All Files|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
tbxXmlFile.Text = dlg.FileName;
XmlDocument invDoc = new XmlDocument();
invDoc.Load(dlg.FileName);
....
....
}
}
下面是我的btnStore_click
private void btnStore_Click(object sender, EventArgs e)
{
try
{
string cs = @"Data Source=localhost;Initial Catalog=db;integrated security=true;";
using (SqlConnection sqlConn = new SqlConnection(cs))
{
DataSet ds = new DataSet();
ds.ReadXml("This is where I want to get the selected file");
....
....
}
}
}
那么我该怎么做呢?
您可以使用私有成员变量
private String filename = null;
private void btnChooseFile_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Multiselect = false;
dlg.Filter = "XML documents (*.xml)|*.xml|All Files|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
tbxXmlFile.Text = dlg.FileName;
XmlDocument invDoc = new XmlDocument();
invDoc.Load(dlg.FileName);
....
....
this.filename = dlg.FileName;
}
}
private void btnStore_Click(object sender, EventArgs e)
{
try
{
string cs = @"Data Source=localhost;Initial Catalog=db;integrated security=true;";
using (SqlConnection sqlConn = new SqlConnection(cs))
{
DataSet ds = new DataSet();
ds.ReadXml(this.filename);
....
....
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句