visual studio - c#在新函数中使用datatable
本文关键字:函数 datatable studio 新函数 visual | 更新日期: 2023-09-27 18:02:14
我有一个数据表,它是根据电子表格中的数据定义和填充的。然后,在另一个函数中,我希望访问该数据表,将这些值作为文本转换为属性。我如何在DrawCircuits函数中使用相同的datatable ?
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电子表格加载到内存中的数据表。我认为这是我想要达到的最好的方法。然而,我现在再次尝试在另一个函数中使用这个数据表,在这个函数中,我将试图使用的所有内容捆绑在一起,即Test3()。一切都很好,一切都很好。TextString = dr.Table.Rows[1][0].ToString();DrawCircuits()函数中的语句通过将数据表中的值作为属性的字符串填充来正常工作。
我遇到的问题是,现在,在另一个函数Test3()中,我无法获得Test3()的for循环来用数据表的下一行填充下一组属性。使用来自数据表的相同行值填充所有属性。我自己也试过很多次。如果你们有什么正确的解决办法,请告诉我。
[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);
}
}