C# 在新函数中使用数据表
C# Use datatable in a new function
我有一个数据table,它是根据电子表格中的数据定义和填充的。然后,在另一个函数中,我想访问该数据 table 以将这些值作为文本转换为属性。如何在 DrawCircuits 函数中使用相同的数据table?
public static System.Data.DataTable ReadExcelToTable(string path)
{
string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
System.Data.DataSet set = new DataSet();
using (OleDbConnection conn = new OleDbConnection(connstring))
{
conn.Open();
System.Data.DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
string firstSheetName = sheetsName.Rows[0][2].ToString();
string sql = string.Format("SELECT * FROM [{0}]", firstSheetName);
OleDbDataAdapter ada = new OleDbDataAdapter(sql, connstring);
ada.Fill(set);
conn.Close();
}
return set.Tables[0];
}
//this command can insert a block and fill out attributes (text)
[CommandMethod("DrawCircuits")]
public void DrawCircuits(string name, double x, double y, double z)
{
Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction myT = db.TransactionManager.StartTransaction())
{
//Get the block definition
string blockName = name;
BlockTable bt =
db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
BlockTableRecord blockDef =
bt[blockName].GetObject(OpenMode.ForRead) as BlockTableRecord;
//Also open paper space - we'll be adding our BlockReference to it
BlockTableRecord ps =
bt[BlockTableRecord.PaperSpace].GetObject(OpenMode.ForWrite)
as BlockTableRecord;
//Create new BlockReference, and link it to our block definition
Point3d point = new Point3d(x, y, z);
using (BlockReference blockRef =
new BlockReference(point, blockDef.ObjectId))
{
//Add the block reference to paper space
ps.AppendEntity(blockRef);
myT.AddNewlyCreatedDBObject(blockRef, true);
//Iterate block definition to find all non-constant
// AttributeDefinitions
foreach (ObjectId id in blockDef)
{
DBObject obj = id.GetObject(OpenMode.ForRead);
AttributeDefinition attDef = obj as AttributeDefinition;
if ((attDef != null) && (!attDef.Constant))
{
//This is a non-constant AttributeDefinition
//Create a new AttributeReference
using (AttributeReference attRef = new AttributeReference())
{
attRef.SetAttributeFromBlock(attDef, blockRef.BlockTransform);
// below is where I want to access the
// datatable loaded into memory in the other
// function. I want the att.Ref.TextString
// value to start from row 0 column 0 of the
// datatable
if (attRef.Tag == "TAG1")
{
attRef.TextString = "";
}
if (attRef.Tag == "XXX-NNNN+")
{
attRef.TextString = "";
}
//Add the AttributeReference to the BlockReference
blockRef.AttributeCollection.AppendAttribute(attRef);
myT.AddNewlyCreatedDBObject(attRef, true);
}
}
}
}
//Our work here is done
myT.Commit();
}
}
***8-18-2016 更新!
在帮助下,我已经能够通过调用 DrawCircuits() 函数的参数,在新函数中使用从 Excel 电子表格加载到内存中的数据 table。我认为这是我要实现的目标的最佳方法。但是,我现在再次尝试在另一个函数中使用此数据table,在该函数中,我将我尝试使用的所有内容绑定在一起,Test3()。一切正常, attRef.TextString = dr.Table.Rows[1][0].ToString(); DrawCircuits() 函数中的语句通过将 datatable 中的值填充为属性的字符串来正常工作。
我现在遇到的问题是,在另一个函数 Test3() 中,我无法让 Test3() 的 for 循环用下一行数据填充下一组属性table.所有属性都使用来自数据 table 的相同行值进行填充。我自己尝试了很多次。如果你们中的任何人对正确的解决方案有任何想法,请告诉我。
[CommandMethod("Test3")]
public void Test3()
{
string Path = SelectSpreadsheet();
System.Data.DataTable table = ReadExcelToTable(Path);
InsertBlocks();
DrawModule("AI-1756-IF16H-SHEET1");
for (int i = 0; i < 8; i++)
{
DrawCircuits("AI-AIT-CIRCUIT", 24, 17 - (i * 1), 0, table.Rows[i]);
}
}
public static System.Data.DataTable ReadExcelToTable(string path)
{
string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
System.Data.DataSet set = new DataSet();
using (OleDbConnection conn = new OleDbConnection(connstring))
{
conn.Open();
System.Data.DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
string firstSheetName = sheetsName.Rows[0][2].ToString();
string sql = string.Format("SELECT * FROM [{0}]", firstSheetName);
OleDbDataAdapter ada = new OleDbDataAdapter(sql, connstring);
ada.Fill(set);
conn.Close();
}
return set.Tables[0];
}
[CommandMethod("DrawCircuits")]
public void DrawCircuits(string name, int x, int y, int z, System.Data.DataRow dr)
{
Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction myT = db.TransactionManager.StartTransaction())
{
//Get the block definition
string blockName = name;
BlockTable bt =
db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
BlockTableRecord blockDef =
bt[blockName].GetObject(OpenMode.ForRead) as BlockTableRecord;
//Also open paper space - we'll be adding our BlockReference to it
BlockTableRecord ps =
bt[BlockTableRecord.PaperSpace].GetObject(OpenMode.ForWrite)
as BlockTableRecord;
//Create new BlockReference, and link it to our block definition
Point3d point = new Point3d(x, y, z);
using (BlockReference blockRef =
new BlockReference(point, blockDef.ObjectId))
{
//Add the block reference to paper space
ps.AppendEntity(blockRef);
myT.AddNewlyCreatedDBObject(blockRef, true);
//Iterate block definition to find all non-constant
// AttributeDefinitions
foreach (ObjectId id in blockDef)
{
DBObject obj = id.GetObject(OpenMode.ForRead);
AttributeDefinition attDef = obj as AttributeDefinition;
if ((attDef != null) && (!attDef.Constant))
{
//This is a non-constant AttributeDefinition
//Create a new AttributeReference
using (AttributeReference attRef = new AttributeReference())
{
attRef.SetAttributeFromBlock(attDef, blockRef.BlockTransform);
if (attRef.Tag == "TAG1")
{
attRef.TextString = dr.Table.Rows[1][0].ToString();
}
if (attRef.Tag == "XXX-NNNN+")
{
attRef.TextString = dr.Table.Rows[1][1].ToString();
}
//Add the AttributeReference to the BlockReference
blockRef.AttributeCollection.AppendAttribute(attRef);
myT.AddNewlyCreatedDBObject(attRef, true);
}
}
}
}
//Our work here is done
myT.Commit();
}
}
public static System.Data.DataTable ReadExcelToTable(string path)
{
//Method codes
...
// Return the data table
return set.Tables[0];
}
public void DrawCircuits(string name, double x, double y, double z,DataTable dt)
{
...
attRef.TextString = dt.Rows[0][0];
...
}
public DataTable ChangeDt(DataTable dt)
{
// Change dt codes
...
// Return changed dt
return dt;
}
public void Use(DataTable dt)
{
var myDt = ChangeDt(dt);
for(i=0; i<=10; i++)
{
DrawCircuits("name", 1, 2, i, myDt);
}
}
或者不带参数的Use
方法:
public void Use()
{
var dt = ReadExcelToTable("....The Path....");
var myDt = ChangeDt(dt);
for(i=0; i<=10; i++)
{
DrawCircuits("name", 1, 2, i, dt);
}
}
我有一个数据table,它是根据电子表格中的数据定义和填充的。然后,在另一个函数中,我想访问该数据 table 以将这些值作为文本转换为属性。如何在 DrawCircuits 函数中使用相同的数据table?
public static System.Data.DataTable ReadExcelToTable(string path)
{
string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
System.Data.DataSet set = new DataSet();
using (OleDbConnection conn = new OleDbConnection(connstring))
{
conn.Open();
System.Data.DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
string firstSheetName = sheetsName.Rows[0][2].ToString();
string sql = string.Format("SELECT * FROM [{0}]", firstSheetName);
OleDbDataAdapter ada = new OleDbDataAdapter(sql, connstring);
ada.Fill(set);
conn.Close();
}
return set.Tables[0];
}
//this command can insert a block and fill out attributes (text)
[CommandMethod("DrawCircuits")]
public void DrawCircuits(string name, double x, double y, double z)
{
Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction myT = db.TransactionManager.StartTransaction())
{
//Get the block definition
string blockName = name;
BlockTable bt =
db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
BlockTableRecord blockDef =
bt[blockName].GetObject(OpenMode.ForRead) as BlockTableRecord;
//Also open paper space - we'll be adding our BlockReference to it
BlockTableRecord ps =
bt[BlockTableRecord.PaperSpace].GetObject(OpenMode.ForWrite)
as BlockTableRecord;
//Create new BlockReference, and link it to our block definition
Point3d point = new Point3d(x, y, z);
using (BlockReference blockRef =
new BlockReference(point, blockDef.ObjectId))
{
//Add the block reference to paper space
ps.AppendEntity(blockRef);
myT.AddNewlyCreatedDBObject(blockRef, true);
//Iterate block definition to find all non-constant
// AttributeDefinitions
foreach (ObjectId id in blockDef)
{
DBObject obj = id.GetObject(OpenMode.ForRead);
AttributeDefinition attDef = obj as AttributeDefinition;
if ((attDef != null) && (!attDef.Constant))
{
//This is a non-constant AttributeDefinition
//Create a new AttributeReference
using (AttributeReference attRef = new AttributeReference())
{
attRef.SetAttributeFromBlock(attDef, blockRef.BlockTransform);
// below is where I want to access the
// datatable loaded into memory in the other
// function. I want the att.Ref.TextString
// value to start from row 0 column 0 of the
// datatable
if (attRef.Tag == "TAG1")
{
attRef.TextString = "";
}
if (attRef.Tag == "XXX-NNNN+")
{
attRef.TextString = "";
}
//Add the AttributeReference to the BlockReference
blockRef.AttributeCollection.AppendAttribute(attRef);
myT.AddNewlyCreatedDBObject(attRef, true);
}
}
}
}
//Our work here is done
myT.Commit();
}
}
***8-18-2016 更新! 在帮助下,我已经能够通过调用 DrawCircuits() 函数的参数,在新函数中使用从 Excel 电子表格加载到内存中的数据 table。我认为这是我要实现的目标的最佳方法。但是,我现在再次尝试在另一个函数中使用此数据table,在该函数中,我将我尝试使用的所有内容绑定在一起,Test3()。一切正常, attRef.TextString = dr.Table.Rows[1][0].ToString(); DrawCircuits() 函数中的语句通过将 datatable 中的值填充为属性的字符串来正常工作。
我现在遇到的问题是,在另一个函数 Test3() 中,我无法让 Test3() 的 for 循环用下一行数据填充下一组属性table.所有属性都使用来自数据 table 的相同行值进行填充。我自己尝试了很多次。如果你们中的任何人对正确的解决方案有任何想法,请告诉我。
[CommandMethod("Test3")]
public void Test3()
{
string Path = SelectSpreadsheet();
System.Data.DataTable table = ReadExcelToTable(Path);
InsertBlocks();
DrawModule("AI-1756-IF16H-SHEET1");
for (int i = 0; i < 8; i++)
{
DrawCircuits("AI-AIT-CIRCUIT", 24, 17 - (i * 1), 0, table.Rows[i]);
}
}
public static System.Data.DataTable ReadExcelToTable(string path)
{
string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
System.Data.DataSet set = new DataSet();
using (OleDbConnection conn = new OleDbConnection(connstring))
{
conn.Open();
System.Data.DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
string firstSheetName = sheetsName.Rows[0][2].ToString();
string sql = string.Format("SELECT * FROM [{0}]", firstSheetName);
OleDbDataAdapter ada = new OleDbDataAdapter(sql, connstring);
ada.Fill(set);
conn.Close();
}
return set.Tables[0];
}
[CommandMethod("DrawCircuits")]
public void DrawCircuits(string name, int x, int y, int z, System.Data.DataRow dr)
{
Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction myT = db.TransactionManager.StartTransaction())
{
//Get the block definition
string blockName = name;
BlockTable bt =
db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
BlockTableRecord blockDef =
bt[blockName].GetObject(OpenMode.ForRead) as BlockTableRecord;
//Also open paper space - we'll be adding our BlockReference to it
BlockTableRecord ps =
bt[BlockTableRecord.PaperSpace].GetObject(OpenMode.ForWrite)
as BlockTableRecord;
//Create new BlockReference, and link it to our block definition
Point3d point = new Point3d(x, y, z);
using (BlockReference blockRef =
new BlockReference(point, blockDef.ObjectId))
{
//Add the block reference to paper space
ps.AppendEntity(blockRef);
myT.AddNewlyCreatedDBObject(blockRef, true);
//Iterate block definition to find all non-constant
// AttributeDefinitions
foreach (ObjectId id in blockDef)
{
DBObject obj = id.GetObject(OpenMode.ForRead);
AttributeDefinition attDef = obj as AttributeDefinition;
if ((attDef != null) && (!attDef.Constant))
{
//This is a non-constant AttributeDefinition
//Create a new AttributeReference
using (AttributeReference attRef = new AttributeReference())
{
attRef.SetAttributeFromBlock(attDef, blockRef.BlockTransform);
if (attRef.Tag == "TAG1")
{
attRef.TextString = dr.Table.Rows[1][0].ToString();
}
if (attRef.Tag == "XXX-NNNN+")
{
attRef.TextString = dr.Table.Rows[1][1].ToString();
}
//Add the AttributeReference to the BlockReference
blockRef.AttributeCollection.AppendAttribute(attRef);
myT.AddNewlyCreatedDBObject(attRef, true);
}
}
}
}
//Our work here is done
myT.Commit();
}
}
public static System.Data.DataTable ReadExcelToTable(string path)
{
//Method codes
...
// Return the data table
return set.Tables[0];
}
public void DrawCircuits(string name, double x, double y, double z,DataTable dt)
{
...
attRef.TextString = dt.Rows[0][0];
...
}
public DataTable ChangeDt(DataTable dt)
{
// Change dt codes
...
// Return changed dt
return dt;
}
public void Use(DataTable dt)
{
var myDt = ChangeDt(dt);
for(i=0; i<=10; i++)
{
DrawCircuits("name", 1, 2, i, myDt);
}
}
或者不带参数的Use
方法:
public void Use()
{
var dt = ReadExcelToTable("....The Path....");
var myDt = ChangeDt(dt);
for(i=0; i<=10; i++)
{
DrawCircuits("name", 1, 2, i, dt);
}
}