1 / 13

第 11 章 Web 应用开发

第 11 章 Web 应用开发. 王德俊 上海交通大学继续教育学院. 第 11 章 Web 应用开发. 11.1 关于 ASP.NET 11.2 Web 基础和多层程序体系结构 11.3 ASP.NET 控件和对象 11.4 ASP.NET 数据库应用程序. 11.4 ASP.NET 数据库应用程序. ASP.NET 数据库应用程序 和 C# 窗体数据库应用程序 的开发原理一样 不同之处:前者使用 Web 界面(网页),后者使用 C# 窗体 界面。. 11.4 ASP.NET 数据库应用程序. 11.4.1 数据库的连接和数据浏览.

Download Presentation

第 11 章 Web 应用开发

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第11章 Web应用开发 王德俊 上海交通大学继续教育学院

  2. 第11章 Web应用开发 11.1 关于ASP.NET 11.2 Web基础和多层程序体系结构 11.3 ASP.NET控件和对象 11.4 ASP.NET数据库应用程序

  3. 11.4 ASP.NET数据库应用程序 • ASP.NET数据库应用程序和C#窗体数据库应用程序的开发原理一样 • 不同之处:前者使用Web界面(网页),后者使用C#窗体界面。

  4. 11.4 ASP.NET数据库应用程序 11.4.1 数据库的连接和数据浏览 • 在ASP.NET数据库应用程序中,可以使用ADO.NET中Connection对象来连接数据库。 • 下面语句将建立一个连接到数据库MyDatabase的Connection对象: stringConnectionString = "Data Source =mzq; Initial Catalog =MyDatabase;" + "Persist Security Info =True; User ID =sa; Password =sql2008"; SqlConnectionconn = newSqlConnection (ConnectionString);

  5. 11.4 ASP.NET数据库应用程序 11.4.1 数据库的连接和数据浏览 【例11.3】创建ASP.NET应用程序ConnectionDB浏览指定数据表中的数据: • 在Web窗体上添加一个GridView控件,并适当调整它的位置和大小; • 双击Web窗体,进入窗体的Load事件处理函数,在此函数中编写实现数据库连接和数据浏览功能的代码,运行效果如下:

  6. 核心代码: protectedvoid Page_Load(object sender, EventArgs e) { string ConnectionString = "Data Source=mzq;Initial Catalog=MyDatabase;" + "Persist Security Info=True;User ID=sa;Password=sql2008"; SqlConnection conn = newSqlConnection(ConnectionString); DataSet dataset = new DataSet(); //创建数据集 //创建数据提供者 SqlDataAdapter DataAdapter = newSqlDataAdapter("SELECT * FROM student", conn); //填充数据集dataset,并为本次填充的数据起名“student_table” DataAdapter.Fill(dataset, "student_table"); GridView1.DataSource = dataset; GridView1.DataMember = "student_table"; GridView1.DataBind(); //必须绑定数据 }

  7. 11.4 ASP.NET数据库应用程序 11.4.2 对数据库的增、删、改操作 • Insert、Delete和Update语句属于数据操纵语句,在ASP.NET程序中可利用ADO.NET中的Command对象来执行它们。 【例11.4】SQL代码执行器:创建一个ASP.NET应用程序,提供一个用于输入SQL代码的文本框,在该文本框中可以输入Insert、Delete或Update语句文本(一次只能执行一条语句),执行后将实时显示对应表中的当前数据。

  8. 执行该程序 :

  9. 11.4.2 对数据库的增、删、改操作 • 开发步骤如下: • (1)创建ASP.NET应用程序SQLSIDU,在Web窗体上分别添加GridView、Label、TextBox和Button控件,并适当设置它们的属性、大小和位置

  10. 11.4.2 对数据库的增、删、改操作 • (2)在Default.aspx.cs文件中编写【执行SQL语句】按钮的事件处理代码及其他相关代码。核心代码如下: publicpartialclass _Default : System.Web.UI.Page { privatestring ConnectionString = "Data Source=mzq;Initial Catalog=" + "MyDatabase;Persist Security Info=True;User ID=sa;Password=sql2008"; privateSqlConnection conn = null; privateSqlDataAdapter DataAdapter = null; privateDataSet dataset = null; privateSqlCommand Command = null; protectedvoid Page_Load(object sender, EventArgs e) { try { conn = new SqlConnection(ConnectionString); dataset = new DataSet();

  11. 11.4.2 对数据库的增、删、改操作 DataAdapter = new SqlDataAdapter("SELECT * FROM student", conn); DataAdapter.Fill(dataset, "student_table"); GridView1.DataSource = dataset; GridView1.DataMember = "student_table"; GridView1.DataBind(); //必须绑定数据 } catch (Exception ex) { Response.Write("语法错误:"+ex.Message); Response.End(); } finally { if(conn!=null) conn.Dispose(); if(dataset!=null) dataset.Dispose(); } }

  12. protected void Button1_Click(object sender, EventArgs e) //【执行SQL语句】 { string strSQL = TextBox1.Text; try { conn = new SqlConnection(ConnectionString); Command = new SqlCommand(strSQL, conn); conn.Open(); int n = Command.ExecuteNonQuery(); //执行SQL语句 Response.Write("<script language=javascript>alert('有 " + n.ToString() + " 记录受到影响!');</script>"); } catch (Exception ex) { Response.Write("语法错误:" + ex.Message); } finally { if (conn != null) conn.Close(); if (Command != null) Command.Dispose(); } Page_Load(null,null); //显示表中的数据 } }

  13. 本讲小结 ASP.NET数据库应用程序: ASP.NET数据库应用程序和C#窗体数据库应用程序的开发原理一样 不同之处:前者使用Web界面(网页),后者使用C#窗体界面。

More Related