• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#基本数据库操作

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
 1    
2 protected void ViewData_Click(object sender, EventArgs e)
3 {
4 string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=";
5 strConnection += Server.MapPath("mydata.mdb");
6 OleDbConnection objConnection = new OleDbConnection(strConnection);
7
8 OleDbCommand objCommand = new OleDbCommand("select*from 通信录", objConnection);
9 objConnection.Open();
10 OleDbDataReader objDataReader = objCommand.ExecuteReader();
11 String str = " ";
12 ArrayList values = new ArrayList();
13 while (objDataReader.Read())
14 {
15 TBId.Text=Convert.ToString(objDataReader["ID"]);
16 TBName.Text = Convert.ToString(objDataReader["name"]);
17 TBClass.Text=Convert.ToString(objDataReader["classm"]);
18 TBTelephone.Text = Convert.ToString(objDataReader["telephone"]);
19 TBEmail.Text = Convert.ToString(objDataReader["Email"]);
20 TBAdress.Text = Convert.ToString(objDataReader["Adress"]);
21
22 String str2=Convert.ToString(objDataReader["ID"]) + str + Convert.ToString(objDataReader["name"]) + str + Convert.ToString(objDataReader["classm"]) + str + Convert.ToString(objDataReader["telephone"]) + str + Convert.ToString(objDataReader["Email"])+ str + Convert.ToString(objDataReader["Adress"]);
23 values.Add(str2);
24 //Response.Write("<br>");//输出回车换行
25 }
26 ListBox.DataSource = values;
27 ListBox.DataBind();
28 values.Clear();
29 objDataReader.Close();
30 objConnection.Close();
31 }
32 protected void FindData_Click(object sender, EventArgs e)
33 {
34 string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=";
35 strConnection += Server.MapPath("mydata.mdb");
36 OleDbConnection objConnection = new OleDbConnection(strConnection);
37 objConnection.Open();
38 string m_select;
39 string m_id = Convert.ToString(TBId.Text);
40 m_select = "select*from 通信录 where ID ='" + m_id + "'";
41 OleDbCommand objCommand = new OleDbCommand(m_select, objConnection);
42 OleDbDataReader objDataReader = objCommand.ExecuteReader();
43 while (objDataReader.Read())
44 {
45 TBId.Text = Convert.ToString(objDataReader["ID"]);
46 TBName.Text = Convert.ToString(objDataReader["name"]);
47 TBClass.Text = Convert.ToString(objDataReader["classm"]);
48 TBTelephone.Text = Convert.ToString(objDataReader["telephone"]);
49 TBEmail.Text = Convert.ToString(objDataReader["Email"]);
50 TBAdress.Text = Convert.ToString(objDataReader["Adress"]);
51 }
52 objDataReader.Close();
53 objConnection.Close();
54
55 }
56 protected void AddData_Click(object sender, EventArgs e)
57 {
58 string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=";
59 strConnection += Server.MapPath("mydata.mdb");
60 OleDbConnection objConnection = new OleDbConnection(strConnection);
61 string StrInsert;
62 string m_id = Convert.ToString(TBId.Text);
63 string m_name = Convert.ToString(TBName.Text);
64 string m_class = Convert.ToString(TBClass.Text);
65 string m_telephone = Convert.ToString(TBTelephone.Text);
66 string m_email = Convert.ToString(TBEmail.Text);
67 string m_adress = Convert.ToString(TBAdress.Text);
68 StrInsert = "insert into 通信录(ID,name,classm,telephone,Email,Adress)values('" + m_id + "','" + m_name + "','" + m_class + "','" + m_telephone + "','" + m_email + "','" + m_adress + "')";
69 OleDbCommand objCommand = new OleDbCommand(StrInsert, objConnection);
70 objConnection.Open();
71 int a = objCommand.ExecuteNonQuery();
72 objConnection.Close();
73 }
74 protected void UpdateData_Click(object sender, EventArgs e)
75 {
76 string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=";
77 strConnection += Server.MapPath("mydata.mdb");
78 OleDbConnection objConnection = new OleDbConnection(strConnection);
79 objConnection.Open();
80 string m_update;
81 string m_id = Convert.ToString(TBId.Text);
82 string m_name = Convert.ToString(TBName.Text);
83 m_update = "update 通信录 set name='" + m_name + "'where ID='" + m_id + "'";
84 OleDbCommand objCommand = new OleDbCommand(m_update, objConnection);
85
86 OleDbDataReader objDataReader = objCommand.ExecuteReader();
87 objDataReader.Close();
88 objConnection.Close();
89 }
90


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
与动态执行的C# 代码进行通讯发布时间:2022-07-10
下一篇:
(转)C#快速高效率复制对象的方式发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap