private void Add_Xml_Btn_Click(object sender, EventArgs e)
{
try
{
string filename = "D:\\C#\\" + VSL.Text.Trim() + VVD.Text.Trim() + ".xml";
if (File.Exists(filename))
{
}
else
{
File.Create(filename);
}
int BL_NumALl=0;
OleDbConnection conn_access = new OleDbConnection();
conn_access.ConnectionString = SQLOld_Conn.M_str_ldconn;
conn_access.Open()
OleDbCommand odcommandNum = conn_access.CreateCommand();
odcommandNum.CommandText = "select 语句";
OleDbDataReader N1 = odcommandNum.ExecuteReader();
bool ifcom1=N1.Read();
if (ifcom1)
{
BL_NumALl = Convert.ToInt32(N1[0].ToString());
}
N1.Close();
OleDbCommand odcommand = conn_access.CreateCommand();
odcommand.CommandText ="select 语句";
OleDbDataReader odbr = odcommand.ExecuteReader();
bool ifcom = odbr.Read();
if (ifcom)
{
DateTime.Now.ToShortTimeString();
DateTime dt = Convert.ToDateTime(odbr[20].ToString());
string SendTime = string.Format("{0:yyyyMMddHHmmss}",dt);
System.Xml.Linq.XDocument xdoc = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"));
XElement root = new XElement("MESSAGE",
new XElement("MESSAGE_ID", odbr[0].ToString()),
new XElement("MESSAGE_DESC", odbr[1].ToString()),
new XElement("RECEIVERS",
new XElement("RECEIVER", odbr[6].ToString())
),
new XElement("MANIFEST",
new XElement("I_E_FLAG", odbr[7].ToString()),
new XElement("I_F_FLAG", odbr[8].ToString()),
new XElement("EMPTY_CONTA_NUM", odbr[13].ToString()),
new XElement("VSL_VOY",
new XElement("SHIP_ID", odbr[14].ToString()),
new XElement("I_E_DATE", SendTime),
new XElement("L_D_WHARF_NAME", odbr[24].ToString())
)
)
);
odbr.Close();
odcommand.CommandText = "select语句";
OleDbDataReader odbr_xbl = odcommand.ExecuteReader();
string BL_No2 = "Nulll";
while (odbr_xbl.Read())
{
string BL_NO_TMP = odbr_xbl[1].ToString();
string BL_No1 = odbr_xbl[1].ToString();
if (BL_No1 != BL_No2)
{
XElement xele = root.Element("MANIFEST").Element("VSL_VOY");
xele.AddAfterSelf(new XElement("BILL",
new XElement("BILL_SEQ_NO", BL_NumALl ),
new XElement("BILL_NO", odbr_xbl[1].ToString()),
new XElement("SHIPPER", odbr_xbl[2].ToString()),
new XElement("CONSIGNEE", odbr_xbl[3].ToString()),
new XElement("NOTIFY", odbr_xbl[4].ToString()),
new XElement("LOAD_PORT", odbr_xbl[5].ToString()),
new XElement("LOAD_COUNTRY", odbr_xbl[6].ToString()),
new XElement("DISCHARGE_PORT", odbr_xbl[7].ToString()),
new XElement("DISCHARGE_COUNTRY", odbr_xbl[8].ToString()),
new XElement("BILL_GROSS_WT", odbr_xbl[9].ToString()),
new XElement("BILL_CONTA_NUM", odbr_xbl[10].ToString()),
new XElement("REBATE", odbr_xbl[11].ToString()),
new XElement("TRANSIT", odbr_xbl[12].ToString()),
new XElement("REMARK", odbr_xbl[13].ToString())
)
);
BL_NumALl-- ;
int cno = 1;
OleDbCommand odcommandgoods = conn_access.CreateCommand();
odcommandgoods.CommandText = "select语句";
OleDbDataReader odbr_xcargo = odcommandgoods.ExecuteReader();
while (odbr_xcargo.Read())
{
if (BL_No1 != BL_No2)
{
string GOODS_NAME = "鏈‘瀹?;
if(odbr_xcargo[6].ToString() == "E")
GOODS_NAME = "绌虹";
if (odbr_xcargo[0].ToString() != "")
GOODS_NAME = odbr_xcargo[0].ToString();
Console.WriteLine(odbr_xcargo[0].ToString());
XElement cxele = root.Element("MANIFEST").Element("BILL").Element("REMARK");
cxele.AddAfterSelf(new XElement("CARGO",
new XElement("CARGO_SEQ_NO", cno),
new XElement("GOODS_NAME", GOODS_NAME),
new XElement("GOODS_HS", odbr_xcargo[1].ToString()),
new XElement("GOODS_JT", odbr_xcargo[2].ToString()),
new XElement("MARK_CONTR", odbr_xcargo[3].ToString()),
new XElement("GROSS_WT", odbr_xcargo[4].ToString()),
new XElement("VOLUME", odbr_xcargo[5].ToString())
)
);
Console.WriteLine(cno);
cno = cno + 1;
}
BL_No2 = BL_No1;
}
odbr_xcargo.Close();
int Cn_Num = 0;
OleDbCommand odcommandCnNum = conn_access.CreateCommand();
odcommandCnNum.CommandText = "select语句";
OleDbDataReader CnNum1 = odcommandCnNum.ExecuteReader();
bool ifcom2 = CnNum1.Read();
if (ifcom2)
{
Cn_Num = Convert.ToInt32(CnNum1[0].ToString());
}
CnNum1.Close();
N1.Close();
OleDbCommand odcommandcntr = conn_access.CreateCommand();
odcommandcntr.CommandText = "select语句";
OleDbDataReader odbr_cntr = odcommandcntr.ExecuteReader();
while (odbr_cntr.Read())
{
XElement cntr_xel = root.Element("MANIFEST").Element("BILL").Element("CARGO");
cntr_xel.AddAfterSelf(new XElement("CONTAINER",
new XElement("CONTA_SEQ_NO", Cn_Num ),
new XElement("CONTA_NO", odbr_cntr[1].ToString()),
new XElement("ISO_TYPE", odbr_cntr[2].ToString()),
new XElement("CONTA_STATUS", odbr_cntr[3].ToString()),
new XElement("SEAL_NO", odbr_cntr[4].ToString())
)
);
Cn_Num-- ;
}
odbr_cntr.Close();
}
}
odbr_xbl.Close();
root.Save(@filename); this.Cursor = Cursors.Arrow;
}
MessageBox.Show(filename + "鏂囦欢宸茬敓鎴?");
conn_access.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
{
try
{
string filename = "D:\\C#\\" + VSL.Text.Trim() + VVD.Text.Trim() + ".xml";
if (File.Exists(filename))
{
}
else
{
File.Create(filename);
}
int BL_NumALl=0;
OleDbConnection conn_access = new OleDbConnection();
conn_access.ConnectionString = SQLOld_Conn.M_str_ldconn;
conn_access.Open()
OleDbCommand odcommandNum = conn_access.CreateCommand();
odcommandNum.CommandText = "select 语句";
OleDbDataReader N1 = odcommandNum.ExecuteReader();
bool ifcom1=N1.Read();
if (ifcom1)
{
BL_NumALl = Convert.ToInt32(N1[0].ToString());
}
N1.Close();
OleDbCommand odcommand = conn_access.CreateCommand();
odcommand.CommandText ="select 语句";
OleDbDataReader odbr = odcommand.ExecuteReader();
bool ifcom = odbr.Read();
if (ifcom)
{
DateTime.Now.ToShortTimeString();
DateTime dt = Convert.ToDateTime(odbr[20].ToString());
string SendTime = string.Format("{0:yyyyMMddHHmmss}",dt);
System.Xml.Linq.XDocument xdoc = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"));
XElement root = new XElement("MESSAGE",
new XElement("MESSAGE_ID", odbr[0].ToString()),
new XElement("MESSAGE_DESC", odbr[1].ToString()),
new XElement("RECEIVERS",
new XElement("RECEIVER", odbr[6].ToString())
),
new XElement("MANIFEST",
new XElement("I_E_FLAG", odbr[7].ToString()),
new XElement("I_F_FLAG", odbr[8].ToString()),
new XElement("EMPTY_CONTA_NUM", odbr[13].ToString()),
new XElement("VSL_VOY",
new XElement("SHIP_ID", odbr[14].ToString()),
new XElement("I_E_DATE", SendTime),
new XElement("L_D_WHARF_NAME", odbr[24].ToString())
)
)
);
odbr.Close();
odcommand.CommandText = "select语句";
OleDbDataReader odbr_xbl = odcommand.ExecuteReader();
string BL_No2 = "Nulll";
while (odbr_xbl.Read())
{
string BL_NO_TMP = odbr_xbl[1].ToString();
string BL_No1 = odbr_xbl[1].ToString();
if (BL_No1 != BL_No2)
{
XElement xele = root.Element("MANIFEST").Element("VSL_VOY");
xele.AddAfterSelf(new XElement("BILL",
new XElement("BILL_SEQ_NO", BL_NumALl ),
new XElement("BILL_NO", odbr_xbl[1].ToString()),
new XElement("SHIPPER", odbr_xbl[2].ToString()),
new XElement("CONSIGNEE", odbr_xbl[3].ToString()),
new XElement("NOTIFY", odbr_xbl[4].ToString()),
new XElement("LOAD_PORT", odbr_xbl[5].ToString()),
new XElement("LOAD_COUNTRY", odbr_xbl[6].ToString()),
new XElement("DISCHARGE_PORT", odbr_xbl[7].ToString()),
new XElement("DISCHARGE_COUNTRY", odbr_xbl[8].ToString()),
new XElement("BILL_GROSS_WT", odbr_xbl[9].ToString()),
new XElement("BILL_CONTA_NUM", odbr_xbl[10].ToString()),
new XElement("REBATE", odbr_xbl[11].ToString()),
new XElement("TRANSIT", odbr_xbl[12].ToString()),
new XElement("REMARK", odbr_xbl[13].ToString())
)
);
BL_NumALl-- ;
int cno = 1;
OleDbCommand odcommandgoods = conn_access.CreateCommand();
odcommandgoods.CommandText = "select语句";
OleDbDataReader odbr_xcargo = odcommandgoods.ExecuteReader();
while (odbr_xcargo.Read())
{
if (BL_No1 != BL_No2)
{
string GOODS_NAME = "鏈‘瀹?;
if(odbr_xcargo[6].ToString() == "E")
GOODS_NAME = "绌虹";
if (odbr_xcargo[0].ToString() != "")
GOODS_NAME = odbr_xcargo[0].ToString();
Console.WriteLine(odbr_xcargo[0].ToString());
XElement cxele = root.Element("MANIFEST").Element("BILL").Element("REMARK");
cxele.AddAfterSelf(new XElement("CARGO",
new XElement("CARGO_SEQ_NO", cno),
new XElement("GOODS_NAME", GOODS_NAME),
new XElement("GOODS_HS", odbr_xcargo[1].ToString()),
new XElement("GOODS_JT", odbr_xcargo[2].ToString()),
new XElement("MARK_CONTR", odbr_xcargo[3].ToString()),
new XElement("GROSS_WT", odbr_xcargo[4].ToString()),
new XElement("VOLUME", odbr_xcargo[5].ToString())
)
);
Console.WriteLine(cno);
cno = cno + 1;
}
BL_No2 = BL_No1;
}
odbr_xcargo.Close();
int Cn_Num = 0;
OleDbCommand odcommandCnNum = conn_access.CreateCommand();
odcommandCnNum.CommandText = "select语句";
OleDbDataReader CnNum1 = odcommandCnNum.ExecuteReader();
bool ifcom2 = CnNum1.Read();
if (ifcom2)
{
Cn_Num = Convert.ToInt32(CnNum1[0].ToString());
}
CnNum1.Close();
N1.Close();
OleDbCommand odcommandcntr = conn_access.CreateCommand();
odcommandcntr.CommandText = "select语句";
OleDbDataReader odbr_cntr = odcommandcntr.ExecuteReader();
while (odbr_cntr.Read())
{
XElement cntr_xel = root.Element("MANIFEST").Element("BILL").Element("CARGO");
cntr_xel.AddAfterSelf(new XElement("CONTAINER",
new XElement("CONTA_SEQ_NO", Cn_Num ),
new XElement("CONTA_NO", odbr_cntr[1].ToString()),
new XElement("ISO_TYPE", odbr_cntr[2].ToString()),
new XElement("CONTA_STATUS", odbr_cntr[3].ToString()),
new XElement("SEAL_NO", odbr_cntr[4].ToString())
)
);
Cn_Num-- ;
}
odbr_cntr.Close();
}
}
odbr_xbl.Close();
root.Save(@filename); this.Cursor = Cursors.Arrow;
}
MessageBox.Show(filename + "鏂囦欢宸茬敓鎴?");
conn_access.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
文件写完要Close,不Close的话,就一直被进程占用着,下次就写不了了
File.Close(filename);
conn_access.Close();
报错,提示
system.IO.File并不包含“Close”的定义
try
{
string filename = "D:\\C#\\" + VSL.Text.Trim() + VVD.Text.Trim() + ".xml";
FileStream fs = File.Create(filename);..... MessageBox.Show(filename + "文件已生成!");
conn_access.Close();
fs.Close();}还是报错啊。
{}简单的记为:有流的地方都用using 包裹
{
this.Cursor = Cursors.WaitCursor;
string filename = "D:\\C#\\" + VSL.Text.Trim() + VVD.Text.Trim() + ".xml";
using (FileStream fs = File.Create(filename))
{
.....................
this.Cursor = Cursors.Arrow;
}
MessageBox.Show(filename + "文件已生成!");
conn_access.Close();
fs.Close();
}
}
catch (Exception ex)晕,还是报错啊。比原来没改时更糟糕!原来还时好时报错,该了后一按导出XML就报原来的错。
各位大虾麻烦再看看!
加个
finally
{
对象名.Close
}
这样总是会关闭