欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

利用visual studio c#开发通讯录系统

程序员文章站 2022-07-14 09:30:55
...

封装类

class Class1
    {
        public static SqlConnection con;
        public static DataTable dt;
        //数据库连接
        public static SqlConnection getcon()
        {
            string str = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Txl;Data Source=DESKTOP-3DTIOB8"
            con = new SqlConnection(str);
            con.Open();
            return con;
        }
        //查询,获取数据表	
         public static  DataTable getdatable(string sql) 
        {
            getcon();
            SqlDataAdapter da = new SqlDataAdapter(sql,con);
            dt = new DataTable();
            da.Fill(dt);
            con.Close();
            return dt;
        }
        //增删改
        public static int geExecuteNonQuery(string sql) 
        {
            con=getcon();
            SqlCommand cmd = new SqlCommand(sql,con);
            int i = cmd.ExecuteNonQuery();
            return i;
        }
        //查询
        public static int geExecuteScalar(string sql) 
        {
            getcon();
            SqlCommand cmd = new SqlCommand(sql,con);
            int i = Convert.ToInt32(cmd.ExecuteScalar());
            return i;
           
        }
    }

登录模块

利用visual studio c#开发通讯录系统
代码

public partial class Login : Form
    {
        public static string user = null;//用户名,获取当前登录的用户名
        public Login()
        {
            InitializeComponent();
        }
        //登录页面布局
        private void Form1_Load(object sender, EventArgs e)
        {
         
        }
        //登录按钮
 	private void button1_Click(object sender, EventArgs e)
        {
            string sql = "select count(*) from Login where Users='"+textBox1.Text.Trim()+"' and Password='"+textBox2.Text.Trim()+"'";
            int i = Class1.geExecuteScalar(sql);//调用封装类的查询方法
            if (i > 0)//说明数据表有与输入的用户名和密码匹配
            {
                user = textBox1.Text.Trim();//获取用户名
                Manager f = new Manager();
                f.Show();//打开manager页面
                this.Hide();//隐藏当前页面
            }
            else
            {
                MessageBox.Show("用户名或密码有误!请重新输入");
            }
     }
   }

主界面模块

利用visual studio c#开发通讯录系统代码

public partial class Manager : Form
    {
        public Manager()
        {
            InitializeComponent();
        }
        private void Manager_Load(object sender, EventArgs e)
        {
            string sql = "select * from Connection where Users='" + Login.user + "'";
            dataGridView1.DataSource = Class1.getdatable(sql);
            int count=dataGridView1.RowCount-1;//获取datagridview的行数
            toolStripStatusLabel1.Text = "当前用户"+Login .user+"目前有个"+count+"联系人";
            string sql2 = "select distinct 组别 from Connection where Users='"+Login.user+"'";
            DataTable dt = Class1.getdatable(sql2);
            for (int i = 0; i < dt.Rows.Count; i++) 
            {
                treeView1.Nodes.Add(dt.Rows[i][0].ToString());
            }
        }
        //点击获取节点
        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            string s = treeView1.SelectedNode.Text;
            string sql = "select * from Connection where Users='" + Login.user + "' and 组别='" + s + "'";
            dataGridView1.DataSource = Class1.getdatable(sql);
        }
        
        //解决点击节点滞后问题
        private void treeView1_MouseDown(object sender, MouseEventArgs e)
        {
            if ((sender as TreeView) != null)
            {
            	treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y);
             }
        }
        //点击添加联系人等按钮,显示对应的界面
         private void toolStripLabel1_Click(object sender, EventArgs e)
        {
            Add f = new Add();
            f.Show();
        }
        private void toolStripLabel2_Click(object sender, EventArgs e)
        {
            xiugai f = new xiugai();
            f.Show();
        }
         private void toolStripLabel3_Click(object sender, EventArgs e)
        {
            Del f = new Del();
            f.Show();
        }
        private void toolStripLabel4_Click(object sender, EventArgs e)
        {
            Check f = new Check();
            f.Show();
        }
        private void toolStripLabel5_Click(object sender, EventArgs e)
        {
            Manager_Load(null,null);
        }

添加联系人模块

利用visual studio c#开发通讯录系统
代码

 public partial class Add : Form
    {
        public Add()
        {
            InitializeComponent();
        }
        private void Add_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;//显示items第一个值
            textBox1.Text =  Login.user;//绑定用户
            textBox1.ReadOnly = true;//只读
        }
        private void button1_Click(object sender, EventArgs e)
        {
           if (comboBox1.SelectedIndex != 0) 
            {
                string sql = "insert into Connection (编号,姓名,组别,电话,单位,邮件,Users)values('" + textBox2.Text.Trim() + "','" + textBox3.Text.Trim() + "','"+comboBox1.SelectedItem.ToString()+"','" + textBox4.Text.Trim() + "','" + textBox5.Text.Trim() + "','" + textBox6.Text.Trim() + "','"+textBox1.Text.Trim()+"')";
                int i = Class1.geExecuteNonQuery(sql);
                if (i > 0)
                    MessageBox.Show("增加成功");  
                 else
                    MessageBox.Show("增加失败");
                button2_Click(null, null);//清空
              }
            else    
            MessageBox.Show("请选择组别");
        }
        private void button2_Click(object sender, EventArgs e)
        {
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            comboBox1.SelectedIndex = 0;//显示items第一个值
        }
    }     

删除联系人模块

利用visual studio c#开发通讯录系统
代码


public partial class Del : Form
    {
        public Del()
        {
            InitializeComponent();
        }
        private void Del_Load(object sender, EventArgs e)
        {
            string sql = "select * from Connection where Users='"+Login.user+"'";
            dataGridView1.DataSource = Class1.getdatable(sql);
            textBox1.ReadOnly = true;
            textBox2.ReadOnly = true;
            textBox3.ReadOnly = true;
            textBox4.ReadOnly = true;
          }
          private void button1_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value.ToString());
            string sql = "delete from Connection where ID='" + id + "'";
            int i = Class1.geExecuteNonQuery(sql);
            if(i>0) MessageBox.Show("删除成功");
            Del_Load(null, null);
            dataGridView1_Click(null, null);
            }
        private void dataGridView1_Click(object sender, EventArgs e)
        {
            textBox1.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            textBox2.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            textBox3.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            textBox4.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
        }
    }

修改联系人模块

利用visual studio c#开发通讯录系统
代码

public partial class xiugai : Form
    {
        public xiugai()
        {
            InitializeComponent();
        }
         private void xiugai_Load(object sender, EventArgs e)
        {
            string sql = "select * from Connection where Users='" + Login.user.Trim() + "'";
            dataGridView1.DataSource = Class1.getdatable(sql);
        }
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            
            textBox1.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            textBox2.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            comboBox1.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            textBox3.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            textBox4.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
            textBox5.Text = dataGridView1.CurrentRow.Cells[6].Value.ToString();  
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string sql = "update Connection set 编号='" + textBox1.Text.Trim() + "',姓名='" + textBox2.Text.Trim() + "',组别='" + comboBox1.Text.Trim() + "',电话='" + textBox3.Text.Trim() + "',单位='" + textBox4.Text.Trim() + "',邮件='" + textBox5.Text.Trim() + "'where ID='"+dataGridView1.CurrentRow.Cells[0].Value+"'";
            int i = Class1.geExecuteNonQuery(sql);
            if (i > 0)
                MessageBox.Show("修改成功");
            else
                MessageBox.Show("修改失败");
            xiugai_Load(null, null);
            button2_Click(null, null);//清空
             }
             private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            comboBox1.Text =null;
        }
        private void button3_Click(object sender, EventArgs e)
        {
            this.dataGridView1.CurrentCell = this.dataGridView1[this.dataGridView1.CurrentCell.ColumnIndex, 0];
            dataGridView1_CellClick(null,null);
        }
        private void button4_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.CurrentCell.RowIndex > 0)
                this.dataGridView1.CurrentCell = this.dataGridView1[this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex - 1];
            dataGridView1_CellClick(null, null);
        }
        private void button5_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.CurrentCell.RowIndex < this.dataGridView1.RowCount - 1)
                this.dataGridView1.CurrentCell = this.dataGridView1[this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex + 1];
            dataGridView1_CellClick(null, null);
        }
        private void button6_Click(object sender, EventArgs e)
        {
            this.dataGridView1.CurrentCell = this.dataGridView1[this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.RowCount - 2];
            dataGridView1_CellClick(null, null);
        }
    }

查询联系人模块

利用visual studio c#开发通讯录系统
代码

    public partial class Check : Form
    {
        public Check()
        {
            InitializeComponent();
        }
        private void Check_Load(object sender, EventArgs e)
        {
            string sql = "select * from Connection where ID='0'";
            dataGridView1.DataSource = Class1.getdatable(sql);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string sql = "select * from Connection where ( 姓名 like '%"+textBox1.Text.Trim()+"%' or 电话 like '"+textBox2.Text.Trim()+"%') and  Users='"+Login.user+"'";
            dataGridView1.DataSource = Class1.getdatable(sql);
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex < 0)
                MessageBox.Show("请选择条件");
            else 
            {
                string sql = "select * from Connection where " + comboBox1.Text.Trim() + " like '%" + textBox3.Text.Trim() + "%' and  Users='" + Login.user + "'";
                dataGridView1.DataSource = Class1.getdatable(sql);
            }
        }
        private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {
            comboBox1.Text =null;
            textBox3.Text = null;
        }
        private void comboBox1_MouseClick(object sender, MouseEventArgs e)
        {
            textBox1.Text =null;
            textBox2.Text =null;
        }      
    }