asp.net串連sql

來源:互聯網
上載者:User

 方式一:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
說明:
<asp:TextBox ID="TextBox1" runat="server" Width="98px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="更新" />
<asp:GridView ID="gv" runat="server">
源碼:
//程式名稱:13-05.aspx.cs
//程式功能:13-05.aspx功能代碼,更新資料
public partial class _13_05 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //建立Connection對象
        SqlConnection sqlcon = new SqlConnection();
        //資料庫連接語句
        sqlcon.ConnectionString = "server =localhost;database =test;uid=wedy;pwd ='123'";
        sqlcon.Open();

        //查詢語句
        string strSelect = "select * from article";
        //建立Command對象
        SqlCommand sqlcmd = new SqlCommand(strSelect, sqlcon);
        SqlDataReader dr = sqlcmd.ExecuteReader();
        gv.DataSource = dr;
        gv.DataBind();
        sqlcon.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection sqlcon1 = new SqlConnection();
        sqlcon1.ConnectionString = "server =localhost;database =test;uid=wedy;pwd ='123'";
        sqlcon1.Open();

        string strTitle = TextBox1.Text.ToString();
        string strContent = TextBox2.Text.ToString();
        string strId = TextBox3.Text.ToString();
        //構造更新資料的SQL語句
        //string strUpdate = string.Format("update article set content ='{0}' where title='{1}'",strContent,strTitle);
        string strUpdate = string.Format("update article set content ='{0}',title='{1}' where id='{2}'", strContent, strTitle, strId);
        //建立Command對象
        SqlCommand sqlcmd1 = new SqlCommand(strUpdate,sqlcon1);
        sqlcmd1.ExecuteNonQuery();
        sqlcon1.Close();
        Response.Redirect("13-05.aspx");
    }
}
------------------------------------------------------------
//程式名稱:13-07.aspx.cs
//程式功能:13-07.aspx功能代碼,輸出單個值
public partial class _13_07 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection sqlcon = new SqlConnection();

        sqlcon.ConnectionString = "server =localhost;database =test;uid=wedy;pwd ='123'";
        sqlcon.Open();
        //該SQL語句作用為:查詢資料表中的資料記錄總數
        string strSelect = "select count(*) from article";
        SqlCommand sqlcmd = new SqlCommand(strSelect, sqlcon);
        //ExecuteScalar()方法將返回單個值,用來執資料列彙總函式
        int x = (int)sqlcmd.ExecuteScalar();
        Label1.Text = "article表中當前的資料記錄總數為:";
        Label1.Text += x.ToString();
    }
}
------------------------------------------------------------

方式二:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
說明:

源碼:
//程式名稱:13-09.aspx.cs
//程式功能:13-09.aspx功能代碼,向DataSet添加DataTable
public partial class _13_09 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //建立Connection對象
        SqlConnection sqlcon = new SqlConnection();
        //建立DataSet對象
        DataSet ds = new DataSet();
        sqlcon.ConnectionString = "Data Source=localhost;Initial Catalog=test;User ID=wedy;Password='123'";

        //串連沒有開啟的情況下,DataAdapter對象的Fill()方法會自己開啟一個串連,使用後會自動關閉
        SqlDataAdapter sda = new SqlDataAdapter("select * from class", sqlcon);
        sda.Fill(ds, "class");//向DataSet中加入一個class表
        if (ds.Tables["class"].IsInitialized == true)
        {
            //判斷表是否被加入,然後輸出相應資訊
            Response.Write("已成功向DataSet中添加名為" + ds.Tables["class"].TableName + "的DataTable");
        }
    }
}
------------------------------------------------------------
方式三:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
說明:
//web.config 配置應用程式的設定:
  <connectionStrings>
    <add name="ConnectionString" connectionString="Data Source=localhost;Initial Catalog=test;User ID=wedy;Password='123'" providerName="System.Data.SqlClient" />
  </connectionStrings>

<asp:GridView ID="GridView1" runat="server"></asp:GridView>
源碼:
//程式名稱:13-10.aspx.cs
//程式功能:向DataSet中加入一個Datatable並綁定到GridView
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//引入所需的命名空間
using System.Data.SqlClient;

public partial class _13_10 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //建立Connection對象
        SqlConnection sqlcon = new SqlConnection();
        //建立DataSet對象
        DataSet ds = new DataSet();
        sqlcon.ConnectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
        //sqlcon.ConnectionString = "Data Source=localhost;Initial Catalog=test;User ID=wedy;Password='123'";
        //串連沒有開啟的情況下,DataAdapter對象的Fill()方法會自己開啟一個串連,使用後會自動關閉
        SqlDataAdapter sda = new SqlDataAdapter("select * from class", sqlcon);
        //向DataSet中加入一個class表
        sda.Fill(ds, "class");
        //將GridView1的資料來源指定為前邊所建立的DataTable
        GridView1.DataSource = ds.Tables["class"];
        //資料繫結
        GridView1.DataBind();
    }
}
------------------------------------------------------------
//程式名稱:13-12.aspx.cs
//程式功能:緩衝DataSet
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _13_12 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataView dv;
        dv = (DataView)Cache["article"];
        //檢查緩衝是否為空白
        if (dv == null)
        {
            DataSet ds = new DataSet();
            //建立Connection對象
            SqlConnection sqlcon = new SqlConnection();
            sqlcon.ConnectionString = "Data Source=localhost;Initial Catalog=test;User ID=wedy;Password='123'";
            //建立DataAdapter對象
            SqlDataAdapter sda = new SqlDataAdapter("select * from article",sqlcon);
            //向DataSet中加入一個article表
            sda.Fill(ds, "article");
            dv = new DataView(ds.Tables["article"]);
            //快取資料
            Cache["article"] = dv;
        }
        //將緩衝綁定到GridView並顯示
        GridView1.DataSource = dv;
        GridView1.DataBind();

        //add by wedy:--------------------------------------------------------
        DataView dv_1;
        dv_1 = (DataView)Cache["class"];
        //檢查緩衝是否為空白
        if (dv_1 == null)
        {
            SqlConnection sqlcon = new SqlConnection();
            sqlcon.ConnectionString = "Data Source=localhost;Initial Catalog=test;User ID=wedy;Password='123'";
           
            DataSet ds_1 = new DataSet();
            SqlDataAdapter sda_1 = new SqlDataAdapter("select * from class", sqlcon);
            sda_1.Fill(ds_1, "class");
            dv_1 = new DataView(ds_1.Tables["class"]);
            Cache["class"] = dv_1;
        }
        GridView2.DataSource = dv_1;
        GridView2.DataBind();
    }
}
------------------------------------------------------------
//程式名稱:13-13.aspx.cs
//程式功能:實現多個DataAdapter對象填充同一DataSet
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient;

public partial class _13_13 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //建立Connection對象
        SqlConnection sqlcon = new SqlConnection();
        //建立DataSet對象
        DataSet ds = new DataSet();
        //設定連接字串
        sqlcon.ConnectionString = "server =localhost;database =test;uid=wedy;pwd ='123'";
        //建立DataAdapter對象的執行個體sda1
        SqlDataAdapter sda1 = new SqlDataAdapter("select * from article", sqlcon);
        //填充DataSet
        sda1.Fill(ds, "article");
        //建立DataAdapter對象的執行個體sda2
        SqlDataAdapter sda2 = new SqlDataAdapter("select * from People", sqlcon);
        //填充DataSet
        sda2.Fill(ds, "People");
        //設定DataSet中"article"表為GridView1的資料來源
        GridView1.DataSource = ds.Tables["article"].DefaultView;
        //資料繫結
        GridView1.DataBind();
        //設定DataSet中"People"表為GridView2的資料來源
        GridView2.DataSource = ds.Tables["People"].DefaultView;
        //資料繫結
        GridView2.DataBind();
    }
}
------------------------------------------------------------

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.