?? cupdatetransition.cs
字號:
using System;
using System.Windows.Forms;
using System.Collections;
using System.Data.OleDb;
namespace DataC
{
public struct TFieldAssociate
{
public string m_strTargetField;
public string m_strSourceField;
public string m_strTargetFieldType;
public string m_strSourceFieldType;
}
/// <summary>
/// 更新轉換類
/// 編制人:朱銘 2005-5-17
/// 功能:實現更新轉換,即在第一次導入數據之后,對剩余數據字段的導入。
/// 成員變量:_alFieldAssociate---是一個TFieldAssociate的結構數組。每個元素保存著在更新轉換時所需要的信息,即update中的where子句。
/// </summary>
public class CUpdateTransition:CTransition
{
protected
System.Collections.ArrayList _alFieldAssociate;
System.Collections.Hashtable _htTargetFieldAndType;
public CUpdateTransition()
{
_alFieldAssociate=new ArrayList();
_htTargetFieldAndType=new Hashtable();
}
public System.Collections.ArrayList FieldAssociate
{
get { return _alFieldAssociate;}
set { _alFieldAssociate=value;}
}
public System.Collections.Hashtable TargetFieldAndType
{
get { return _htTargetFieldAndType;}
set { _htTargetFieldAndType=value;}
}
/// <summary>
/// 更新轉換函數。
/// 問題:在關聯鍵值(即原標的主鍵為空值)為空時的處理方法。
/// </summary>
public void UpdateTransStart()
{
ArrayList strsMBZDL=this.TargetFieldList;
ArrayList strsMBZDLXL=this.TargetFieldListType;
string strTCmd=null;
string strSet;;
string strWhere;
string strValue="";
if(this.SourceConn.State==System.Data.ConnectionState.Closed)
this.SourceConn.Open();
CError ce1=new CError();
OleDbCommand dcY=this.SourceConn.CreateCommand();
try
{
dcY.CommandText=this.GenerateSourceSql(ce1);
}
catch(CError ce)
{
MessageBox.Show(ce.Msg);
return;
}
OleDbDataReader drY=dcY.ExecuteReader();
if(this.TargetConn.State==System.Data.ConnectionState.Closed)
this.TargetConn.Open();
OleDbCommand dcM=this.TargetConn.CreateCommand();
while(drY.Read())
{
strSet=" set ";
try
{
strTCmd="update "+this.TargetTable[0].ToString();
for(int j=0;j<strsMBZDL.Count;j++)
{
strSet+=strsMBZDL[j].ToString();
CFieldRelation relation=new CFieldRelation();
relation=(CFieldRelation)this.FieldRelation[j];
if(relation.TransType=="直接轉換")
{
if(strsMBZDLXL[j].ToString()=="System.String")
{
if(drY.IsDBNull(j+this.FieldAssociate.Count))
strValue="";
else
//strValue=(string)drY.GetValue(j+this.FieldAssociate.Count);
strValue=drY[j+this.FieldAssociate.Count].ToString();
strSet+="='"+strValue+"'";
}
else if(strsMBZDLXL[j].ToString()=="System.Decimal")
{
if(drY.IsDBNull(j+this.FieldAssociate.Count))
strValue="0";
else
strValue=(string)drY.GetValue(j+this.FieldAssociate.Count);
strSet+="="+strValue;
}
else if(strsMBZDLXL[j].ToString()=="System.DateTime")
{
if(drY.IsDBNull(j+this.FieldAssociate.Count))
strValue="";
else
strValue=(string)drY.GetValue(j+this.FieldAssociate.Count);
strSet+="='"+strValue+"'";
}
else if (strsMBZDLXL[j].ToString()=="在此處添加新的字段類型")
{
//to do list
}
else
{
MessageBox.Show("ERROR:*****不支持該類型的數據轉換,請與開發商聯系*****");
return;
}
}
else if(relation.TransType=="定值轉換")
{
if(strsMBZDLXL[j].ToString()=="System.String")
{
if(drY.IsDBNull(j+this.FieldAssociate.Count))
strValue="";
else
strValue=(string)relation.ValueMap[drY.GetString(j+this.FieldAssociate.Count)];
strSet+="='"+strValue+"'";
}
else if(strsMBZDLXL[j].ToString()=="System.Decimal")
{
if(drY.IsDBNull(j+this.FieldAssociate.Count))
strValue="0";
else
strValue=(string)relation.ValueMap[drY.GetValue(j+this.FieldAssociate.Count)];
strSet+="="+strValue;
}
else if(strsMBZDLXL[j].ToString()=="System.DateTime")
{
if(drY.IsDBNull(j+this.FieldAssociate.Count))
strValue="";
else
strValue=(string)relation.ValueMap[drY.GetValue(j+this.FieldAssociate.Count)];
strSet+="='"+strValue+"'";
}
else if (strsMBZDLXL[j].ToString()=="在此處添加新的字段類型")
{
//to do list
}
else
{
MessageBox.Show("ERROR:*****不支持該類型的數據轉換,請與開發商聯系*****");
return;
}
}
if(j<strsMBZDL.Count-1)
strSet+=",";
}
strWhere=" where ";
for(int i=0;i<this.FieldAssociate.Count;i++)
{
TFieldAssociate t=(TFieldAssociate)this.FieldAssociate[i];
strWhere+=t.m_strTargetField+"=";
if(t.m_strTargetFieldType=="System.String")
{
if(drY.IsDBNull(i))
{
throw new System.InvalidCastException("原關聯鍵值為空,無法跟新值");
}
else
{
strWhere+="'"+drY.GetString(i)+"'";
}
}
else
{
}
}
strTCmd+=strSet+strWhere;
dcM.CommandText=strTCmd;
dcM.CommandType=System.Data.CommandType.Text;
dcM.ExecuteNonQuery();
}
catch(System.InvalidCastException e)
{
string strLog=e.Message+"---"+strTCmd+"\n";
int hwnd=FindWindow(null,"數據轉換工具");
SendMessage(hwnd,WM_SendLog,System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(strLog),strLog.Length);
}
catch(System.Data.OleDb.OleDbException e)
{
string strLog=e.Message+"---"+strTCmd+"\n";
int hwnd=FindWindow(null,"數據轉換工具");
SendMessage(hwnd,WM_SendLog,System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(strLog),strLog.Length);
}
}
drY.Close();
dcM.Dispose();
MessageBox.Show("數據轉換完成 ");
}
public string GenerateSourceSql(CError ce)
{
System.Collections.ArrayList srcField=this.SourceFieldList;
string strRtn="";
if(srcField.Count<=0||this.FieldAssociate.Count<=0)
{
ce.Msg="源字段為空或關聯鍵為空!";
throw ce;
return strRtn;
}
strRtn="select ";
for(int i=0;i<this.FieldAssociate.Count;i++)
{
TFieldAssociate t=(TFieldAssociate)this.FieldAssociate[i];
strRtn+=t.m_strSourceField+",";
}
strRtn+=srcField[0].ToString();
for(int i=1;i<srcField.Count;i++)
{
strRtn+=","+srcField[i].ToString();
}
strRtn+=" from "+this.SourceTable[0].ToString();
return strRtn;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -