📄 mydataset.cs
字号:
using System;
using System.IO;
using mathfunsharp;
using expert;
using NameSpacePetroGA;
using System.Data.Common;
using System.Data.OleDb;
namespace OWNERDATASET
{
/// <summary>
/// myDataSet 的摘要说明。
/// </summary>
///
using System;
using System.Data;
using System.Data.OleDb;
using System.Runtime.Serialization;
using System.Collections;
using System.Windows.Forms;
[Serializable()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Diagnostics.DebuggerStepThrough()]
[System.ComponentModel.ToolboxItem(true)]
public class myDataSet : DataSet
{
public static string NAME_ALL_DIRECTPRINT_TABLENAME="直接打印的表";
public static DataTable FieldSetting=null;//用户对所有录井参数的设定,min,max,useit,weight
Hashtable adList;
public System.Data.OleDb.OleDbConnection cnnexpert;
protected
static OleDbType GetOleDbType(Type t)
{
if(t==typeof( Int64 ))
return OleDbType. BigInt ;
if(t==typeof( Byte ))
return OleDbType. Binary ;
if(t==typeof( bool ))
return OleDbType. Boolean ;
if(t==typeof( String ))
return OleDbType. BSTR ;
if(t==typeof( String ))
return OleDbType. Char ;
if(t==typeof( decimal ))
return OleDbType. Currency ;
if(t==typeof( DateTime ))
return OleDbType. Date ;
if(t==typeof( DateTime ))
return OleDbType. DBDate ;
if(t==typeof( TimeSpan ))
return OleDbType. DBTime ;
if(t==typeof( DateTime ))
return OleDbType. DBTimeStamp ;
if(t==typeof( decimal ))
return OleDbType. Decimal ;
if(t==typeof( double ))
return OleDbType. Double ;
if(t==null )
return OleDbType. Empty ;
if(t==typeof( Exception ))
return OleDbType. Error ;
if(t==typeof( DateTime ))
return OleDbType. Filetime ;
if(t==typeof( Guid ))
return OleDbType. Guid ;
if(t==typeof( Object ))
return OleDbType. IDispatch ;
if(t==typeof( Int32 ))
return OleDbType. Integer ;
if(t==typeof( Object ))
return OleDbType. IUnknown ;
if(t==typeof( byte[] ))
return OleDbType. LongVarBinary ;
if(t==typeof( String ))
return OleDbType. LongVarChar ;
if(t==typeof( String ))
return OleDbType. LongVarWChar ;
if(t==typeof( decimal ))
return OleDbType. Numeric ;
if(t==typeof( Object ))
return OleDbType. PropVariant ;
if(t==typeof( Single ))
return OleDbType. Single ;
if(t==typeof( Int16 ))
return OleDbType. SmallInt ;
if(t==typeof( SByte ))
return OleDbType. TinyInt ;
if(t==typeof( UInt64 ))
return OleDbType. UnsignedBigInt ;
if(t==typeof( UInt32 ))
return OleDbType. UnsignedInt ;
if(t==typeof( UInt16 ))
return OleDbType. UnsignedSmallInt ;
if(t==typeof( byte ))
return OleDbType. UnsignedTinyInt ;
if(t==typeof( byte ))
return OleDbType. VarBinary ;
if(t==typeof( String ))
return OleDbType. VarChar ;
if(t==typeof( Object ))
return OleDbType. Variant ;
if(t==typeof( decimal ))
return OleDbType. VarNumeric ;
if(t==typeof( String ))
return OleDbType. VarWChar ;
if(t==typeof( String ))
return OleDbType. WChar ;
return OleDbType.Empty;
}
private void InitAdpter(ref OleDbDataAdapter adapter,ref OleDbConnection cnn,string tablename)
{
// oleDbDataAdapter1.Update(dataSet1);
OleDbCommand selectCmd=new OleDbCommand();
OleDbCommand insertCmd=new OleDbCommand();
OleDbCommand updateCmd=new OleDbCommand();
OleDbCommand deleteCmd=new OleDbCommand();
selectCmd.Connection=cnn;
insertCmd.Connection=cnn;
updateCmd.Connection=cnn;
deleteCmd.Connection=cnn;
selectCmd.CommandText="select * from "+tablename;
adapter.SelectCommand=selectCmd;
DataTableMapping tblMapping=new DataTableMapping(tablename,tablename);
OleDbCommand tempCmd=new OleDbCommand("select * from "+tablename,cnn);
OleDbDataReader reader=tempCmd.ExecuteReader();
DataTable schemaTable=reader.GetSchemaTable();
string sSet=" SET ";
string sValue="";
string sInsert="";
bool first=true;
foreach(DataRow r in schemaTable.Rows)
{
string rN=r["ColumnName"] as string;
tblMapping.ColumnMappings.Add(new DataColumnMapping(rN,rN));
if(first)
{
sSet +="[" + rN+ "]= ? ";
sInsert+="[" + rN+ "]";
sValue+="?";
first=false;
}
else
{
sSet +=",[" + rN+ "]= ? ";
sInsert+=",[" + rN+ "]";
sValue+=",? ";
}
updateCmd.Parameters.Add(new OleDbParameter(rN,
GetOleDbType(r["DataType"] as Type),
(int)r["ColumnSize"],
ParameterDirection.Input,
true,
(System.Byte)0,//;r["NumericPrecision"],
(System.Byte)0,//;r["NumericScale"],
rN,
System.Data.DataRowVersion.Original,
null));
insertCmd.Parameters.Add(new OleDbParameter(rN,
GetOleDbType(r["DataType"] as Type),
(int)r["ColumnSize"],
ParameterDirection.Input,
true,
(System.Byte)0,//;r["NumericPrecision"],
(System.Byte)0,//;r["NumericScale"],
rN,
System.Data.DataRowVersion.Original,
null));
//Parameters.Add(new OleDbParameter("C1_1", System.Data.OleDb.OleDbType.VarWChar, 16, "C1 1"));
}
reader.Close();
adapter.TableMappings.Add(tblMapping);
deleteCmd.CommandText = "DELETE FROM "+ tablename+" WHERE (序号 = ?)";
deleteCmd.Parameters.Add(
new OleDbParameter( "Original_序号",
OleDbType.Integer,
0,
System.Data.ParameterDirection.Input,
false,
((System.Byte)(10)),
((System.Byte)(0)),
"序号",
System.Data.DataRowVersion.Original,
null
)
);
adapter.DeleteCommand=deleteCmd;
updateCmd.CommandText="UPDATE "+tablename + sSet;
updateCmd.CommandText+=" WHERE (序号 = ?)";
updateCmd.Parameters.Add(
new OleDbParameter( "Original_序号",OleDbType.Integer,
0,
System.Data.ParameterDirection.Input,
false,
((System.Byte)(10)),
((System.Byte)(0)),
"序号",
System.Data.DataRowVersion.Original,
null
)
);
adapter.UpdateCommand=updateCmd;
insertCmd.CommandText = @"INSERT INTO "+ tablename+ "(" + sInsert+ ") " + " VALUES ( "+ sValue+ ")";
adapter.InsertCommand=insertCmd;
int a=0;
a++;
}
private void InitTables()
{
if(cnnexpert.State==System.Data.ConnectionState.Closed)
{
cnnexpert.Open();
}
DataTable schemaTable = cnnexpert.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
new object[] {null, null, null, "TABLE"});
string s;
foreach(DataRow myRow in schemaTable.Rows)
{
OleDbDataAdapter aAdapter= new OleDbDataAdapter();
InitAdpter(ref aAdapter,ref cnnexpert,(string)myRow["Table_name"]);
OleDbCommand aCommand=new OleDbCommand();
s=(string)myRow["Table_name"];
aAdapter.Fill(this,s);
adList.Add(s,aAdapter);
aAdapter.Update(this,s);
}
// CreateRelation();
}
private void InitViews()
{
if(cnnexpert.State==System.Data.ConnectionState.Closed)
{
cnnexpert.Open();
}
DataTable schemaTable = cnnexpert.GetOleDbSchemaTable(OleDbSchemaGuid.Views,
new object[] {null, null,null/* "全部数据forGA"*/});
string s;
foreach(DataRow myRow in schemaTable.Rows)
{
OleDbDataAdapter aAdapter= new OleDbDataAdapter();
InitAdpter(ref aAdapter,ref cnnexpert,(string)myRow["Table_name"]);
OleDbCommand aCommand=new OleDbCommand();
s=(string)myRow["Table_name"];
aAdapter.Fill(this,s);
adList.Add(s,aAdapter);
aAdapter.Update(this,s);
}
// CreateRelation();
}
static public string GetSourceFile()
{
//return "Y:\\南阳专家系统\\专家系统\\原始数据表.mdb";
OpenFileDialog openFileDialog1 = new OpenFileDialog();
//openFileDialog1.InitialDirectory = "c:\\" ;
openFileDialog1.Filter = "access文件(*.mdb)|*.mdb|All files (*.*)|*.*" ;
openFileDialog1.FilterIndex = 2 ;
openFileDialog1.RestoreDirectory = true ;
if(openFileDialog1.ShowDialog() == DialogResult.OK)
return openFileDialog1.FileName;
else
return null;
//return "Y:\\南阳专家系统\\专家系统\\原始数据表.mdb";
}
public myDataSet()
{
//
adList=new Hashtable();
cnnexpert = new System.Data.OleDb.OleDbConnection();
//
}
public void Init(string filename)
{
string s="";
s+=@";Provider=Microsoft.Jet.OLEDB.4.0";
s+=@";Prompt=Complete";
s+=@";Password=""""";
s+=@";User ID=Admin";
// s+=@";Mode=ReadWrite|Share Deny None";
s+=@";Extended Properties=""""";
s+=@";Jet OLEDB:System database=""""";
s+=@";Jet OLEDB:Registry Path=""""";
s+=@";Jet OLEDB:Database Password=""""";
s+=@";Jet OLEDB:Engine Type=5";
s+=@";Jet OLEDB:Database Locking Mode=1";
s+=@";Jet OLEDB:Global Partial Bulk Ops=2";
s+=@";Jet OLEDB:Global Bulk Transactions=1";
s+=@";Jet OLEDB:New Database Password=""""";
s+=@";Jet OLEDB:Create System Database=False";
s+=@";Jet OLEDB:Encrypt Database=False";
s+=@";Jet OLEDB:Don't Copy Locale on Compact=False";
s+=@";Jet OLEDB:Compact Without Replica Repair=False";
s+=@";Jet OLEDB:SFP=False";
if(filename.Length<=0)
{
s+=";Data Source=";
string temps=GetSourceFile();
if(temps==null)
{
MessageBox.Show("You must desinate a file.", "fileName Entry Error",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
s+=temps;
}
else
{
s+=";Data Source=";
s+=filename;
}
cnnexpert.ConnectionString=s;
try
{
cnnexpert.Open();
this.DataSetName =cnnexpert.DataSource;
if(adList!=null)
adList.Clear();
InitTables();
InitViews();
//TruncateAllTables2views();
}
catch(InvalidOperationException e)
{
MessageBox.Show(e.Message, "打开文件错误",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
catch(OleDbException e)
{
MessageBox.Show(e.Message, "打开文件错误",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
catch(Exception e)
{
MessageBox.Show("错误号:" +e.Source.ToString()+e.Message,"突发错误!请详细记录本错误信息及目前的状态及操作,并报告我们!");
}
//InitRelations();
}
public void TruncateAllTables2views()
{
foreach(DataTable table in this.Tables)
{
foreach(DataColumn col in table.Columns)
{
if(!myCriDataSet.IsCriField(col.ColumnName))
table.Columns.Remove(col);
}
};
}
~myDataSet()
{
//
adList.Clear();
//
}
/// <summary>
/// 更新数据到数据库
/// </summary>
public void Update()
{
IDictionaryEnumerator myEnumerator = adList.GetEnumerator();
try
{
while ( myEnumerator.MoveNext() )
{
OleDbDataAdapter ad=((OleDbDataAdapter)(myEnumerator.Value));
OleDbCommandBuilder custCB=new OleDbCommandBuilder(ad);
ad.Update(this , (string)(myEnumerator.Key));
}
}
catch(OleDbException e)
{
MessageBox.Show("错误号:" + Convert.ToString(e.ErrorCode)+e.Message,"数据库操作错误");
}
catch(Exception e)
{
MessageBox.Show("错误号:" +e.Source.ToString()+e.Message,"突发错误!请详细记录本错误信息及目前的状态及操作,并报告我们!");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -