将列添加到memmory表中 [英] add column to in memmory table
本文介绍了将列添加到memmory表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好我想知道是否有可能在内存数据表中添加一个标识列,以便更容易地编辑memroy数据我导入电子表格,并且我想在数据中添加一个标识列,其中增加了1到数据所以每行旁边都会有一个id号码,这是我的代码,百分之百的导入我只是不知道如何添加id列
Hi everyone i want to know if it is possible to add a identity column to an in memory datatable to make editing of the in memroy data easier I import an spreadsheet and i want to add a identity column to the data which increments with 1 to the data so there will be an id number next to each row here is my code it works hundred percent to import i just dont know how to add an id column
void ExportToGrid(String path)
{
OleDbConnection MyConnection = null;
DataSet DtSet = null;
OleDbDataAdapter MyCommand = null;
String NewString = filename.Remove(filename.Length - 2, 2);
string last = NewString[NewString.Length - 1].ToString();
switch (last)
{
case "x":
//Connection for MS Excel 2003 .xls format
MyConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; Data Source='" + path + "';Extended Properties='Excel 8.0;HDR=No;'");
break;
case "l":
//Connection for .xslx 2007 format
MyConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + path + "';Extended Properties='Excel 12.0;HDR=No;'");
break;
}
//Select your Excel file
MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
//Worksheet sheet = new Worksheet("Sheet1");
//int LastRow = sheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing).Row;
DtSet = new System.Data.DataSet();
//Bind all excel data in to data set
MyCommand.Fill(DtSet, "[Sheet1$]");
System.Data.DataTable dt = DtSet.Tables[0];
ViewState["SelectedRecords"] = dt;
MyConnection.Close();
//Check datatable have records
if (dt.Rows.Count > 0)
{
GridView2.DataSource = dt;
GridView2.DataBind();
}
//Delete temporary Excel file from the Server path
if (System.IO.File.Exists(path))
{
System.IO.File.Delete(path);
}
string expression1 = "F4 = ''";
String valuesarr = String.Empty;
for (int i = 0; i < dt.Rows.Count - 1; i++)
{
List<object> lst = dt.Rows[i].ItemArray.ToList();
foreach (Object s in lst)
{
valuesarr += s.ToString();
}
if (String.IsNullOrEmpty(valuesarr))
dt.Rows.RemoveAt(i);
}
DataRow[] foundRows = dt.Select(expression1);
int CellCount = foundRows.Length;
string expression2 = "F2 IS NULL";
DataRow[] foundRows2 = dt.Select(expression2);
int nameCount = foundRows2.Length;
string expression3 = "F3 IS NULL";
DataRow[] foundRows3 = dt.Select(expression3);
int surnameCount = foundRows3.Length;
tableImport.Visible = true;
}
推荐答案
,MyConnection);
// 工作表表=新工作表(Sheet1);
// int LastRow = sheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell,Type.Missing).Row;
DtSet = new System.Data.DataSet();
// 绑定所有Excel数据in to data set
MyCommand.Fill(DtSet, [Sheet1
", MyConnection); //Worksheet sheet = new Worksheet("Sheet1"); //int LastRow = sheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing).Row; DtSet = new System.Data.DataSet(); //Bind all excel data in to data set MyCommand.Fill(DtSet, "[Sheet1
);
System.Data.DataTable dt = DtSet.Tables [ 0 ];
ViewState [ SelectedRecords] = dt;
MyConnection。关();
// 检查数据表是否有记录
if (dt.Rows.Count > 0 )
{
GridView2.DataSource = dt;
GridView2.DataBind();
}
// 从服务器路径中删除临时Excel文件
if (System.IO.File.Exists(path))
{
System.IO.File.Delete(path);
}
string expression1 = F4 ='';
String valuesarr = String .Empty;
for ( int i = 0 ; i < dt.Rows.Count - 1 ; i ++)
{
List< object> lst = dt.Rows [i] .ItemArray.ToList();
foreach (对象 s lst)
{
valuesarr + = s.ToString();
}
if ( String .IsNullOrEmpty(valuesarr))
dt.Rows.RemoveAt(i);
}
DataRow [] foundRows = dt.Select(expression1);
int CellCount = foundRows.Length;
string expression2 = F2 IS NULL;
DataRow [] foundRows2 = dt.Select(expression2);
int nameCount = foundRows2.Length;
string expression3 = F3 IS NULL;
DataRow [] foundRows3 = dt.Select(expression3);
int surnameCount = foundRows3.Length;
tableImport.Visible = true ;
}
"); System.Data.DataTable dt = DtSet.Tables[0]; ViewState["SelectedRecords"] = dt; MyConnection.Close(); //Check datatable have records if (dt.Rows.Count > 0) { GridView2.DataSource = dt; GridView2.DataBind(); } //Delete temporary Excel file from the Server path if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } string expression1 = "F4 = ''"; String valuesarr = String.Empty; for (int i = 0; i < dt.Rows.Count - 1; i++) { List<object> lst = dt.Rows[i].ItemArray.ToList(); foreach (Object s in lst) { valuesarr += s.ToString(); } if (String.IsNullOrEmpty(valuesarr)) dt.Rows.RemoveAt(i); } DataRow[] foundRows = dt.Select(expression1); int CellCount = foundRows.Length; string expression2 = "F2 IS NULL"; DataRow[] foundRows2 = dt.Select(expression2); int nameCount = foundRows2.Length; string expression3 = "F3 IS NULL"; DataRow[] foundRows3 = dt.Select(expression3); int surnameCount = foundRows3.Length; tableImport.Visible = true; }
Hi Divan,
您可以在现有数据表中创建标识列按照以下步骤:
步骤1)创建一个标识栏
Hi Divan,
you can create Identity column in Existing Datatable in following Steps :
Step 1) Create an Identity Column
DataColumn IdentityCol = new DataColumn("ID");
IdentityCol.AutoIncrement = true;
IdentityCol.AutoIncrementSeed = 1;
IdentityCol.AutoIncrementStep = 1;
步骤2)将标识列添加到现有数据表
Step 2) Add Identity Column to Exisiting Datatable
//Assuming dt as Datatable
dt.Columns.Add(IdentityCol);
步骤3)更新Exisitng DataColumn值
Step 3) Update Exisitng DataColumn Value
for(i=1; i<=dt.Rows.Count; i++)
{
dt.Rows[i-1]["ID"] = i;
}
<pre>
Now Identity Column will automatically Incremented, whenever new row gets added.
Thanks!
Yogendra Dubey
这篇关于将列添加到memmory表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文