using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Threading.Tasks;namespace Tonxunlu
{public class publicclass{public static SqlConnection createconn(){//@"Data source=PC-20140124HPZO\SQLEXPRESS;User ID=sa;pwd=123456;Initial Catalog=DatabaseTonxunlu";//注意:Data source---指定sql sever數據庫(我用的是sql)所在計算機名稱或IP;(PC-20140124HPZO本計算機名+/SQLEXPRESS)// User ID,pwd---指定用戶名和數據庫的賬戶和密碼// Initial Catalog---指定連接的數據庫名稱(DatabaseTonxunlu)string connStr = @"Data source=PC-20140124HPZO\SQLEXPRESS;User ID=sa;pwd=123456;Initial Catalog=DatabaseTonxunlu";SqlConnection conn = new SqlConnection(connStr);return conn;}public static DataTable gettable(string sql){SqlConnection conn = createconn();SqlDataAdapter da = new SqlDataAdapter(sql, conn);DataTable dt = new DataTable();da.Fill(dt);return dt;}public static bool zhixingSQl(string sql){SqlConnection conn1 = createconn();SqlCommand cmd = new SqlCommand(sql, conn1);if (conn1.State != ConnectionState.Open){conn1.Open();}if (cmd.ExecuteNonQuery() > 0){return true;}else{return false;}conn1.Close();}public static bool checkUser(string userName, string passWord){string sql = "select 密碼 from users where 用戶名= '" + userName + "'";DataTable dt = gettable(sql);if (passWord == dt.Rows[0]["密碼"].ToString().Trim()){return true;}else{return false;}}}
}
2.welcome界面源代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace Tonxunlu
{public partial class welcome : Form{public welcome(){InitializeComponent();}private void Welcome_Load(object sender, EventArgs e){}private void button1_Click(object sender, EventArgs e){login lo = new login();lo.Show();}private void button2_Click(object sender, EventArgs e){login1 lo1 = new login1();lo1.Show();}private void closenow_Click(object sender, EventArgs e){this.Close();}}
}
3.login界面源代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace Tonxunlu
{public partial class login : Form{public login(){InitializeComponent();}private void login_Load(object sender, EventArgs e){}private void buttonok_Click(object sender, EventArgs e){if(textBoxname.Text.Length != 0 && textBoxpwd.Text.Length != 0){if(publicclass.checkUser(textBoxname.Text.Trim(), textBoxpwd.Text.Trim())){main?formMain = new?main();formMain.Show();}else{MessageBox.Show("用戶名或密碼不正確", "提示");}}else{MessageBox.Show("用戶名或密碼不能為空", "提示");}}private void buttoncancel_Click(object sender, EventArgs e){this.Close();}}
}
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;namespace Tonxunlu
{public partial class main : Form{public main(){InitializeComponent();}private void main_Load(object sender, EventArgs e){DataTable dt = publicclass.gettable("select * from 聯系人表");dataGridView1.DataSource = dt;}private void buttoninsert_Click(object sender, EventArgs e){insert forminsert = new insert();forminsert.Show();}private void buttondelete_Click(object sender, EventArgs e){string?name;int?index = dataGridView1.CurrentRow.Index;name = dataGridView1["姓名", index].Value.ToString().Trim();string?sql = "delete 聯系人表 where 姓名='"?+ name + "'";if?(MessageBox.Show("是否要刪除", "提示", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes){if?(publicclass.zhixingSQl(sql)){MessageBox.Show("刪除成功|");group?formgroup1 = new?group();formgroup1.Show();}else{MessageBox.Show("刪除失敗");}}}private void buttonchange_Click(object sender, EventArgs e){string?name;int?index = dataGridView1.CurrentRow.Index;name = dataGridView1["姓名", index].Value.ToString().Trim();modify?formmodify = new?modify(name);formmodify.Show();}private void buttonaddgroup_Click(object sender, EventArgs e){group formgroup = new group();formgroup.Show();}private void buttoninquiry_Click(object sender, EventArgs e){string?sql;if?(textBoxname.Text.Length != 0){sql = "select * from 聯系人表 where 姓名= '"?+ textBoxname.Text.Trim() + "'";}else{sql = "select * from 聯系人表";MessageBox.Show("請輸入姓名!", "提示");}DataTable?dt = publicclass.gettable(sql);dataGridView1.DataSource = dt;}private void buttonrefresh_Click(object sender, EventArgs e){main_Load(null, null);}}
}
6.main1界面源代碼
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;namespace Tonxunlu
{public partial class main1 : Form{public main1(){InitializeComponent();}private void main1_Load(object sender, EventArgs e){DataTable dt = publicclass.gettable("select * from 分組信息");dataGridView1.DataSource = dt;}private void buttoninquiry_Click(object sender, EventArgs e){string?sql;if?(textBox1.Text.Length != 0){sql = "select 聯系人表.*,分組信息.分組名稱,分組信息.工作地點 from 聯系人表,分組信息 where 分組信息.分組名稱='" + textBox1.Text.Trim() + "' and 聯系人表.姓名=分組信息.姓名";}else{sql = "select * from 分組信息";MessageBox.Show("請輸入分組名稱!", "提示");}DataTable?dt = publicclass.gettable(sql);dataGridView1.DataSource = dt;}}
}
7.insert界面源代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tonxunlu;namespace Tonxunlu
{public partial class insert : Form{public insert(){InitializeComponent();}private void button2_Click(object sender, EventArgs e){this.Close();}private void insert_Load(object sender, EventArgs e){try{comboBox1.Items.Add("man");comboBox1.Items.Add("woman");}catch (Exception ex){MessageBox.Show(ex.Message);}}private void in_insert_Click_1(object sender, EventArgs e){if (in_name.Text.Length != 0 && in_phone.Text.Length != 0 &&in_email.Text.Length != 0 && in_datatime.Text.Length != 0 &&comboBox1.SelectedItem != null){string sql = "insert into 聯系人表 (姓名,性別,出生日期,email,電話號碼)values('" +in_name.Text.Trim() + "','" + comboBox1.SelectedItem.ToString() + "','" + in_datatime.Text.Trim() + "','" + in_email.Text.Trim() + "','" + in_phone.Text.Trim() + "')";if (publicclass.zhixingSQl(sql)){MessageBox.Show("插入成功");this.Close();}else{MessageBox.Show("插入失敗");}}else{MessageBox.Show("請完整填寫信息");}}private void button2_Click_1(object sender, EventArgs e){this.Close();}}
}