需要在C#中将数据集转换为字符串

时间:2009-09-21 17:27:22

标签: c# string dataset

拥有我正在查询的Excel数据源并在数据网格中发布数据。诀窍是我想要一个字符串变量中的数据,但我没有这样做。我已经尝试过arrayLists和数组,但我还没有到达那里因为数据集有多种数据类型(数字,字符串)。

请帮帮我。源代码:

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data.OleDb" %>

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System" %>

<script language="C#" runat="server">
protected void Page_Load(Object Src, EventArgs E)
{
string strConn;

    string str="08/PST";
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=|DataDirectory|marks.xls;" +
"Extended Properties=Excel 8.0;";
//You must use the $ after the object you reference in the spreadsheet
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT [Subject ID] FROM [Employees$] where emp_id='" + str + "'", strConn);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet, "ExcelInfo");
DataGrid1.DataSource = myDataSet.Tables["ExcelInfo"].DefaultView;
DataGrid1.DataBind();
}
</script>
<html>
<head><title>Data Exported</title></head>
<body style="FONT-FAMILY: arial">
    <h2>Simple Data Report 
    </h2>
    <hr size="1" />
    <form id="Form1" runat="server">
        <asp:datagrid id="DataGrid1" runat="server" EnableViewState="False" ForeColor="Black" BackColor="White" CellPadding="3" GridLines="None" CellSpacing="1">
            <HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
            <ItemStyle backcolor="#DEDFDE"></ItemStyle>
        </asp:datagrid>
    </form>
</body>
</html>

提前致谢

3 个答案:

答案 0 :(得分:4)

调用DataSet的GetXml()方法。

答案 1 :(得分:2)

目前,您的数据集仅获取数据集中的[主题ID]。那么您会考虑更改查询本身以返回字符串吗?

SQL SERVER:

SELECT CONVERT (VarChar, [Subject ID]) FROM [Employees$] where emp_id=

JET

SELECT CSTR ([Subject ID]) as [Subject ID] FROM [Employees$] where emp_id=

答案 2 :(得分:0)

在我读取每一行时构建了一个字符串,并且能够成功输出

while(reader.Read())

            {
               string str="";

                str = str + reader[4].ToString().PadLeft(10) + '|' + 

reader [5] .ToString()。PadLeft(1)+'|' + reader [6] .ToString()+'\ n';

            }