English 中文(简体)
VB.Net 指定数据表格的值
原标题:VB.Net assign values to datatable
  • 时间:2012-05-22 05:44:51
  •  标签:
  • vb.net

Hi 我有指定某些值的字符串和整数。 现在我需要读读字符串和整数, 并将其分配给我用 VB. Net 创建的数据表格 。

If dt.Rows.Count > 0 Then 
For n As Integer = 0 To dt.Rows.Count - 1 
EmployeeNo = (dt.Rows(n)(0)) 
EmpName = (dt.Rows(n)(1)).ToString 
Commission = CDbl(dt.Rows(n)(2)) 
 I need to read one by one and assign the [EmployeeNo,EmpName,Commission] 
 to the dataset DtEmployee in which Employee is the table and EmpNo, EmpName, Commission  are the coloumns.. 
Next 
End If 

请你帮我...

问题回答

尝试以此将整个工作表倾弃到数据表 :

var connectionString = " Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:file.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1";"
        var sheetName = "Sheet1";

        using (var con = new OleDbConnection(connectionString))
        {
            con.Open();

            var table = new DataTable(sheetName);
            var query = "SELECT * FROM [" + sheetName + "]";
            OleDbDataAdapter adapter = new OleDbDataAdapter(query, con);
            adapter.Fill(table);
            return table;
        }

编辑: 根据更新后的问题

If dt.Rows.Count > 0 Then 
For n As Integer = 0 To dt.Rows.Count - 1 
EmployeeNo = (dt.Rows(n)(0)) 
EmpName = (dt.Rows(n)(1)).ToString 
Commission = CDbl(dt.Rows(n)(2)) 
 I need to read one by one and assign the [EmployeeNo,EmpName,Commission] 
 to the dataset DtEmployee in which Employee is the table and EmpNo, EmpName, Commission  are the coloumns.. 

Dim dataRow As DataRow = DtEmployee.Tables("Employee").AsEnumerable().Where(Function(row) row.Field(Of String)("EmpNo") = EmployeeNo).SingleOrDefault()
If dataRow IsNot Nothing Then

//Set your DataRow
dataRow("EmpName") = EmpName 
dataRow("Commission ") = Commission
DtEmployee.AcceptChanges();
End If

Next 
End If 

从 DtE用人表格中查找行并从 DtE用人表格中查找行并更新该行,或者如果您想要添加新行,则找不到行,只需创建新行,设置值并添加到 DtE用人

DataRow dataRow  = DtEmployee.Tables("Employee").NewRow()
//Set your DataRow
 dataRow("EmpName") = EmpName 
 dataRow("Commission ") = Commission
DtEmployee.Tables("Employee").Rows.Add(dataRow)

希望这能帮上忙...





相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签