问题描述
- C#点击load在指定位置创建TXT文件,并把读取的内容生成到TXT文件(已实现读取数据库内容)
-
代码如下,求帮忙
namespace LDD__Generator
{
public partial class LDD : Form
{
public LDD()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
GetData();} private void GetData() { try { DataTable dtMAT_NR = SQLUtility.Select("Select MAT_NR From ADE_LOS Where LOS_NR = '" + txtLOS_NR.Text.Trim() + "'", null); DataTable dtPANELID = SQLUtility.Select("Select PANELID From FCBDB.dbo.PANELTAB Where LOSID ='" + txtLOS_NR.Text.Trim() + "'", null); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } private void txtLOS_NR_KeyPress(object sender, KeyPressEventArgs e) { GetData(); }
解决方案
private void GetData()
{
try
{
DataTable dtMAT_NR = SQLUtility.Select("Select MAT_NR From ADE_LOS Where LOS_NR = '" + txtLOS_NR.Text.Trim() + "'", null);
DataTable dtPANELID = SQLUtility.Select("Select PANELID From FCBDB.dbo.PANELTAB Where LOSID ='" + txtLOS_NR.Text.Trim() + "'", null);
//遍历2个表格组合好字符串调用IO写入记事本不就好了
string s="";
foreach (DataRow dr in dtMAT_NR) s += "MAT_NR=" + dr[0].ToString() + "rn";
foreach (DataRow dr in dtPANELID) s += "PAN=" + dr[0].ToString() + "rn";
//记得修改第一个参数为你需要制定的位置
System.IO.File.WriteAllText(@"c:xxx.txt",s,System.Text.Encoding.UTF8);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
解决方案二:
private void GetData()
{
try
{
DataTable dtMAT_NR = SQLUtility.Select("Select MAT_NR From ADE_LOS Where LOS_NR = '" + txtLOS_NR.Text.Trim() + "'", null);
DataTable dtPANELID = SQLUtility.Select("Select PANELID From FCBDB.dbo.PANELTAB Where LOSID ='" + txtLOS_NR.Text.Trim() + "'", null);
string s = "";
foreach (DataRow dr in dtMAT_NR.Rows)
{
s += "MAT_NR=" + dr[0].ToString() + "rn";
}
foreach (DataRow dr in dtPANELID.Rows)
{
s += "PAN=" + dr[0].ToString() + "rn";
}
System.IO.File.WriteAllText(@"D:A.txt", s, System.Text.Encoding.UTF8);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
根据大神答案进行一些细节修改已经能完成运行,留给需要的人看看
解决方案三:
就是两个操作,说白了就是一个文件操作问题。File之类的,你看下
时间: 2024-09-12 23:13:30