中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#怎么實現窗體通訊錄系統功能

發布時間:2022-04-18 13:45:13 來源:億速云 閱讀:180 作者:iii 欄目:開發技術

本文小編為大家詳細介紹“C#怎么實現窗體通訊錄系統功能”,內容詳細,步驟清晰,細節處理妥當,希望這篇“C#怎么實現窗體通訊錄系統功能”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。

具體如下:

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

登錄頁面:

C#怎么實現窗體通訊錄系統功能

 雙加登錄里面代碼:

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

C#怎么實現窗體通訊錄系統功能

輸入用戶表里面的信息,進行登錄:

C#怎么實現窗體通訊錄系統功能

2.創建主頁面:

C#怎么實現窗體通訊錄系統功能

添加菜單欄、工具欄、狀態欄

菜單欄控件:MenuStrip  以mus開頭命名

項目名稱以tsm開頭 項目里的項目以tsmi開頭命名

工具欄控件:ToolStrip  項顯示圖片和文本更改屬性DisplayStyle,工具欄:以tst開頭

狀態欄控件:StatusStrip,狀態欄:以tss命名開頭

C#怎么實現窗體通訊錄系統功能

C#怎么實現窗體通訊錄系統功能

當頁面加載那個用戶登錄,狀態用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.添加聯系人頁面

C#怎么實現窗體通訊錄系統功能

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

C#怎么實現窗體通訊錄系統功能

C#怎么實現窗體通訊錄系統功能

4.修改頁面

C#怎么實現窗體通訊錄系統功能

5.查詢頁面

C#怎么實現窗體通訊錄系統功能

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

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#怎么實現窗體通訊錄系統功能”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

桦南县| 银川市| 长阳| 襄汾县| 色达县| 长葛市| 莒南县| 邹城市| 沾益县| 同仁县| 阿坝县| 湘潭县| 凉山| 沁源县| 叶城县| 镇安县| 齐河县| 灵山县| 两当县| 凤城市| 宣城市| 马龙县| 文昌市| 福海县| 新营市| 桦川县| 教育| 礼泉县| 叙永县| 龙山县| 襄城县| 姜堰市| 平罗县| 武宣县| 五台县| 丰都县| 衡东县| 太仓市| 深州市| 沅陵县| 阜平县|