asp.net连接sql

 方式一:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
说明:
<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();
    }
}
------------------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值