首頁 > 軟體

C#表單通訊錄系統的範例程式碼

2022-04-18 16:00:52

本文主要介紹了C#表單通訊錄系統的範例程式碼,分享給大家,具體如下:

1.首先建立DBhelp類用來連線資料庫

程式碼如下:

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();
        }

輸入使用者表裡面的資訊,進行登入:

2.建立主頁面:

新增選單欄、工具列、狀態列

選單欄控制元件: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();
            
        }
        
    }
}

3.新增聯絡人頁面

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();
        }

4.修改頁面

5.查詢頁面

雙擊查詢按鈕,裡面程式碼:

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!


IT145.com E-mail:sddin#qq.com