c# - 如何使用 WPF 按钮单击事件将 .txt 文件导入 SQL Server 数据库
问题描述
我有一个按钮单击事件,它打开文件对话框,选择一个 .txt 文件,并在 .txt 中显示文件路径TextBox
。
我遇到的问题是我不确定如何使用另一个按钮单击事件将该 .txt 文件上传到 SQL Server 中的表。请帮忙,谢谢!
private void butn_Upload_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Multiselect = false;
fileDialog.Filter = "Textfiles|* .txt|All Files|*.*";
fileDialog.DefaultExt = ".txt";
Nullable<bool> dialogOK = fileDialog.ShowDialog();
if (dialogOK == true)
{
string sFilenames = "";
foreach (string sFilename in fileDialog.FileNames)
{
sFilenames += ";" + sFilename;
}
sFilenames = sFilenames.Substring(1);
// Show file path in TextBox
TbxFiles.Text = sFilenames;
}
}
此代码是我的第二次单击事件,我想将 .txt 文件插入 SQL Server。
private void btnUpload_Click(object sender, RoutedEventArgs e)
{
//I'm not sure how to take txt file and use it with an Update SQL Statement
SqlConnection con = new SqlConnection("Data Source=WINDOWS-B1AT5HC\\SQLEXPRESS;Initial Catalog=CustomerRelations;Integrated Security=True;");
try
{
SqlCommand cmd = new SqlCommand("UPDATE", con);
cmd.Parameters.AddWithValue("");
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Data updated!");
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
enter code here
解决方案
推荐阅读
- javascript - 检查是否单击了href按钮然后在php中执行代码
- android - 在本机反应中链接到 Teamviewer
- javascript - 最初收到 FirebaseError: Function Query.where() 需要有效的第三个参数,但未定义。但随后刷新
- r - 从宽变长再变宽
- javascript - 如何不在 AdoniJS 中覆盖我的 sqlite 数据库?
- rpc - Godot 多人游戏 RPC 节点树问题?
- asp.net-mvc - 如何将多个参数从 Kendo.Grid 传递给控制器?
- spring-boot - 使用 Google Cloud Pub/Sub 消息订阅者实现时无法创建 Spring-Boot bean
- c# - 我的数据库不是由 Entity Framework 6 代码优先方法自动创建的吗?
- r - 如何在没有降价的情况下在 R 中将文本打印或添加到 pdf() 中