2,ibatisnet
c#的和java的基本没有太大区别,sqlMap写法都是一样的
使用的数据库与java的一样。
贴出代码。
Maps.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace MapUpdate
{
class Maps
{
private int _id;
public int id
{
get { return _id; }
set { _id = value; }
}
private string _eName;
public string eName
{
get { return _eName; }
set { _eName = value; }
}
private string _imageName;
public string imageName
{
get { return _imageName; }
set { _imageName = value; }
}
private int _xmin;
public int xmin
{
get { return _xmin; }
set { _xmin = value; }
}
private int _ymin;
public int ymin
{
get { return _ymin; }
set { _ymin = value; }
}
private int _xmax;
public int xmax
{
get { return _xmax; }
set { _xmax = value; }
}
private int _ymax;
public int ymax
{
get { return _ymax; }
set { _ymax = value; }
}
private string _scale;
public string scale
{
get { return _scale; }
set { _scale = value; }
}
private string _valid;
public string valid
{
get { return _valid; }
set { _valid = value; }
}
}
}
FileControl.cs
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data;
using System.Xml;
using log4net;
namespace MapUpdate
{
class FileControl
{
#region common define
private int total = 0;
//private int FileNumber = 0;
private StreamWriter sw1;
private StreamReader sr1;
private string FileName;
private string TextString;
private static string DEFAULT_PATH = "\\\\192.168.7.44\\数字地图";//"\\\\172.27.1.199\\数字地图";
#endregion
private string appPath = Application.ExecutablePath + ".config";
private XmlDocument configData = new XmlDocument();
ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
///
/// GetAppValue(String Key) /// ///
///
public string GetAppValue(string key)
{
return configData.SelectSingleNode("/configuration/appSettings/add[@key='" + key + "']").Attributes["value"].Value;
}
///
/// 默认根目录 /// ///
DEFAULT_PATH public string getDefaultPath()
{
return DEFAULT_PATH;
}
///
/// 获得总文件数 /// ///
public int getTotal()
{
return this.total;
}
///
/// 设置文件名 /// ///
public void SetFileName(string str)
{
this.FileName = str;
}
///
/// 打开文件 /// public void OpenFile()
{
if (File.Exists(FileName))
{
sw1 = File.AppendText(FileName);
}
else
{
sw1 = File.CreateText(FileName);
}
}
///
/// 删除文件 /// public void DeleteFile()
{
if (File.Exists(FileName))
{
File.Delete(FileName);
}
}
///
/// 读取文件 /// public void ReadFile()
{
if (File.Exists(FileName))
{
try
{
sr1 = File.OpenText(FileName);
}
catch (Exception ex)
{
//DebugLog.WriteLog(ex);
log.Debug(ex);
}
}
else
{
//DebugLog.Debuglog("文件不存在");
log.Debug("文件不存在");
}
}
///
/// 按行读取文件 /// ///
public string ReadFileByLine()
{
TextString = sr1.ReadLine();
return this.TextString;
}
///
/// 写字符串到文件 /// ///
public void WriteFile(string str)
{
if (File.Exists(FileName))
{
sw1.WriteLine(str);
}
else
{
sw1 = File.CreateText(FileName);
sw1.WriteLine(str);
}
}
///
/// 写object类型到文件 /// ///
public void WriteFile(object obj)
{
if (File.Exists(FileName))
{
sw1.WriteLine(obj.ToString());
}
else
{
sw1 = File.CreateText(FileName);
sw1.WriteLine(obj.ToString());
}
}
///
/// 关闭文件 /// public void CloseFile()
{
sw1.Close();
}
///
/// 关闭reader /// public void CloseReader()
{
try
{
sr1.Close();
}
catch (Exception ex)
{
//DebugLog.WriteLog(ex);
log.Debug(ex.Message);
}
}
///
/// 拷贝目录 /// ///
///
///
public void CopyDirectory(string sourceDirName, string destDirName, string pipei)
{
FileControl fc = new FileControl();
fc.SetFileName(Environment.CurrentDirectory + "\\path.txt");
fc.OpenFile();
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
if (destDirName[destDirName.Length - 1] != Path.DirectorySeparatorChar)
destDirName = destDirName + Path.DirectorySeparatorChar;
string[] files = Directory.GetFiles(@sourceDirName, pipei);
foreach (string file in files)
{
File.Copy(file, destDirName + Path.GetFileName(file), true);
fc.WriteFile(destDirName + Path.GetFileName(file));
File.SetAttributes(destDirName + Path.GetFileName(file), FileAttributes.Normal);
total++;
}
fc.CloseFile();
string[] dirs = Directory.GetDirectories(sourceDirName);
foreach (string dir in dirs)
{
CopyDirectory(dir, destDirName + Path.GetFileName(dir), pipei);
}
//return total;
}
///
/// 生成给定的路径下的文件路径 /// ///
///
///
public void GenerateFilePath(string sourceDirName, string pipei, string indexFile)
{
FileControl fc = new FileControl();
fc.SetFileName(Environment.CurrentDirectory + "\\" + indexFile);
fc.OpenFile();
string[] files = Directory.GetFiles(@sourceDirName, pipei);
foreach (string file in files)
{
if (sourceDirName.LastIndexOf("\\") != sourceDirName.Length - 1)
{
fc.WriteFile(sourceDirName + "\\" + Path.GetFileName(file));
}
else
{
fc.WriteFile(sourceDirName + Path.GetFileName(file));
}
total++;
}
fc.CloseFile();
}
///
/// 先取出所有初步筛选的文件路径,然后在路径中搜索文件,按文件路径直接复制 /// 第一步,生成一个存放文件路径的文件 /// ///
///
///
///
public void GenerateCopyPath(string sourceDirName, string destDirName, string pipei, string indexFile)
{
FileControl fc = new FileControl();
fc.SetFileName(Environment.CurrentDirectory + "\\" + indexFile);
fc.OpenFile();
// if (!Directory.Exists(destDirName))
// {
// Directory.CreateDirectory(destDirName);
// }
//if (destDirName[destDirName.Length - 1] != Path.DirectorySeparatorChar)
// destDirName = destDirName + Path.DirectorySeparatorChar;
string[] files = Directory.GetFiles(@sourceDirName, pipei);
foreach (string file in files)
{
if (sourceDirName.LastIndexOf("\\") != sourceDirName.Length - 1)
{
fc.WriteFile(sourceDirName + "\\" + Path.GetFileName(file));
}
else
{
fc.WriteFile(sourceDirName + Path.GetFileName(file));
}
//File.SetAttributes(destDirName + Path.GetFileName(file), FileAttributes.Normal);
total++;
}
fc.CloseFile();
string[] dirs = Directory.GetDirectories(sourceDirName);
foreach (string dir in dirs)
{
GenerateCopyPath(dir, destDirName + Path.GetFileName(dir), pipei, indexFile);
}
}
///
/// 从完整路径名中获得文件名 /// ///
///
public string getFileNameFromPath(string path)
{
string temp = path;
int num = temp.LastIndexOf("\\");
temp = path.Substring(num + 1);
return temp;
}
///
/// 从完整路径名中获得除去文件名的路径 /// ///
///
public string getFilePathFromPath(string path)
{
string temp = path;
int num = temp.LastIndexOf("\\");
temp = temp.Substring(0, num);
return temp;
}
///
/// 第二步,开始拷贝,从文件里面的路径查询是否存在需要的文件,拷贝 /// ///
///
///
///
///
//public void MutiCopyPath(string sourceDirName, string destDirName, string pipei, string fileNamePathAll, string pipeiFilePathAll)
//{
// DBFactory dbf = new DBFactory();
// DataSet ds = new DataSet();
// DataTable dt = new DataTable();
// DataTable dt1 = new DataTable();
// OleDbDataReader dr;
// string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Environment.CurrentDirectory + "\\" + ";Extended Properties=\"Text;HDR=yes;FMT=Delimited;\";";
// OleDbConnection con = new System.Data.OleDb.OleDbConnection(connStr);
// con.Open();
// //有匹配规则内容的文件
// string pipeiFileName = getFileNameFromPath(pipeiFilePathAll);
// string pipeiFilePath = getFilePathFromPath(pipeiFilePathAll);
// FileStream fs = new FileStream(Environment.CurrentDirectory + "\\out.txt", FileMode.Append, FileAccess.Write);
// StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Unicode);
// //所有路径存放的文件
// string fileName = getFileNameFromPath(fileNamePathAll);
// string fileNamePath = getFilePathFromPath(fileNamePathAll);
// string sql = "select * from " + fileName;
// string sql1 = "select * from " + pipeiFileName;
// OleDbDataAdapter adapter = new OleDbDataAdapter(sql, con);
// adapter.Fill(ds, "text1");
// int i = ds.Tables["text1"].Rows.Count;
// OleDbDataAdapter adapter1 = new OleDbDataAdapter(sql1, con);
// adapter1.Fill(ds, "text2");
// int j = ds.Tables["text2"].Rows.Count;
// string sql2 = "select a.path from " + fileName + " a where a.path like '*" + pipeiFileName + "*'";
// //string sql3 = "select * from " + fileName;
// OleDbCommand comm = new OleDbCommand(sql2, con);
// try
// {
// int recordNumbers = comm.ExecuteNonQuery();
// dr = comm.ExecuteReader();
// int recs = 1;
// while (dr.Read())
// {
// //dr.GetValue(1);
// //sw.WriteLine(dr.GetString(0). + " " + recs);
// string ss = dr.GetValue(0).ToString();
// //DebugLog.WriteLog(dr.GetValue(0)+" "+recs);
// recs++;
// }
// dr.Close();
// if (recordNumbers != 0)
// {
// DebugLog.WriteLog("ok!");
// }
// comm.Dispose();
// sw.Close();
// fs.Close();
// }
// catch (Exception ex)
// {
// //DebugLog.Debuglog(ex);
// log.Debug(ex.Message());
// comm.Dispose();
// sw.Close();
// fs.Close();
// }
//}
///
/// 第二步的第二种方法:直接用字符串匹配来找路径 /// ///
///
///
public void MutiCopyPath(string pipei, string oraPath, string tarPath)
{
string pipeiStr = pipei; //匹配的字符串
string raPathStr = oraPath; //路径
string tarPathStr = tarPath; //路径
//string indexPathStr = indexPath; //文件名 默认index.txt
string indexFileName = "index.txt";
StreamReader sr1 = new StreamReader(Environment.CurrentDirectory + "\\" + indexFileName);
//ArrayList al = new ArrayList ();
string sr1Str;
while ((sr1Str = sr1.ReadLine()) != null)
{
string fileName = sr1Str;
int num = sr1Str.LastIndexOf("\\");
fileName = fileName.Substring(num + 1);
string sr1Temp = sr1Str;
int numTemp = sr1Str.LastIndexOf("\\");
sr1Temp = sr1Temp.Substring(numTemp + 1);
if (pipeiStr != "" && sr1Str != "")
{
try
{
if (sr1Temp.LastIndexOf(pipeiStr) >= 0)
{
//string files = Directory.GetFiles(sr1Str);
File.Copy(sr1Str, tarPathStr + "\\" + fileName);
}
}
catch (Exception ex)
{
//DebugLog.WriteLog(ex);
log.Debug(ex.Message);
}
}
}
}
///
/// 获得文件夹内的文件总数 /// ///
///
public static int GetFilesCount(System.IO.DirectoryInfo dirInfo)
{
int totalFile = 0;
totalFile += dirInfo.GetFiles().Length;
foreach (System.IO.DirectoryInfo subdir in dirInfo.GetDirectories())
{
totalFile += GetFilesCount(subdir);
}
return totalFile;
}
}
}
provider.config 数据库驱动
xmlns="http://ibatis.apache.org/providers" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> name="sqlServer2.0" enabled="true" description="Microsoft SQL Server, provider V2.0.0.0 in framework .NET V2.0" assemblyName="System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" connectionClass="System.Data.SqlClient.SqlConnection" commandClass="System.Data.SqlClient.SqlCommand" parameterClass="System.Data.SqlClient.SqlParameter" parameterDbTypeClass="System.Data.SqlDbType" parameterDbTypeProperty="SqlDbType" dataAdapterClass="System.Data.SqlClient.SqlDataAdapter" commandBuilderClass=" System.Data.SqlClient.SqlCommandBuilder" usePositionalParameters = "false" useParameterPrefixInSql = "true" useParameterPrefixInParameter = "true" parameterPrefix="@" allowMARS="false" /> name="oracleClient1.0" description="Oracle, Microsoft provider V1.0.5000.0" enabled="true" assemblyName="System.Data.OracleClient, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" connectionClass="System.Data.OracleClient.OracleConnection" commandClass="System.Data.OracleClient.OracleCommand" parameterClass="System.Data.OracleClient.OracleParameter" parameterDbTypeClass="System.Data.OracleClient.OracleType" parameterDbTypeProperty="OracleType" dataAdapterClass="System.Data.OracleClient.OracleDataAdapter" commandBuilderClass="System.Data.OracleClient.OracleCommandBuilder" usePositionalParameters="false" useParameterPrefixInSql="true" useParameterPrefixInParameter="false" parameterPrefix=":" allowMARS="false" /> SqlMap.config
<!-- --> <!-- Data Source --> <!-- --> <!-- Sql Map files --> Maps.xml
<!-- 测试:for oracle --> SELECT * FROM mapsall06 WHERE id = #id# and rownum <5 ]]> <!-- 测试:for sqlserver2000 --> SELECT top 5 * FROM mapsall06 WHERE id = #id# ]]> <!-- 找出当前表内id的最大值 --> select max(id) as id from mapsall06 <!-- 初始化,删除现有记录 --> delete from mapsall06 <!-- 测试: insert use map--> insert into mapsall06 (id, eName, imageName, xmin, ymin, xmax, ymax, scale, valid) values (#id#, #eName#, #imageName#, #xmin#, #ymin#, #xmax#, #ymax#, #scale#, #valid#) <!-- 插入图形信息 --> insert into mapsall06 (id, eName, imageName, xmin, ymin, xmax, ymax, scale, valid) values (#id#, #eName#, #imageName#, #xmin#, #ymin#, #xmax#, #ymax#, #scale#, #valid#) <!-- 更新比例尺字段,去掉\new --> update mapsall06 set scale = replace(scale, '\new', '') where length(scale) > 4 ]]> <!-- 将有对应新图的旧图的valid值置为2 for oracle9i or sqlserver2000--> UPDATE mapsall06 SET valid = '2' WHERE (id IN (SELECT id FROM (SELECT eName, MIN(id) AS id FROM mapsall06 GROUP BY eName HAVING COUNT(*) > 1) DERIVEDTBL)) ]]> select id, xmin, ymin, xmax, ymax, scale, valid from mapsall06 where scale = #scale# and valid = #valid# select count(*) as ct from mapsall06 where xmin >= #xmin# and ymin >= #ymin# and xmax <= #xmax# and ymax <= #ymax# and scale = #scale# and valid = #valid# ]]> update mapsall06 set valid='0' where id = #id# and valid = '1' update mapsall06 set valid = #in_valid# where xmin >= #xmin# and ymin >= #ymin# and xmax <= #xmax# and ymax <= #ymax# and scale = #scale# and valid = #valid# ]]> update mapsall06 set valid = '0' where id = #id# and valid <> '2' ]]> update mapsall06 set valid = '0' where valid = '3' app.config 主要是log4net的配置,由于c#的简易log和console与java的一样
这里使用log4net,以免重复。
<!--定义输出到文件中--> <!--定义文件存放位置--> <!--每条日志末尾的文字说明--> <!--输出格式--> <!--样例:2008-03-26 13:42:32,111 [10] INFO Log4NetDemo.MainClass [(null)] - info--> <!--定义输出到控制台命令行中--> <!--定义输出到windows事件中--> <!--定义输出到数据库中,这里举例输出到Access数据库中,数据库为C盘的log4net.mdb--> <!--定义各个参数--> <!--定义日志的输出媒介,下面定义日志以四种方式输出。也可以下面的按照一种类型或其他类型输出。--> <!--文件形式记录日志--> <!--控制台控制显示日志--> <!--Windows事件日志--> <!-- 如果不启用相应的日志记录,可以通过这种方式注释掉 --> 使用log4net注意在assemblyInfo.cs后面加上一行
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
MapsUpdate.cs
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using log4net;
using IBatisNet.Common;
using IBatisNet.DataMapper;
using System.Reflection;
using IBatisNet.DataMapper.Configuration;
using System.IO;
namespace MapUpdate
{
class MapsUpdate
{
#region define
public int id = 0;
public int x1 = 0;
public int x2 = 0;
public int y1 = 0;
public int y2 = 0;
public string scale = "";
public string valid = "";
public static string[] mapPath = { "500", "1000", "2000", "500\\new", "1000\\new", "2000\\new" };
ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#endregion
///
/// /// public MapsUpdate()
{
Test();
}
///
/// 测试 /// public void Test()
{
//doGenFilePath();
//doInit();
//doMapInsert();
//doMapScaleupdate();
doMapUpdate();
}
///
/// 初始化,清空数据库(检测连接) /// public void doInit()
{
DomSqlMapBuilder builder = new DomSqlMapBuilder();
ISqlMapper sqlMap = builder.Configure("SqlMap.config");
try
{
sqlMap.BeginTransaction();
//sqlMap.OpenConnection();
//Maps map = new Maps();
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/16179598/viewspace-539593/,如需转载,请注明出处,否则将追究法律责任。