sql数据库实例(c#查询登录界面)
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace SQLHelp//數據庫操作help類
{
? ? public class SQLCMDFunction
? ? { ? //連接字符串
? ? ? ? public static string ?connString="server=.;database=MyfirstDataBase;Trusted_Connection=SSPI;";
? ? ? ? //非查詢操作
? ? ? ? #region
? ? ? ? public static int ?ExecuteNonquery(string cmdString, params SqlParameter[] ps )
? ? ? ? {
? ? ? ? using(SqlConnection conn=new SqlConnection( connString))
? ? ? ? {
? ? ? ? ? ? using(SqlCommand comm=new SqlCommand(cmdString,conn))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? comm.Parameters.AddRange(ps);
? ? ? ? ? ? ? ? conn.Open();
? ? ? ? ? ? ? ? return comm.ExecuteNonQuery();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? }
? ? ? ? #endregion
? ? ? ? //查詢操作
? ? ? ? #region
? ? ? ? public static object ExecuteScalar(string cmd,params SqlParameter[] ps)
? ? ? ? {
? ? ? ? ? ?using(SqlConnection conn=new SqlConnection(connString))
? ? ? ? ? ?{
? ? ? ? ? ? ? ?using(SqlCommand comm=new SqlCommand(cmd,conn))
? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ?comm.Parameters.AddRange(ps);
? ? ? ? ? ? ? ? ? ?conn.Open();
? ? ? ? ? ? ? ? ? return ?comm.ExecuteScalar();
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ? }
? ? ? ? #endregion
? ? ? ? //讀操作
? ? ? ? #region
? ? ? ? public static SqlDataReader ExecuteReader(string cmd,params SqlParameter[] ps)
? ? ? ? {
? ? ? ? ? ? SqlConnection conn = new SqlConnection(connString);
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? using (SqlCommand comm = new SqlCommand(cmd, conn))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? comm.Parameters.AddRange(ps);
? ? ? ? ? ? ? ? ? ? conn.Open();
? ? ? ? ? ? ? ? ? ? return comm.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? catch(Exception ep)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? conn.Dispose();
? ? ? ? ? ? ? ? throw ep;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? #endregion
? ? ? ? //DataBase適配器
? ? ? ? #region
? ? ? ? public static System.Data.DataSet GetDataSet(string cmd,params SqlParameter[] ps)
? ? ? ? {
? ? ? ? ? ? DataSet da = new DataSet();
? ? ? ? ? ? using(SqlDataAdapter sda=new SqlDataAdapter(cmd,connString))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? sda.SelectCommand.Parameters.AddRange(ps);
? ? ? ? ? ? ? ? sda.Fill(da);
? ? ? ? ? ? ? ??
? ? ? ? ? ? }
? ? ? ? ? ? return da;
? ? ? ? }
? ? ? ? #endregion
? ? }
}
//*********************************************************************************************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();
? ? ? ? }
? ? }
}
//*******************************************************************************sql內部命令
use MyfirstDataBase;
Create table LoginTest2
(
userName nvarchar(20) not null
, [password] varchar(20) not null
, lastLoginDateTime datetime not null
);
use MyfirstDataBase;
insert into LoginTest(userName,password,lastLoginTime)
values('趙建虎','qwe','2016-06-06 00:00:00.000');
use master;
use MyfirstDataBase;
select userName,password,lastLoginTime from LoginTest;
use MyfirstDataBase;
delete from LoginTest where userName='趙建虎';
use MyfirstDataBase;
update LoginTest set userName='牛亮亮' where userName='趙小虎';
use MyfirstDataBase;
Create table sds
(
ID int identity(1,1) constraint PK_sds_ID primary key,
stuName nvarchar(20) not null,
stuSex char(1) ?null,
stuBirthday datetime null,
isdel bit default(0),
inputTime datetime default(current_timestamp)
)
insert into sds (stuName,stuSex,stuBirthday) values('王輝輝','m','1990-11-8');
總結
以上是生活随笔為你收集整理的sql数据库实例(c#查询登录界面)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《那些年啊,那些事——一个程序员的奋斗史
- 下一篇: C#图像处理基础概念知识