Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of form address Book system in C #

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "how to achieve the function of the form address book system in C#". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to achieve the function of the form address book system in C#" can help you solve your doubts. Let's follow the editor's way of thinking to learn new knowledge.

The details are as follows:

1. First create a DBhelp class to connect to the database

The code is as follows:

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

Login page:

Double plus login code:

Private void btnOK_Click (object sender, EventArgs e) {/ / verify that the username and password are not empty if (txtUserName.Text = "") {MessageBox.Show ("user name cannot be empty, please enter!") ; txtUserName.Focus (); return;} if (txtUserPwd.Text = = "") {MessageBox.Show ("password cannot be empty, please enter!") ; txtUserPwd.Focus (); return;} / / defines the link string and link object string sqlcon = "Data Source=.;Initial Catalog=TXL;Integrated Security=True"; SqlConnection con = new System.Data.SqlClient.SqlConnection (sqlcon) / / operate the database to achieve the login function 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 ("login successful"); Users.UserName = txtUserName.Text; FrmMain fmain = new FrmMain () Fmain.Show ();} else {MessageBox.Show ("login failed");} / / MessageBox.Show ("database opened successfully", "prompt message", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) } catch (Exception ex) {MessageBox.Show (ex.Message, "prompt", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);} finally {con.Close () / / MessageBox.Show ("Database closed successfully", "prompt message", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);}}

Double-click to cancel, inside the code:

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

Enter the information in the user table to log in:

two。 Create the main page:

Add menu bar, toolbar, status bar

Menu bar controls: MenuStrip is named with the beginning of mus

The project name begins with tsm. The project in the project starts with tsmi.

Toolbar controls: ToolStrip items display pictures and text change properties DisplayStyle, toolbars: start with tst

Status bar control: StatusStrip, status bar: starts with tss name

When the page loads which user logs in, the status uses the Label control to display whose name, code:

Private void FrmMain_Load (object sender, EventArgs e) {/ / accept login toolStripStatusLabel2.Text + = Users.UserName; toolStripStatusLabel3.Text + = GetNum (Users.UserName). ToString (); LoadGroup ();}

The detailed function code in the main page is as follows:

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 ();} / / Show add form public void add () {frmAdd fadd = new frmAdd () Fadd.Show ();} / / display modified form public void edit () {frmEdit fedit = new frmEdit (); fedit.Show ();} / display query form public void seacher () {frmSeach fseacher=new frmSeach (); fseacher.Show () } / / count the number of login contacts 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 } / / count the number of login contact groups 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;} / / load the treeview control 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 ();}} / / load ListView control public void loadList () {if (treeView1.Nodes.Count = = 0) {MessageBox.Show ("please reload") ListView1.Items.Clear (); return;} if (treeView1.SelectedNode.Index > = 0) {listView1.Clear (); listView1.Columns.Add (name, 100); listView1.Columns.Add (work unit, 100) ListView1.Columns.Add (contact telephone number, 100); listView1.Columns.Add (email address, 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 adds contact 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) {/ / accept login 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. Add contact Page

Tablcontrol control: paging options: used for paging: basic and other information

Double-click OK, inside the code:

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 ("add successful");} else {MessageBox.Show ("failed");}} catch (Exception ex) {MessageBox.Show (ex.Message) } finally {DBHelp.con.Close ();}}

Load the page, double-click the pressing surface, and the code inside

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. Modify the page

5. Query page

Double-click the query button, which contains the code:

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 () }} here, the article "how to realize the function of the form address book system in C#" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report