当前位置: 首页>>代码示例>>C#>>正文


C# DataTable.AddRow方法代码示例

本文整理汇总了C#中DataTable.AddRow方法的典型用法代码示例。如果您正苦于以下问题:C# DataTable.AddRow方法的具体用法?C# DataTable.AddRow怎么用?C# DataTable.AddRow使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DataTable的用法示例。


在下文中一共展示了DataTable.AddRow方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ReadXLS

    public DataTable ReadXLS(string sheetName = "" )
    {
        Debug.Log("Begin to read " + sheetName + " form " + fileName);

        TextAsset xmlData = new TextAsset();
        xmlData = (TextAsset)Resources.Load(fileName, typeof(TextAsset));

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xmlData.text);

        DataTable res = new DataTable();

        //Get sheet by sheetName
        XmlNodeList sheets = xmlDoc.GetElementsByTagName("Worksheet");
        XmlNode sheet = null;

        foreach (XmlNode s in sheets)
        {
            if (s.Attributes["ss:Name"].Value == sheetName)
            {
                sheet = s;
            }
        }

        if (sheet == null )
        {
            Debug.Log("Cannot find sheet " + sheetName);
            return res;
        }

        //Analyze the sheet
        // xmlDoc.LoadXml(sheet.InnerXml);

        XmlNode sheetContent = sheet.ChildNodes[0];

        for(int i = sheetContent.ChildNodes.Count - 1 ; i >= 0 ; --i)
        {
            // Debug.Log(sheetContent.ChildNodes[i].LocalName);
            if ( sheetContent.ChildNodes[i].LocalName != "Row" )
                sheetContent.RemoveChild(sheetContent.ChildNodes[i]);
        }

        // XmlNodeList rows = xmlDoc.GetElementsByTagName("Row");

        XmlNodeList rows = sheetContent.ChildNodes;

        var firstLineDic = new Dictionary<string,string>();

        // xmlDoc.LoadXml(rows[0].InnerXml);
        XmlNodeList firstLine = rows[0].ChildNodes;

        for(int i = 0 ; i < firstLine.Count ; ++ i )
        {
            firstLineDic.Add((i+1).ToString(), firstLine[i].InnerText);
            res.AddFirst(firstLine[i].InnerText);
            // Debug.Log("First Line " +  firstLine[i].InnerText);
        }

        for(int i = 1 ; i < rows.Count ; ++i )
        // for( int i = 1 ; i < 3 ; ++ i )
        {
            // xmlDoc.LoadXml(rows[i].InnerXml);
            // XmlNodeList cells = xmlDoc.GetElementsByTagName("Cell",rows[i].NamespaceURI);

            XmlNodeList cells = rows[i].ChildNodes;

            DataRow r = new DataRow();

            int indexNow = 1;

            for(int j = 0 ; j < cells.Count ; ++ j )
            // for( int j = 0  ; j < 3 ; ++ j)
            {

                if (cells[j].Attributes["ss:Index"] != null)
                {
                    indexNow = int.Parse( cells[j].Attributes["ss:Index"].Value);
                }

                if (cells[j].InnerText != "" )
                    r.Add(firstLineDic[indexNow.ToString()],cells[j].InnerText);
                indexNow ++;
            }

            string key = "";
            if (!r.isBlank(firstLineDic["1"]))
                key = r.Select(firstLineDic["1"]);

            res.AddRow( key, r);

        }

        Debug.Log("rows " + rows.Count.ToString());

        return res;
    }
开发者ID:fdu5526,项目名称:balloon,代码行数:96,代码来源:ExcelHelper.cs

示例2: ExecuteQuery

		/// <summary>
		/// Executes a query that requires a response (SELECT, etc).
		/// </summary>
		/// <returns>
		/// Dictionary with the response data
		/// </returns>
		/// <param name='query'>
		/// Query.
		/// </param>
		/// <exception cref='SqliteException'>
		/// Is thrown when the sqlite exception.
		/// </exception>
		public DataTable ExecuteQuery (string query)
		{
			if (!CanExQuery) {
				Debug.Log ("ERROR: Can't execute the query, verify DB origin file");
				return null;
			}
			
			this.Open ();
			if (!IsConnectionOpen) {
				throw new SqliteException ("SQLite database is not open.");
			}
			
			IntPtr stmHandle = Prepare (query);
			
			int columnCount = sqlite3_column_count (stmHandle);
			
			var dataTable = new DataTable ();
			for (int i = 0; i < columnCount; i++) {
				string columnName = Marshal.PtrToStringAnsi (sqlite3_column_name (stmHandle, i));
				dataTable.Columns.Add (columnName);
			}
			
			
			//populate datatable
			while (sqlite3_step(stmHandle) == SQLITE_ROW) {
				object[] row = new object[columnCount];
				for (int i = 0; i < columnCount; i++) {
					switch (sqlite3_column_type (stmHandle, i)) {
					case SQLITE_INTEGER:
						row [i] = sqlite3_column_int (stmHandle, i);
						break;
						
					case SQLITE_TEXT:
						IntPtr text = sqlite3_column_text (stmHandle, i);
						row [i] = Marshal.PtrToStringAnsi (text);
						break;
						
					case SQLITE_FLOAT:
						row [i] = sqlite3_column_double (stmHandle, i);
						break;
						
					case SQLITE_BLOB:
						IntPtr blob = sqlite3_column_blob (stmHandle, i);
						int size = sqlite3_column_bytes (stmHandle, i);
						byte[] data = new byte[size];
						Marshal.Copy (blob, data, 0, size);
						row [i] = data;
						break;
						
					case SQLITE_NULL:
						row [i] = null;
						break;
					}
				}
				
				dataTable.AddRow (row);
			}
			
			Finalize (stmHandle);
			this.Close ();
			return dataTable;
		}
开发者ID:SonGit,项目名称:NailGame,代码行数:74,代码来源:SqliteDatabase.cs

示例3: ExecuteQuery

        public DataTable ExecuteQuery(string query)
        {
            if (!IsConnectionOpen)
            {
                throw new SqliteException(SQLITE_ERROR_NOT_OPENED,"SQLite database is not open.");
            }

            IntPtr stmHandle = Prepare(query);

            int columnCount = sqlite3_column_count(stmHandle);

            var dataTable = new DataTable();
            for (int i = 0; i < columnCount; i++)
            {
                string columnName = Marshal.PtrToStringAnsi(sqlite3_column_name(stmHandle, i));
                dataTable.Columns.Add(columnName);
            }

            //populate datatable
            while (sqlite3_step(stmHandle) == SQLITE_ROW)
            {
                object[] row = new object[columnCount];
                for (int i = 0; i < columnCount; i++)
                {
                    switch (sqlite3_column_type(stmHandle, i))
                    {
                        case SQLITE_INTEGER:
                            row[i] = sqlite3_column_int64(stmHandle, i);
                            break;

                        case SQLITE_TEXT:
                            IntPtr text = sqlite3_column_text(stmHandle, i);
                            row[i] = Marshal.PtrToStringAnsi(text);
                            break;

                        case SQLITE_FLOAT:
                            row[i] = sqlite3_column_double(stmHandle, i);
                            break;

                        case SQLITE_NULL:
                            row[i] = null;
                            break;
                    }
                }

                dataTable.AddRow(row);
            }

            Finalize(stmHandle);

            return dataTable;
        }
开发者ID:s0s0s0,项目名称:unity-orm,代码行数:52,代码来源:SqliteDatabase.cs


注:本文中的DataTable.AddRow方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。