1 / 84

C#.NET 数据库应用 闪电入门教程

C#.NET 数据库应用 闪电入门教程. Why such a fuss?. Why such a fuss?. “ 蜡笔小编 ” 学习一年半,困惑一大堆: 如何在程序中访问数据库 如何将网页和数据库联系起来 .NET 程序中如何显示和修改数据中的数据 。。。。。 其实很简单,只需要闪电 84 分钟 / 页 。. 目标项目:网页聊天室. 目的:做一个简单的聊天网页 支持多人同时使用该页面进行聊天 练习: 向数据库添加数据; 在网页上显示数据。. 数据库 /RDBMS ,就是 “ 表 ” 们. 字段. 记录.

cole-cross
Download Presentation

C#.NET 数据库应用 闪电入门教程

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. C#.NET 数据库应用闪电入门教程 Why such a fuss?

  2. Why such a fuss? • “蜡笔小编”学习一年半,困惑一大堆: • 如何在程序中访问数据库 • 如何将网页和数据库联系起来 • .NET程序中如何显示和修改数据中的数据 • 。。。。。 • 其实很简单,只需要闪电84分钟/页。

  3. 目标项目:网页聊天室 • 目的:做一个简单的聊天网页 • 支持多人同时使用该页面进行聊天 • 练习: • 向数据库添加数据; • 在网页上显示数据。

  4. 数据库/RDBMS,就是“表”们 字段 记录

  5. 下面在MySQL中建一个聊天用的表:QQMSG如果你不需要学习这些小儿科,猛击此按钮下面在MySQL中建一个聊天用的表:QQMSG如果你不需要学习这些小儿科,猛击此按钮 • 首先安装免费的MySQL • 可以从MySQL.com下载 • 也可以下载一个Apache+PHP+MySQL的捆绑包:AppServ • AppServ下载地址: • http://www.appservnetwork.com/ • 下面假定您的本机(localhost)上已经运行了MySQL,在3306端口

  6. 其次下载一个MySQL管理工具 • 现在MySQL的管理工具名字叫: • MySQL Workbench • 下载:http://www.mysql.com/products/workbench/ • 这个工具可以方便地在数据库中建表、浏览、修改等等。

  7. 上图!MySQL Workbench 点这个

  8. 双击test数据库, 选为当前schema, 然后“add table”

  9. 表名:qqmsg 然后:

  10. 建4个字段 确定

  11. 加点数据

  12. 保存

  13. 这个自动生成的SQL语句, 是我们学习的好模板!

  14. 好了,数据表准备好了 • 我们有一个MySQL数据库服务器运行于本机(localhost) • 其中有一个数据库名为test • Test中有一个表名为qqmsg • Qqmsg包含4个字段 Let’s roll!

  15. ADO.NET是个神马? • “ADO.NET 是一组向 .NET Framework 程序员公开数据访问服务的类。”--msdn 应用程序 数据库

  16. 访问数据库的一般步骤 • 使用Connection对象建立数据库连接 • 初始化Command对象,设置SQL命令 • 执行SQL并获得返回数据结果 不同的操作,以及不同的数据库,有不同的Connection和Command的实现类,例如:MySqlCommand。

  17. 说明1:Connection字符串 • Connection的创建,使用ConnectionString参数,格式为: • “server=host;user id=name;database=dbname;password=xxx” • 例如: • “server=localhost;user id=root;database=test;password=12345”

  18. 说明2:Command对象 • Command一般代表一个要执行的SQL命令 • 例如 • “select * from mytable”

  19. 说明3:执行Command • 根据实际的Command的不同,Command执行后返回的结果也不同 • 例如可以为一个IDataReader的实现,然后可以从该DataReader中获取读到的数据。

  20. 一个简单的例子:访问MySql数据库 • MySql.Data.MySqlClient.MySqlConnection c = new MySql.Data.MySqlClient.MySqlConnection("server=localhost;user id=root;database=test;password=123“); • c.Open(); • MySqlCommand m = new MySqlCommand("select * from qqmsg", c); • MySqlDataReader r = m.ExecuteReader(); 请同志们认清形势,只有4行!

  21. 下面用一个窗体,运行一下这个小小4行程序

  22. 做一个窗体,包含一个button,一个多行文本框

  23. 打开“解决方案资源管理器”

  24. 添加对“MySQL.Data”的引用

  25. 为按钮编程 !!! • using MySql.Data.MySqlClient; MySqlConnection c = newMySqlConnection("server=localhost;user id=root;database=test;password=123"); c.Open(); MySqlCommand m = new MySqlCommand("select * from qqmsg", c); MySqlDataReader r = m.ExecuteReader(); String s = ""; while (r.Read()) { s += r.GetString(0) + "\n" + r.GetString(1) + "\n" + r.GetString(2) + "\n" + r.GetString(3) + "\n"; } richTextBox1.Text = s; c.Close(); 获得当前数据行第1列的数据,并使用String类型

  26. 这个例子够简单!

  27. 另一种访问MySql数据库的方法 • Live long and prosper! There’s always another way...

  28. 打开:视图->服务器资源管理器

  29. 新建一个数据连接

  30. 添加BindingSource控件,并设置Datasource属性

  31. 添加DataGrid控件并绑定数据源

  32. 运行结果:竟然没有写代码!

  33. 再来看看如何在网页上连接数据库 就一点点不同。。

  34. 新建一个ASP.NET网站 (可参考ASP.NET闪电入门教程)

  35. 打开服务器视图 添加一个数据库连接

  36. 在页面上面添加一个sql数据源 然后配置连接 指向

  37. 就选我们刚刚 配置好的连接

  38. 注意MySQL不允许中括号,所以要手工写SQL语句

  39. !!!

More Related