ASP.NET Tutorial/ADO.net Database/CSV

Материал из .Net Framework эксперт
Версия от 11:56, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Create connection to csv text based database (C#)

<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
    void Page_Load(object sender, EventArgs e) {
    
        string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Database; Extended Properties=""text;HDR=YES;"";";
        string CommandText = "select * from csv.txt";
    
        OleDbConnection myConnection = new OleDbConnection(ConnectionString);
        OleDbCommand myCommand = new OleDbCommand(CommandText, myConnection);
    
        myConnection.Open();
    
        DataGrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
        DataGrid1.DataBind();
    
        myConnection.Close();
    }
</script>
<html>
<head>
</head>
<body>
    <form 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>


Load csv file to DataTable (C#)

<%@ Page Language="C#" Debug="true" %>
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
  if(!IsPostBack)
  {
    string MyFileName;
    System.IO.StreamReader ObjectStreamReader = null;
    System.Data.DataTable ColorTable = new System.Data.DataTable("colors");
    String[] fileRow;
    System.Data.DataColumn dc;
    System.Data.DataRow dr;
    dc = new System.Data.DataColumn("Hex",typeof(string));
    ColorTable.Columns.Add(dc);
    dc = new System.Data.DataColumn("String",typeof(string));
    ColorTable.Columns.Add(dc);
    MyFileName = Page.MapPath("Data.csv");
    try 
    {
      ObjectStreamReader = new System.IO.StreamReader (MyFileName);
      while (ObjectStreamReader.Peek() > -1) {
        dr = ColorTable.NewRow();
        fileRow = ObjectStreamReader.ReadLine().Split(",");
        dr[0] = fileRow[0];
        dr[1] = fileRow[1];
        ColorTable.Rows.Add(dr);
      }
      Label1.Text = "Select a color:";    
      DropDownList1.DataSource = ColorTable;
      DropDownList1.DataTextField = "String";
      DropDownList1.DataValueField = "Hex";
      DropDownList1.DataBind();
    } 
    catch (Exception ObjectError) 
    {
      Label1.Text = ObjectError.Message;
      DropDownList1.Visible = false;
    } 
    finally 
    {
      if (ObjectStreamReader != null) 
      {
        ObjectStreamReader.Close();
      }
    }
  }  
}
</script>
<html>
  <head>
  </head>
  <body>
    <form runat="server">
      <asp:Label id="Label1" runat="server"></asp:Label>
      <asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
    </form>
  </body>
</html>


Load csv file to DataTable (VB)

<%@ Page Language="VB" Debug="true" %>
<script runat="server">
Sub Page_Load (Sender As Object, E As EventArgs)
  If Not IsPostBack Then
    Dim MyFileName as String
    Dim ObjectStreamReader as System.IO.StreamReader
    Dim ColorTable As New System.Data.DataTable("Colors")
    Dim fileRow() As String
    Dim dc As System.Data.DataColumn
    Dim dr As System.Data.DataRow
    dc = new System.Data.DataColumn("Hex",GetType(String))
    ColorTable.Columns.Add(dc)
    dc = new System.Data.DataColumn("String",GetType(String))
    ColorTable.Columns.Add(dc)
    MyFileName = Page.MapPath("Data.csv")
    Try
      ObjectStreamReader = new System.IO.StreamReader (MyFileName)
      While ObjectStreamReader.Peek() > -1
        dr = ColorTable.NewRow()
        fileRow = ObjectStreamReader.ReadLine().Split(",")
        dr(0) = fileRow(0)
        dr(1) = fileRow(1)
        ColorTable.Rows.Add(dr)
      End While
      Label1.Text = "Select a color:"    
      DropDownList1.DataSource = ColorTable
      DropDownList1.DataTextField = "String"
      DropDownList1.DataValueField = "Hex"
      DropDownList1.DataBind()
    Catch ObjectError as Exception
      Label1.Text = ObjectError.Message
      DropDownList1.Visible = False
    Finally
      If Not ObjectStreamReader Is nothing Then
        ObjectStreamReader.Close()
      End If
    End Try
  End If
End Sub
</script>
<html>
  <head>
  </head>
  <body>
    <form runat="server">
      <asp:Label id="Label1" runat="server"></asp:Label>
      <asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
    </form>
  </body>
</html>