数据源控件

数据源控件:
SqlDataSource
AccessDataSource
ObjectDataSource

ObjectDataSource:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

/// <summary>
///Student 的摘要说明
/// </summary>
public class Student
{
private string strCnn;
public Student()
{
strCnn = ConfigurationManager.ConnectionStrings["studentConnectionString"]
.ConnectionString;

}
public int SID { get; set; }
public string SName { get; set; }
public string Sex { get; set; }
public string Photo { get; set; }

public DataTable StudentSelect()
{
DataSet ds = new DataSet();
using (SqlConnection sqlcnn = new SqlConnection(strCnn))
{
SqlCommand sqlcmm = sqlcnn.CreateCommand();
sqlcmm.CommandText = "select sid,sname,sex,photo from student";
SqlDataAdapter da = new SqlDataAdapter(sqlcmm);
da.Fill(ds);
}
return ds.Tables[0];
}
public List<Student> GetStudents()
{
DataSet ds = new DataSet();
using (SqlConnection sqlcnn = new SqlConnection(strCnn))
{
SqlCommand sqlcmm = sqlcnn.CreateCommand();
sqlcmm.CommandText = "select sid,sname,sex,photo from student";
SqlDataAdapter da = new SqlDataAdapter(sqlcmm);
da.Fill(ds);
}
List<Student> list = new List<Student>();
foreach (DataRow row in ds.Tables[0].Rows)
{
//list.Add(new Student{ SID = (int)row["sid"], SName = row["sname"].ToString(), Sex = row["sex"].ToString(), Photo = row["photo"].ToString()});
Student st = new Student();
st.SID = (int)row["sid"];
st.SName = row["sname"].ToString();
st.Sex = row["sex"].ToString();
st.Photo = row["photo"].ToString();
list.Add(st);
}
return list;
}
public void StudentDelete(int sid)
{
using (SqlDataSource sds = new SqlDataSource()){
sds.DeleteCommand = "delete from student where
sid=@sid";
sds.DeleteParameters.Add("sid", sid.ToString());
sds.ConnectionString = strCnn;
sds.Delete();
}
}
public void StudentUpdate(int sid, string sname, string sex, string photo)
{
using (SqlDataSource sds = new SqlDataSource())
{
sds.UpdateCommand = "update student set
sname=@sname,sex=@sex,photo=@photo where sid=@sid";
sds.UpdateParameters.Add("sid", sid.ToString());
sds.UpdateParameters.Add("sname", sname);
sds.UpdateParameters.Add("sex", sex);
sds.UpdateParameters.Add("photo", photo);
sds.ConnectionString = strCnn;
sds.Update();
}
}
public void StudentUpdate(Student stu)
{
using (SqlDataSource sds = new SqlDataSource())
{
sds.UpdateCommand = "update student set
sname=@sname,sex=@sex,photo=@photo where sid=@sid";
sds.UpdateParameters.Add("sid", stu.SID.ToString());
sds.UpdateParameters.Add("sname", stu.SName);
sds.UpdateParameters.Add("sex", stu.Sex);
sds.UpdateParameters.Add("photo", stu.Photo);
sds.ConnectionString = strCnn;
sds.Update();
}
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值