Tuesday, January 24, 2012

ADO

The database scripts can be found on the syllabus. Here is a description of how to run the scripts with SQL Server Express through visual Studio

Running SQL scripts through Visual Studio

Default.Aspx
<%@ Page Language="C#" AutoEventWireup="true" 
CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>

        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </div>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
        Text="Get Donations" />
    </form>
</body>
</html>
Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection connect = new SqlConnection 
("Data Source=localhost;initial catalog=communityassist;integrated security=true");

            //SqlConnection connect = new SqlConnection 
("Data Source=localhost;initial catalog=communityassist;user=manager;password=p@ssw0rd1");

            //SqlConnection connect2 = new SqlConnection();
            //SqlConnectionStringBuilder builder=new SqlConnectionStringBuilder();
            //builder.DataSource = "localhost";
            //builder.InitialCatalog = "community assist";

            string sql = "Select Distinct DonationDate from donation";

            SqlCommand cmd = new SqlCommand(sql, connect);

            DataSet ds = new DataSet();

            SqlDataAdapter da = new SqlDataAdapter(cmd);

            da.Fill(ds, "Donations");

            DropDownList1.DataSource = ds.Tables["Donations"];
            DropDownList1.DataTextField= "DonationDate";
            DropDownList1.DataBind();
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
         //ado objects

        SqlConnection connect = new SqlConnection
("Data Source=localhost;initial catalog=communityassist;integrated security=true");

        //SqlConnection connect2 = new SqlConnection();
        //SqlConnectionStringBuilder builder=new SqlConnectionStringBuilder();
        //builder.DataSource = "localhost";
        //builder.InitialCatalog = "community assist";

        string sql = "Select DonationDate, DonationAmount from donation Where DonationDate=@date";

        SqlCommand cmd = new SqlCommand(sql, connect);
        cmd.Parameters.AddWithValue("@date", DateTime.Parse(DropDownList1.SelectedItem.ToString()));

        DataSet ds = new DataSet();

        SqlDataAdapter da = new SqlDataAdapter(cmd);

        da.Fill(ds,"Donations");

        GridView1.DataSource = ds.Tables["Donations"];
        GridView1.DataBind();

         
       
    }
}

No comments:

Post a Comment