我有一个像这样的数据表:
| Col1 | Col6 | Col3 | Col43 | Col0 |
---------------------------------------------------
RowA | 1 | 6 | 54 | 4 | 123 |
Run Code Online (Sandbox Code Playgroud)
如您所见,Cols未按其编号排序.这就是我希望它在"魔术"之后的样子:
| Col0 | Col1 | Col3 | Col6 | Col43 |
---------------------------------------------------
RowA | 123 | 1 | 54 | 6 | 4 |
Run Code Online (Sandbox Code Playgroud)
在C#中是否有内置的功能?如果没有,我怎么能开始这个?
说我有3个Dictionary<string, string>物体.所有3都有相同的键,如下所示:
Dic1 Dic2 Dic3 K V K V K V A s A z A i B d B e B u C a C r C o D w D t D p
现在,我想将这些词典合并到一个DataTable中,DataTable应该如下所示:
A s z i B d e u C a r o D w t p
关于如何从单独的词典到DataTable中的组合词典的任何指示或想法?
有没有比以下更好的方法?
特别是,我想Activator用其他东西替换.
public static List<T> ToList<T>(DataTable dt)
{
Type type = typeof(T);
List<T> list = new List<T>();
foreach (DataRow dr in dt.Rows)
{
object[] args = new object[1];
args[0] = dr;
list.Add((T)Activator.CreateInstance(type, args));
}
return list;
}
Run Code Online (Sandbox Code Playgroud) 我试过用这个:
var rowCount = $('#locationsTable tbody tr').length - 1;
Run Code Online (Sandbox Code Playgroud)
但是一个问题是你无法判断表中是否有1条记录或0条记录,因为当有0条记录时,jQuery datatable插件会显示一条额外的tr行,表示"没有匹配的记录".
有没有更好的方法来获得实际有效的行/记录数?
我一直在尝试使用核心JSF实现表行扩展/折叠的功能,并且我还必须保留排序.在核心JSF中有没有办法实现这个功能?
我有一个asp.net网站,它将生成一些包含7-8张数据的excel文件.到目前为止,最好的解决方案似乎是NPOI,这可以创建excel文件而无需在服务器上安装excel,并且有一个很好的API simillar到excel互操作.
但是,我无法找到一种方法来转储excel中的整个数据表类似于 CopyFromRecordset
关于如何做到这一点的任何提示,或者比NPOI更好的解决方案?
所以我在Oracle数据库中有一个表,我基本上试图在C#中复制它的主键.我可以很好地加载表,但是当我尝试添加与oracle表相同的主键约束时,我收到此错误:
"These columns don't currently have unique values".
Run Code Online (Sandbox Code Playgroud)
这是代码
DataTable dt = new DataTable(combine(owner, name));
string q = "select " + getColumnSelect(owner, name) +
" from " + combine(owner, name) + " " +
(where_clause ?? "") + " order by " + getOrderByKeys(owner, name);
// load the data table
OracleDataAdapter oda = new OracleDataAdapter(q, conn);
oda.FillLoadOption = LoadOption.PreserveChanges;
dt.BeginLoadData();
oda.Fill(dt);
dt.EndLoadData();
// now set primary keys
List<DataColumn> cols = new List<DataColumn>();
foreach (string key in getKeys(owner, name))
{
cols.Add(dt.Columns[key]); …Run Code Online (Sandbox Code Playgroud) 鉴于这个例子:
// Create an arary of car objects.
car[] arrayOfCars= new car[]
{
new car("Ford",1992),
new car("Fiat",1988),
new car("Buick",1932),
new car("Ford",1932),
new car("Dodge",1999),
new car("Honda",1977)
};
Run Code Online (Sandbox Code Playgroud)
我试过这样的事情:
for (int i = 0; i < dtable.Rows.Count; i++)
{
DataRow drow = dtable.Rows[i];
arrayOfCars[] = new car(drow["make"].ToString(), drow["year"].ToString());
}
Run Code Online (Sandbox Code Playgroud)
在循环数据表时如何向阵列添加其他数据?
UPDATE1:
我选择了@Reed提出的解决方案.
// Create the array, specifying the total length
car[] arrayOfCars = new car[dtable.Rows.Count];
for (int i = 0; i < dtable.Rows.Count; i++)
{
DataRow drow = dtable.Rows[i];
// …Run Code Online (Sandbox Code Playgroud) 我需要获取列名,主键,外键和其他架构信息.这DataTable堂课似乎包含了所有这些.
下面是我到目前为止的当前代码.有了它,我可以检索除外键之外的所有信息.我期待它们被定义,DataTable.Constraints但它们不是.这是我目前的代码:
private static DataTable LoadSchemaInfo(string tableName, SqlConnection connection)
{
string cmdText = "SELECT * FROM [" + tableName + "] WHERE 1 = 0";
// Create a SqlDataAdapter to get the results as DataTable
var sqlDataAdapter = new SqlDataAdapter(cmdText, connection);
// Create a new DataTable
var dataTable = new DataTable(tableName);
// Fill the DataTable with the result of the SQL statement
sqlDataAdapter.FillSchema(dataTable, SchemaType.Source);
return dataTable;
}
Run Code Online (Sandbox Code Playgroud)
知道如何检索所有信息或如何获取FK(最好不使用纯SQL语法,因为我会缺少一些编译时检查)?