object ADO.NET

Download Report

Transcript object ADO.NET

1



สร้ าง datasource ติดต่อกับตาราง UserPic เพื่อเลือก username และ รูปภาพ
ขึ ้นมาแสดงผล โดยให้ คา่ พารามิเตอร์ username มาจาก Session
"SELECT * FROM [UserPic] WHERE ([username] = @username)”
สร้ าง FormView แล้ วจับคูก่ บั datasource ข้ างต้ น
สร้ าง <asp:Image> ภายใน FormView โดย Edit DataBindings ดังรูป
2

สร้ างตารางโดยไปที่ Database explorer > table > add new table
◦ กาหนดให้ สร้ างตาราง Student เก็บข้ อมูลนักศึกษา
3


สร้ าง DataSource ติดต่อกับตาราง Student โดยใช้ คาสัง่ select * from student
กาหนดให้ DataSource สามารถเพิ่มคาสัง่ insert/update/delete ได้
4


สร้ าง FormView เพื่อใช้ แสดงผลข้ อมูล Student
สามารถเข้ าไปแก้ ไข template ได้ ที่ Edit Template
5
protected void exeSQL(string ins_cmd) {
try {
SqlCommand insert_cmd = new SqlCommand(ins_cmd, dbcon.conn);
insert_cmd.ExecuteNonQuery();
lbStatus.Text = "เพิ่มข้ อมูลสาเร็จ";
}
catch (SqlException se)
{
lbStatus.Text = "เพิ่มข้ อมูลไม่สาเร็จ " + se.ToString();
}
}
6
protected void Button1_Click(object sender, EventArgs e)
{
bool isPay;
double mid,final;
char grade;
string ins_cmd;
connect_db();
foreach (GridViewRow gvr in GridView1.Rows) {
isPay = ((CheckBox)(gvr.FindControl("cbPay"))).Checked;
if (isPay == true) {
mid = Convert.ToDouble(((TextBox)(gvr.FindControl("tbMid"))).Text);
final = Convert.ToDouble(((TextBox)(gvr.FindControl("tbFinal"))).Text);
grade = 'A';
ins_cmd = “ เขียนคาสัง่ update………";
ins_cmd += " where studentID='"+gvr.Cells[0].Text+"'";
exeSQL(ins_cmd);
}
}
}
dbcon.closeDB();
GridView1.DataBind();
protected void connect_db()
{
7
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e) {
char x;
if (e.Row.RowType == DataControlRowType.DataRow) {
x = Convert.ToChar( DataBinder.Eval(e.Row.DataItem, "grade"));
if (x == 'F') {
e.Row.BackColor = System.Drawing.Color.Red;
}
}
}
8