<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文主要介紹了C#表單通訊錄系統的範例程式碼,分享給大家,具體如下:
程式碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Data; namespace TongxunLu { public static class DBHelp { static string sqlcon = "Data Source=.;Initial Catalog=TXL;Integrated Security=True"; public static SqlConnection con = new System.Data.SqlClient.SqlConnection(sqlcon); public static SqlCommand cmd = new SqlCommand(); } }
登入頁面:
雙加登入裡面程式碼:
private void btnOK_Click(object sender, EventArgs e) { //驗證使用者名稱與密碼非空 if (txtUserName.Text == "") { MessageBox.Show("使用者名稱不能為空,請輸入!"); txtUserName.Focus(); return; } if (txtUserPwd.Text == "") { MessageBox.Show("密碼不能為空,請輸入!"); txtUserPwd.Focus(); return; } //定義連結字串和連結物件 string sqlcon = "Data Source=.;Initial Catalog=TXL;Integrated Security=True"; SqlConnection con = new System.Data.SqlClient.SqlConnection(sqlcon); //運算元據庫,實現登入功能 try { con.Open(); string sqlcomm = "select distinct COUNT(*) from Users where UserName='" + txtUserName.Text + "' and Password='" + txtUserPwd.Text + "'"; SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlcomm, con); //cmd.Connection = con; //cmd.CommandText = sqlcomm; if ((int)cmd.ExecuteScalar() == 1) { // MessageBox.Show("登入成功"); Users.UserName = txtUserName.Text; FrmMain fmain = new FrmMain(); fmain.Show(); } else { MessageBox.Show("登入失敗"); } // MessageBox.Show("資料庫開啟成功", "提示資訊", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); } catch (Exception ex) { MessageBox.Show(ex.Message, "提示資訊", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); } finally { con.Close(); // MessageBox.Show("資料庫成功關閉", "提示資訊", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); } }
雙擊取消,裡面程式碼:
private void btnCacel_Click(object sender, EventArgs e) { Application.Exit(); }
輸入使用者表裡面的資訊,進行登入:
新增選單欄、工具列、狀態列
選單欄控制元件:MenuStrip 以mus開頭命名
專案名稱以tsm開頭 專案裡的專案以tsmi開頭命名
工具列控制元件:ToolStrip 項顯示圖片和文字更改屬性DisplayStyle,工具列:以tst開頭
狀態列控制元件:StatusStrip,狀態列:以tss命名開頭
當頁面載入那個使用者登入,狀態用Label控制元件就顯示誰的名字,程式碼:
private void FrmMain_Load(object sender, EventArgs e) { //接受登入名 toolStripStatusLabel2.Text += Users.UserName; toolStripStatusLabel3.Text += GetNum(Users.UserName).ToString(); LoadGroup(); }
主頁面裡面的詳細功能程式碼,如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace TongxunLu { public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); } //顯示新增表單 public void add() { frmAdd fadd = new frmAdd(); fadd.Show(); } //顯示修改表單 public void edit() { frmEdit fedit = new frmEdit(); fedit.Show(); } //顯示查詢表單 public void seacher() { frmSeach fseacher=new frmSeach (); fseacher.Show(); } //統計登入聯絡人的個數 public int GetNum(string str1) { int num = 0; try { DBHelp.con.Open(); string sqlcomm = string.Format("select count(*) from BUsicInfo where UserName='{0}'",str1); DBHelp.cmd.Connection = DBHelp.con; DBHelp.cmd.CommandText = sqlcomm; num = (int)DBHelp.cmd.ExecuteScalar(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { DBHelp.con.Close(); } return num; } //統計登入聯絡人分組的個數 public int GetGroupsNum() { int num = 0; try { DBHelp.con.Open(); string sqlcomm = string.Format("select count(*) from BUsicInfo where UserName='{0}' and groups='{1}'",Users.UserName,treeView1.SelectedNode.Text ); DBHelp.cmd.Connection = DBHelp.con; DBHelp.cmd.CommandText = sqlcomm; num = (int)DBHelp.cmd.ExecuteScalar(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { DBHelp.con.Close(); } return num; } //載入treeview控制元件 public void LoadGroup() { try { DBHelp.con.Open(); string sqlcomm = string.Format("select groups from busicInfo where userName='{0}'",Users.UserName); DBHelp.cmd.Connection = DBHelp.con; DBHelp.cmd.CommandText = sqlcomm; SqlDataReader dr = DBHelp.cmd.ExecuteReader(); while(dr.Read()) { treeView1.Nodes.Add(dr[0].ToString()); } dr.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { DBHelp.con.Close(); } } //載入ListView控制元件 public void loadList() { if (treeView1.Nodes.Count == 0) { MessageBox.Show("請重新載入"); listView1.Items.Clear(); return; } if (treeView1.SelectedNode.Index >= 0) { listView1.Clear(); listView1.Columns.Add("姓名", 100); listView1.Columns.Add("工作單位", 100); listView1.Columns.Add("聯絡電話", 100); listView1.Columns.Add("電子郵箱", 100); listView1.Columns.Add("QQ", 100); try { DBHelp.con.Open(); string sqlcomm = string.Format("select * from busicInfo where userName='{0}' and Groups='{1}'",Users.UserName,treeView1.SelectedNode.Text); DBHelp.cmd.CommandText = sqlcomm; DBHelp.cmd.Connection = DBHelp.con; SqlDataReader dr = DBHelp.cmd.ExecuteReader(); while(dr.Read()) { ListViewItem lvi = new ListViewItem(); lvi.Tag = dr[0]; lvi.Text = dr[3].ToString(); lvi.SubItems.Add(dr[4].ToString()); lvi.SubItems.Add(dr[5].ToString()); lvi.SubItems.Add(dr[6].ToString()); lvi.SubItems.Add(dr[7].ToString()); listView1.Items.Add(lvi); } dr.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { DBHelp.con.Close(); } } } private void 增加聯絡人ToolStripMenuItem_Click(object sender, EventArgs e) { add(); } private void toolStripButton1_Click(object sender, EventArgs e) { add(); } private void toolStripButton4_Click(object sender, EventArgs e) { seacher(); } private void FrmMain_Load(object sender, EventArgs e) { //接受登入名 toolStripStatusLabel2.Text += Users.UserName; toolStripStatusLabel3.Text += GetNum(Users.UserName).ToString(); LoadGroup(); } private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { } private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { loadList(); toolStripStatusLabel3.Text = GetGroupsNum().ToString(); } } }
Tablcontrol控制元件:分頁選項:用來分頁:基本資訊和其他資訊
雙擊確定,裡面程式碼:
private void btnAdd_Click(object sender, EventArgs e) { try { DBHelp.con.Open(); DBHelp.cmd.CommandText =string.Format("insert into BusicInfo(userName,Groups,Name,workUnit,Phone,Email,QQ)values('{0}','{1}','{2}','{3}','{4}','{5}','{6}')",Users.UserName,cmbGroup.SelectedValue,txtName.Text,txtWorkUnit.Text,txtPhone.Text,txtEmail.Text,txtQQ.Text); DBHelp.cmd.Connection = DBHelp.con; if ((int)DBHelp.cmd.ExecuteNonQuery() == 1) { MessageBox.Show("新增成功"); } else { MessageBox.Show("失敗"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { DBHelp.con.Close(); } }
載入頁面,雙擊壓面,裡面程式碼
public void LoadGroup() { try { DBHelp.con.Open(); string sqlcomm = string.Format("select groups from busicInfo where userName='{0}'", Users.UserName); DBHelp.cmd.Connection = DBHelp.con; DBHelp.cmd.CommandText = sqlcomm; SqlDataReader dr = DBHelp.cmd.ExecuteReader(); while (dr.Read()) { cmbGroup.Items.Add(dr[0].ToString()); } dr.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { DBHelp.con.Close(); } } private void frmAdd_Load(object sender, EventArgs e) { LoadGroup(); }
雙擊查詢按鈕,裡面程式碼:
private void btnSeach_Click(object sender, EventArgs e) { string sqlcomm = ""; if(txtUserName.Text==""&&txtUserPhone.Text=="") { sqlcomm = string.Format("select * from BusicInfo "); } else if (txtUserName.Text == "") { sqlcomm = string.Format("select * from BusicInfo where Phone='%{0}%' ", txtUserPhone.Text); } else if (txtUserPhone.Text == "") { sqlcomm = string.Format("select * from BusicInfo where UserName='{0}%' ", txtUserName.Text); } else { sqlcomm = string.Format("select * from BusicInfo where UserName='{0}%' and Phone='{1}%' ", txtUserName.Text, txtUserPhone.Text); } try { DBHelp.con.Open(); SqlDataAdapter da = new SqlDataAdapter(sqlcomm,DBHelp.con); DataSet ds = new DataSet(); da.Fill(ds); dataGridView1.DataSource = ds.Tables[0]; } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { DBHelp.con.Close(); } }
功能未完全實現,只是例子。
到此這篇關於C#表單通訊錄系統的範例程式碼的文章就介紹到這了,更多相關C#表單通訊錄系統內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45