說說我現(xiàn)在吧,樓主現(xiàn)在從事的事IT行業(yè),主攻DotNet技術(shù);當(dāng)然這次上博客園我也是有備而來,所有再次奉獻(xiàn)鄙人拙作,以饗諸位,望諸位不吝賜教。
世界上大多數(shù)的工作都是熟練性的工種,編程也不例外,做久了,做多了,自然也就通了!
作為一個程序員,要具有一個程序化的思維,這種思維不是三五兩天就能一蹴而就的,它是一個不斷累積的過程,就如庖丁解牛一樣,做事不僅要掌握規(guī)律,還要持著一種謹(jǐn)慎小心的態(tài)度,收斂鋒芒,并且在懂得利用規(guī)律的同時,更要去反復(fù)實踐,向庖丁“所解數(shù)千牛矣”一樣,不停地重復(fù),終究會悟出事物的真理所在。所以作為一個初級程序員就更需要通過大量的代碼練習(xí)來積累自己的代碼量。
好了,閑話不多說了。直接進(jìn)入我們今天的主題————asp.net (C#), 利用SQL Server實現(xiàn)注冊和登陸功能!
首先看到題目就要理清思路,第一步做什么,第二步又要做什么,他們之間有何內(nèi)在聯(lián)系。
步驟 :
第一步,我們利用SQl語言建立數(shù)據(jù)庫RegistLogin、數(shù)據(jù)表user、以及創(chuàng)建好約束關(guān)系以及插入測試字段(這步簡單,就省略過程了)
第二部,我們就打開VS連接到數(shù)據(jù)庫,操作流程見圖:
首先我們VS菜單節(jié)面找到“視圖”單擊“服務(wù)器資源管理器”如圖:
單擊“服務(wù)器資源管理器”后出現(xiàn)如同界面:
接著出現(xiàn):
到這一步我們就完成了數(shù)據(jù)庫的連接了 效果圖:
接下來,我們就要在VS里進(jìn)行注冊操作了,注冊操作無非就是往數(shù)據(jù)庫里插入數(shù)據(jù),所以我們創(chuàng)建一個窗應(yīng)用程序體,添加相應(yīng)的控件
插入數(shù)據(jù)代碼
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 數(shù)據(jù)庫驗證{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } PRivate void btnZhuCe_Click(object sender, EventArgs e) { string Sql = "Data Source=.;Initial Catalog=RegistLogin;User ID=sa;PassWord=123"; using (SqlConnection scon = new SqlConnection(Sql)) { string str = "insert into [User](userName,userPwd ,userEmail ,userPhone ) values('" + txtUserName.Text.Trim() + "','" + txtPwd.Text.Trim() + "','" + txtPhone.Text.Trim() + "','" + txtEmail.Text.Trim() + "')"; scon.Open(); SqlCommand command = new SqlCommand(); command.Connection = scon; command.CommandText = str; int obj=command .ExecuteNonQuery(); MessageBox.Show("注冊成功"); Form2 f = new Form2(); f.Show(); } } }}
再在同一個解決方案里創(chuàng)建登陸窗體:
登陸操作 無非就是檢索數(shù)據(jù)庫里的數(shù)據(jù)是否存在,代碼如下:
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 數(shù)據(jù)庫驗證{ public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void btnLogin_Click(object sender, EventArgs e) { SqlCommand sqlcmd = new SqlCommand(); string str="select*from user where userName='" + txtAdmin .Text .Trim () + "' and userPwd='" + txtPwd .Text .Trim () + "'"; sqlcmd.CommandText = str; //執(zhí)行數(shù)據(jù) SqlDataReader sqlRead = sqlcmd.ExecuteReader();//讀取數(shù)據(jù) if (sqlRead.Read()) { MessageBox.Show("登陸成功!"); } else { MessageBox.Show("用戶名或密碼錯誤!"); } } }}
以上就是所有的流程,如有紕漏,還望大家多多指教!
謝謝!
新聞熱點
疑難解答
圖片精選