一、完整一个功能的代码
1、获取本地信息的程序
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 System.Management;
namespace 数据库连接Test
{
public partial class 获取本地信息 : Form
{
public 获取本地信息()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textMac.Text = GetMACString();
textBroadSn.Text = GetBroadSn();
textCPUSn.Text = GetCpuSn();
textDisterSn.Text = GetDisterSn();
}
//获取本机的MAC地址
private string GetMACString()
{
ManagementClass mAdapter = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection mo = mAdapter.GetInstances();
foreach (ManagementBaseObject m in mo)
{
if ((bool)m["IpEnabled"] == true)
{
return m["MacAddress"].ToString();
}
}
mo.Dispose();
return "";
}
//获取CPU编号
public string GetCpuSn()
{
var cpuid = string.Empty;
var mc = new ManagementClass("Win32_Processor");
var moc = mc.GetInstances();
foreach (var o in moc)
{
var mo = (ManagementObject)o;
cpuid = (string)mo.Properties["ProcessorId"].Value;
//
}
return cpuid;
}
//获取硬盘序列号
public string GetDisterSn()
{
//这种模式在插入一个硬盘后可能会有不同的结果,如插入我的手机时
var hDid = string.Empty;
var mc = new ManagementClass("Win32_DiskDrive");
var moc = mc.GetInstances();
foreach (var o in moc)
{
var mo = (ManagementObject)o;
hDid = (string)mo.Properties["Model"].Value;
//这句话解决有多个物理盘时产生的问题,只取第一个物理硬盘
break;
}
return hDid;
}
//获取主板编号
public string GetBroadSn()
{
var st = string.Empty;
var mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard");
foreach (var o in mos.Get())
{
var mo = (ManagementObject)o;
st = mo["SerialNumber"].ToString();
}
return st;
}
}
}
2、数据库连接
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 System.Data.SqlClient; //导入这个包,实现连接数据库的关键
namespace 数据库连接Test
{
public partial class 数据库连接 : Form
{
public 数据库连接()
{
InitializeComponent();
this.IsMdiContainer = true;
}
private SqlConnection objconnect;
private void Form1_Load(object sender, EventArgs e)
{
objconnect = new SqlConnection("");
}
private void button1_Click(object sender, EventArgs e)
{
try
{
objconnect.Open();
MessageBox.Show("连接成功");
}
catch (Exception error)
{
MessageBox.Show(error.ToString());
}
}
}
}
//以下代码是测试打开不同窗体的写法。采用的是隐藏了前一个窗体,显示下一个窗体的后再关
闭第一个窗体 的写法。
//为了调出第二个form
this.Hide(); //调用Form1的Hide()方法隐藏Form1窗口
获取本地信息 form2 = new 获取本地信息(); //生成一个Form2对象,实例化即可使用
form2.ShowDialog(); //将Form2窗体显示为模式对话框。
this.Close(); //关闭From1窗体。
3、随机抓阄
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 数据库连接Test
{
public partial class RandomSelectting : Form
{
public RandomSelectting()
{
InitializeComponent();
}
private void textleft_TextChanged(object sender, EventArgs e)
{
numSel.Maximum = textleft.Lines.Length;
}
private void btngo_Click(object sender, EventArgs e)
{
textright.Text = "";//清空右侧已选的文本内容
int num = (int)numSel.Value;// 选择多少个数
string[] _left = textleft.Lines;
for (int i = 0; i < num; i++)
{
bool isAdd = false; //这个用于随机抽到相同的数判断是否添加到右侧
do
{
Random _r = new Random();//创建定义一个随机数类
int _sjs = _r.Next(0,_left.Length);//随机数 用了next()函数,长度范围
string _sel = _left[_sjs];
if(!_find(_sel,textright.Lines))
{
textright.AppendText(_sel+ Environment.NewLine);
isAdd = true;
}
}
while (!isAdd);
}
}
/// <summary>
/// 从字符串数组里面找某个字符串
/// </summary>
/// <param name="_f">要找的字符串</param>
/// <param name="_array">从这个数字里面找</param>
/// <returns></returns>
private bool _find(string _f,string[] _array)
{
foreach (string _s in _array)
{
if(_s == _f)
{
return true;
}
}
return false;
}
}
}
4、显示服务时间
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 System.Data.SqlClient;
namespace 数据库连接Test
{
public partial class GetServerTime : Form
{
DateTime _Sertime = DateTime.Parse("1900-01-01"); //定义一个服务器时间
public GetServerTime()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
labLocalTime.Text = String.Format(@"本机时间:{0}",DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
if (_Sertime == DateTime.Parse("1900-01-01")) //目的—减少对服务器多次获取时间的压力
{
//string strconn = @"Datebase=127.0.0.1;User Id=sa;password=@www123;Initial Catalog=1234;Integrated Security = SSPI"; //网页上安全连接——Integrated Security = SSPI
string strconn = @"server=127.0.0.1;uid=sa;pwd=@www123;database=1234";
SqlConnection SqlConnection = new SqlConnection(strconn);
SqlConnection.Open();
SqlCommand myComm = new SqlCommand();
myComm.Connection = SqlConnection;
myComm.CommandText = @"Select GETDATE()";
object o = myComm.ExecuteScalar(); //结果集返回第一行第一列
SqlConnection.Close();
//判断状态代码。不能在这里写,死循环了
//if (SqlConnection.State == ConnectionState.Open)
//{
// MessageBox.Show("连接成功");
//}
//else {
// MessageBox.Show("连接失败");
//}
_Sertime = (DateTime)o;
}
labServerTime.Text = String.Format(@"服务器时间:{0}", _Sertime.ToString("yyyy-MM-dd HH:mm:ss"));
}
}
}
5、体重指标
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 数据库连接Test
{
public partial class BodyMessIndex : Form
{
public BodyMessIndex()
{
InitializeComponent();
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnCount_Click(object sender, EventArgs e)
{
decimal _Weight = numWeight.Value, _Height = numHeight.Value;
bool isMan = radMan.Checked; //true为选择男,false为选择女
if(_Weight==0||_Height==0)
{
MessageBox.Show("身高体重不能够为空");
return;
}
decimal _BMI = _Weight / (_Height * _Height);
textBMI.Text = _BMI.ToString();
if(isMan)
{
//计算男性的
if (_BMI <20)
{
textDec.Text = "体重过轻";
}
else if (_BMI >=20&&_BMI < 25)
{
textDec.Text = "体重适中";
}
else if (_BMI >= 25 && _BMI < 30)
{
textDec.Text = "体重过重";
}
else if (_BMI >= 30 && _BMI < 35)
{
textDec.Text = "体重肥胖";
}
else if(_BMI >=35)
{
textDec.Text = "非常肥胖";
}
}
else
{
//计算女性的
if (_BMI < 19)
{
textDec.Text = "体重过轻";
}
else if (_BMI >= 19 && _BMI < 24)
{
textDec.Text = "体重适中";
}
else if (_BMI >= 25 && _BMI < 29)
{
textDec.Text = "体重过重";
}
else if (_BMI >= 29 && _BMI < 34)
{
textDec.Text = "体重肥胖";
}
else if (_BMI >= 34)
{
textDec.Text = "非常肥胖";
}
}
}
}
}