using System;
using System.Collections;
using System.IO;
using System.Text;
using CNEE.IF.VCY.Entity3D;
using MySql.Data.MySqlClient;
using System.Data;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
//
using System.Data.SqlClient; // Use SQL Server data provider namespace
using System.Collections.Specialized;
namespace CNEE.IF.VCY
{
///
/// boxInfo_withMySql 的摘要说明。连接数据库信息
///
public class boxInfo_withMySql
{
///
/// 构造函数
///
public boxInfo_withMySql()
{
//
// TODO: 在此处添加构造函数逻辑,如何解决并发行数据修改?
//
//conn.ConnectionString="";
mysqlTables=new ArrayList();
mysqlDatabase=new ArrayList();
tableDate=new ArrayList();
}
///
/// 连接字段
///
private MySql.Data.MySqlClient.MySqlConnection conn;
private MySqlDataAdapter da;
//private System.Windows.Forms.DataGrid dataGrid;
private MySqlCommandBuilder cb;
private DataTable data;
///
/// 读取指定数据库后保存的所有数据表名
///
public IList mysqlTables;
///
/// 连接数据库后保持的所有数据名称
///
public IList mysqlDatabase;
///
/// 读取指定数据表保存的数据(一行一个对象)
///
public IList tableDate;
public bool islink=false;
#region
///
/// 连接mysql服务器,显示所有数据库名,保存在动态数组mysqlDatabase中
///
public void GetDatabases()
{
MySqlDataReader reader = null;
MySqlCommand cmd = new MySqlCommand("SHOW DATABASES", conn);
try
{
reader = cmd.ExecuteReader();
if(mysqlDatabase==null)
mysqlDatabase=new ArrayList();
mysqlDatabase.Clear();
while (reader.Read())
{
mysqlDatabase.Add( reader.GetString(0) );
}
//return true;//连接成功
}
catch (MySqlException ex)
{
MessageBox.Show("连接MySql数据库失败:/n " + ex.Message );
//return false;
}
finally
{
if (reader != null) reader.Close();
}
}
///
/// 连接数据库,显示指定数据库databaseName中的所有数据表,保存在mysqlTable动态数组中
///
public void GetDatabaseTables(string databaseName)
{
MySqlDataReader reader = null;
conn.ChangeDatabase( databaseName );
MySqlCommand cmd = new MySqlCommand("SHOW TABLES", conn);
try
{
if(mysqlTables==null)
mysqlTables=new ArrayList();
reader = cmd.ExecuteReader();
mysqlTables.Clear();
while (reader.Read())
{
mysqlTables.Add( reader.GetString(0) );
}
}
catch (MySqlException ex)
{
MessageBox.Show("Failed to populate table list: " + ex.Message );
}
finally
{
if (reader != null) reader.Close();
//this.Refresh();
}
}
//public static string server,user,password;
#endregion
///
/// 连接数据库(是否成功)
///
/// 如果连接成功返回真
public bool linkMySql(string mysqlServer,string user,string password)
{
if (conn != null)
conn.Close();
islink=false;
string connStr = String.Format("server={0};user id={1}; password={2}; database=mysql; pooling=false",
mysqlServer, user, password );
try
{
//conn.ConnectionString=connStr;
//if(conn==null)
conn = new MySqlConnection( connStr );
conn.Open();
islink=true;
return true;
//多线程使用
//Thread th=new Thread(new ThreadStart(GetDatabases));
//th.IsBackground = true;
//th.Name = "loadxfile";
//th.Start();
}
catch (MySqlException ex)
{
MessageBox.Show( "Error connecting to the server: " + ex.Message );
return false;
}
return false;//这一步是运行不到的
}
///
/// 读取数据库配置文件,连接指定的数据,填充堆场和轮船中的箱子
///
private void ReadXml()
{
ArrayList ArryList11 = new ArrayList();
ArrayList ArryList12 = new ArrayList();
//假设你的xml文件名为demo.xml确保路径正确
string xmlPath = "demo.xml";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
XmlNodeList xnl = xmlDoc.SelectSingleNode("Configuration").ChildNodes;
foreach (XmlNode node in xnl)
{
foreach (XmlNode emailnode in node.SelectNodes("connstr"))
{
ArryList11.Add(emailnode.InnerText);
}
foreach (XmlNode keywordnode in node.SelectNodes("KeyWord"))
{
ArryList12.Add(keywordnode.InnerText);
}
}
}
///
/// 读取xml连接数据库配置文件得到connstr字符串信息
///
///
public static string readXmlSqlconn()
{
XmlDocument doc = new XmlDocument();
doc.Load("ConnMysql.xml");
//获得根节点下的所有子节点
XmlNodeList xnl = doc.SelectSingleNode("Configuration").ChildNodes;
//获得所有子节点group
string ConnStr="";
foreach (XmlNode node in xnl)
{ //遍历所有group子节点
foreach (XmlNode connNode in node.SelectNodes("connStr"))
{ //遍历group子节点的子节点,选择匹配的节点
ConnStr=connNode.InnerText;
}
}
return ConnStr;
//
}
///
/// 读取xml配置文件连接数据库
///
///
///
public bool linkMySql(string connString)
{
if(conn!=null)
conn.Close();
islink=false;
string connStr=String.Format(connString);
try
{
conn=new MySqlConnection(connStr);
conn.Open();
islink=true;
return true;
}
catch(MySqlException ex)
{
MessageBox.Show(ex.Message);
return false;
}
return false;
}
///
/// 断开连接
///
///
public void closeConn()
{
if(conn!=null)
conn.Close();
islink=false;
}
///
/// 把数据库表内容显示到DataGrid中
///
///
///
///
public void showDataTableSouse(string DatabaseName,string DatetableName,ref DataGrid dv)
{
try
{
conn.ChangeDatabase( DatabaseName);
data = new DataTable();
data.TableName=DatabaseName;
//
da = new MySqlDataAdapter("SELECT * FROM " + DatetableName, conn );
cb = new MySqlCommandBuilder( da );
//
da.Fill( data );
//dv=new DataView();
//da.Fill(dv.Table);
dv.DataSource=data;
}
catch{MessageBox.Show("失败");}
}
///
/// 连接指定的数据库DatabaseName中的数据表DatetableName,返回一个数据表DataTable
///
///
///
///
public DataTable readTabletoSave(string DatabaseName,string DatetableName)
{
try
{
conn.ChangeDatabase( DatabaseName);
data = new DataTable();
//
da = new MySqlDataAdapter("SELECT * FROM " + DatetableName, conn );
cb = new MySqlCommandBuilder( da );
//
da.Fill( data );
//
//下面保存到动态数组tableData中
if(tableDate==null)
tableDate=new ArrayList();
tableDate.Clear();
for(int row=0;row
{
string rowstr="";
object[] row_allColunbs=data.Rows[row].ItemArray;
foreach(object str in row_allColunbs)
{
if(str.ToString()=="System.Byte[]")
rowstr+=",";//最后多了一个空格
else
rowstr+=str.ToString()+",";
}
//rowstr=rowstr.Substring(0,rowstr.Length-2);
//rowstr.Replace("System.Byte[]"," ");//换成 , 符号分割
//char[]dot={','};
//rowstr.TrimEnd(dot);
//rowstr.Replace("System.Byte[]"," ");
string st= rowstr.Remove(rowstr.Length-1,1);
tableDate.Add(st);//把数据保持到tableDate动态数组中
//for(int lie=0;lie
//{
//读取指定的单元格,怎么改成一行数据呢?
//}
}
//把数据写进xml中
DataSet thisDataSet = new DataSet();
da.Fill(thisDataSet,"MysqlDateInfo");//填充到内存缓冲表boxInfo中
XmlDataDocument daxml=new XmlDataDocument(thisDataSet);
//thisDataSet.WriteXml(@"/boxInfo.xml");//写入到xml中
//创建文件夹
if(!Directory.Exists(DatabaseName))
{
Directory.CreateDirectory(DatabaseName);//创建mysql指定数据库名目录
}
daxml.DataSet.WriteXml(DatabaseName+"//"+DatetableName+".xml");
//
//把数据保持到xml.dat中去
SaveOrReadYardBoxInfo.saveIList(tableDate,DatabaseName+"//"+DatetableName);
//
//下面保存数据表标题
//怎么读取?
string strs="";
for(int i=0;i
{
strs+=data.Columns[i].ColumnName.ToString()+",";
}
string str2= strs.Remove(strs.Length-1,1);
SaveOrReadYardBoxInfo.saveString(str2,DatabaseName+"//"+DatetableName+"._Columns"); //+"_Columns");
return data;
//dataGrid1.DataSource = data;
}
catch(MySqlException ex)
{
MessageBox.Show(ex.ToString());
//M
return null;
}
}
///
/// 从Mysql中读取堆场箱子信息(未完成)
///
///
private static IList readYardboxInfo()
{
IList yardboxinfo=new ArrayList();
//
return yardboxinfo;
}
///
/// 把堆场箱子信息保存到数据库(未完成)
///
private static void saveYardboxInfo()
{
//数据库字段很多,这个怎么写?cneecy/container显示在场数据
//
}
///
/// 从MYSQL数据库中读取指定轮船的箱子信息(未完成)
///
///
private static IList readVessileboxInfo()
{
IList vessileboxinfo=new ArrayList();
//读取数据库数据cneecy/container显示在场数据
//读取xml链接数据库配置信息
//
return vessileboxinfo; } /// /// 保存指定轮船的箱子信息到Mysql数据库(未完成) /// /// private static void saveVessileboxInfo(int vessileId) { //轮船箱子信息数据表cydoc/stowageplandetail } }}