溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#怎么實(shí)現(xiàn)窗體通訊錄系統(tǒng)功能

發(fā)布時(shí)間:2022-04-18 13:45:13 來(lái)源:億速云 閱讀:168 作者:iii 欄目:開(kāi)發(fā)技術(shù)

本文小編為大家詳細(xì)介紹“C#怎么實(shí)現(xiàn)窗體通訊錄系統(tǒng)功能”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“C#怎么實(shí)現(xiàn)窗體通訊錄系統(tǒng)功能”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。

具體如下:

C#怎么實(shí)現(xiàn)窗體通訊錄系統(tǒng)功能

1.首先創(chuàng)建DBhelp類用來(lái)連接數(shù)據(jù)庫(kù)

代碼如下:

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

登錄頁(yè)面:

C#怎么實(shí)現(xiàn)窗體通訊錄系統(tǒng)功能

 雙加登錄里面代碼:

 private void btnOK_Click(object sender, EventArgs e)
        {
            //驗(yàn)證用戶名與密碼非空
            if (txtUserName.Text == "")
            {
                MessageBox.Show("用戶名不能為空,請(qǐng)輸入!");
                txtUserName.Focus();
                return;
            }
            if (txtUserPwd.Text == "")
            {
                MessageBox.Show("密碼不能為空,請(qǐng)輸入!");
                txtUserPwd.Focus();
                return;
            }
            //定義鏈接字符串和鏈接對(duì)象        
            string sqlcon = "Data Source=.;Initial Catalog=TXL;Integrated Security=True";
            SqlConnection con = new System.Data.SqlClient.SqlConnection(sqlcon);
            //操作數(shù)據(jù)庫(kù),實(shí)現(xiàn)登錄功能
            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("數(shù)據(jù)庫(kù)打開(kāi)成功", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            }
            finally
            {
                con.Close();
                // MessageBox.Show("數(shù)據(jù)庫(kù)成功關(guān)閉", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            }              
        }

雙擊取消,里面代碼:

private void btnCacel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

C#怎么實(shí)現(xiàn)窗體通訊錄系統(tǒng)功能

輸入用戶表里面的信息,進(jìn)行登錄:

C#怎么實(shí)現(xiàn)窗體通訊錄系統(tǒng)功能

2.創(chuàng)建主頁(yè)面:

C#怎么實(shí)現(xiàn)窗體通訊錄系統(tǒng)功能

添加菜單欄、工具欄、狀態(tài)欄

菜單欄控件:MenuStrip  以mus開(kāi)頭命名

項(xiàng)目名稱以tsm開(kāi)頭 項(xiàng)目里的項(xiàng)目以tsmi開(kāi)頭命名

工具欄控件:ToolStrip  項(xiàng)顯示圖片和文本更改屬性DisplayStyle,工具欄:以tst開(kāi)頭

狀態(tài)欄控件:StatusStrip,狀態(tài)欄:以tss命名開(kāi)頭

C#怎么實(shí)現(xiàn)窗體通訊錄系統(tǒng)功能

C#怎么實(shí)現(xiàn)窗體通訊錄系統(tǒng)功能

當(dāng)頁(yè)面加載那個(gè)用戶登錄,狀態(tài)用Label控件就顯示誰(shuí)的名字,代碼:

 private void FrmMain_Load(object sender, EventArgs e)
        {
            //接受登錄名
            toolStripStatusLabel2.Text += Users.UserName;
            toolStripStatusLabel3.Text += GetNum(Users.UserName).ToString();
           LoadGroup();
        }

主頁(yè)面里面的詳細(xì)功能代碼,如下:

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();
        }
        //統(tǒng)計(jì)登錄聯(lián)系人的個(gè)數(shù)
        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;
 
        }
        //統(tǒng)計(jì)登錄聯(lián)系人分組的個(gè)數(shù)
        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("請(qǐng)重新加載");
                listView1.Items.Clear();
                return;
            }
            if (treeView1.SelectedNode.Index >= 0)
            {
                listView1.Clear();
                listView1.Columns.Add("姓名", 100);
                listView1.Columns.Add("工作單位", 100);
                listView1.Columns.Add("聯(lián)系電話", 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 增加聯(lián)系人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.添加聯(lián)系人頁(yè)面

C#怎么實(shí)現(xiàn)窗體通訊錄系統(tǒng)功能

Tablcontrol控件:分頁(yè)選項(xiàng):用來(lái)分頁(yè):基本信息和其他信息 

雙擊確定,里面代碼:

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

加載頁(yè)面,雙擊壓面,里面代碼

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

C#怎么實(shí)現(xiàn)窗體通訊錄系統(tǒng)功能

C#怎么實(shí)現(xiàn)窗體通訊錄系統(tǒng)功能

4.修改頁(yè)面

C#怎么實(shí)現(xiàn)窗體通訊錄系統(tǒng)功能

5.查詢頁(yè)面

C#怎么實(shí)現(xiàn)窗體通訊錄系統(tǒng)功能

雙擊查詢按鈕,里面代碼:

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#怎么實(shí)現(xiàn)窗體通訊錄系統(tǒng)功能”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI