}
//*********************************************************************************************test
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace SqlTest
{
class sqlServerTest
{
static void Main(string[] args)
{
string connString = @"server=.;database=MyfirstDatabase;Trusted_Connection=SSPI;";
string commString = "select * from [dbo].[LoginTest];";
string commString1 = "insert into [dbo].[LoginTest] (userName,password,lastLoginTime) values('狮子','1235','2016-01-01 00:00:00.123');";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = connString;
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = commString1;
conn.Open();
int count = cmd.ExecuteNonQuery();
Console.WriteLine("{0}受影响", count);
Console.ReadKey();
}
}
}
//*****************************************************************************************************************实例登录form.cs
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;
using System.Data.SqlClient;
using SQLHelp;
namespace StudentSystem
{
public partial class FormLoginIn : Form
{
public FormLoginIn()
{
InitializeComponent();
}
private void LoginIn_Click(object sender, EventArgs e)
{
string uID = textBoxUserID.Text.Trim();
string password = textBoxPassword.Text;
if (string.IsNullOrEmpty(uID) || string.IsNullOrEmpty(password)) return;
string cmdString = "select count(*) from stuDB where userName=@uid and password=@pwd;";
SqlParameter[] ps = { new SqlParameter("@uid", uID), new SqlParameter("@pwd", password) };
int count = (int)SQLCMDFunction.ExecuteScalar(cmdString, ps);
if(count>0)
{
MessageBox.Show("登录成功");
this.DialogResult = DialogResult.OK;
}
else
{
MessageBox.Show("登录失败");
this.DialogResult = DialogResult.None;
}
}
}
}
//*****************************************************************************************************************************实例主界面
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 SQLHelp;
namespace StudentSystem
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void FormMain_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
dgView.DataSource = SQLCMDFunction.GetDataSet("select * from sds;").Tables[0];
}
}
}
//************************************************************************************************************************************登录参数类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LoginTest
{
public static class Parameter
{
private static string connectionString = @"server=.;database=MyfirstDatabase;Trusted_Connection=SSPI;";
public static string ConnectionString
{
set { connectionString = value; }
get { return connectionString; }
}
}
}
//*********************************************************************************************************************************注册,取消,登录
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 LoginTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
string uid = uidText.Text.Trim();
string pwd1 = pwdText1.Text;
string pwd2 = pwdText2.Text;
if (string.IsNullOrEmpty(uid) || string.IsNullOrEmpty(pwd1) || string.IsNullOrEmpty(pwd2))
{
MessageBox.Show("请输入完整信息");
return;
}
if (pwd1 != pwd2)
{ MessageBox.Show("请输入一致"); }
string conString = @"server=.;database=MyfirstDatabase;Trusted_Connection=SSPI;";
string cmdString = string.Format("insert into [dbo].[LoginTest] (userName,password) values('{0}','{1}');",uid,pwd1);
string cmdString2 = "insert into [dbo].[LoginTest] (userName,password) values('大想','1235');";
uidText_Leave(sender, e);
try
{
if (label4.Text == "√")
{
using (SqlConnection conn = new SqlConnection(conString))
{
using (SqlCommand comm = new SqlCommand(cmdString, conn))
{
conn.Open();
int count = comm.ExecuteNonQuery();
MessageBox.Show(count > 0 ? "注册成功" : "注册失败");
conn.Close();
}
}
}
else
{
MessageBox.Show("注册名非法");
}
}
catch(Exception ex)
{ MessageBox.Show(ex.ToString()); }
}
private void uidText_Leave(object sender, EventArgs e)
{
string uid = uidText.Text.Trim();
if (string.IsNullOrEmpty(uid)) return;
string conString = @"server=.;database=MyfirstDatabase;Trusted_Connection=SSPI;";
string cmdString = "select count(*) from LoginTest where userName='" + uid + "'";
int count;
using(SqlConnection conn=new SqlConnection(conString))
{
using(SqlCommand comm=new SqlCommand(cmdString,conn))
{
conn.Open();
count = (int)comm.ExecuteScalar();
conn.Close();
}
}
if(count>0)
{
label4.ForeColor = Color.Red;
label4.Text = "用户已存在";
}
else
{
label4.ForeColor = Color.Green;
label4.Text = "√";
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(uidText.Text))
{
label4.Text = null;
}
}
private void butLgin_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
}
}
//***************************************************************************************************登录
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 LoginTest
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void butLogin_Click(object sender, EventArgs e)
{
string uid=uidText.Text.Trim();
string pwd=pwdText.Text;
string cmd="select count(*) from LoginTest where userName='"+uid+"' and password='"+pwd+"';";
int count;
if(string.IsNullOrEmpty(uid)||string.IsNullOrEmpty(pwd))
{
MessageBox.Show("用户名或密码不能为空");
return;
}
using(SqlConnection conn=new SqlConnection(Parameter.ConnectionString))
{
using(SqlCommand comm=new SqlCommand(cmd,conn))
{
conn.Open();
count = (int)comm.ExecuteScalar();
conn.Close();
}
}
if(count>0)
{
MessageBox.Show("登陆成功");
Form3 form3 = new Form3();
form3.Show();
}
else
{
MessageBox.Show("登陆失败,用户名或密码错误");
}
}
}
}
//********************************************************************************************************报告界面
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 LoginTest
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“MyfirstDataBaseDataSet.LoginTest”中。您可以根据需要移动或删除它。
this.LoginTestTableAdapter.Fill(this.MyfirstDataBaseDataSet.LoginTest);
this.reportViewer1.RefreshReport();
}
}
}