說說我現在吧,樓主現在從事的事IT行業,主攻DotNet技術;當然這次上博客園我也是有備而來,所有再次奉獻鄙人拙作,以饗諸位,望諸位不吝賜教。
世界上大多數的工作都是熟練性的工種,編程也不例外,做久了,做多了,自然也就通了!
作為一個程序員,要具有一個程序化的思維,這種思維不是三五兩天就能一蹴而就的,它是一個不斷累積的過程,就如庖丁解牛一樣,做事不僅要掌握規律,還要持着一種謹慎小心的態度,收斂鋒芒,並且在懂得利用規律的同時,更要去反復實踐,向庖丁“所解數千牛矣”一樣,不停地重復,終究會悟出事物的真理所在。所以作為一個初級程序員就更需要通過大量的代碼練習來積累自己的代碼量。
好了,閑話不多說了。直接進入我們今天的主題————asp.net (C#), 利用SQL Server實現注冊和登陸功能!
首先看到題目就要理清思路,第一步做什么,第二步又要做什么,他們之間有何內在聯系。
步驟 :
第一步,我們利用SQl語言建立數據庫RegistLogin、數據表user、以及創建好約束關系以及插入測試字段(這步簡單,就省略過程了)
第二部,我們就打開VS連接到數據庫,操作流程見圖:
首先我們VS菜單節面找到“視圖”單擊“服務器資源管理器”如圖:
單擊“服務器資源管理器”后出現如同界面:
接着出現:
到這一步我們就完成了數據庫的連接了 效果圖:
接下來,我們就要在VS里進行注冊操作了,注冊操作無非就是往數據庫里插入數據,所以我們創建一個窗應用程序體,添加相應的控件
插入數據代碼
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 數據庫驗證 { 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(); } } } }
再在同一個解決方案里創建登陸窗體:
登陸操作 無非就是檢索數據庫里的數據是否存在,代碼如下:
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 數據庫驗證 { 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; //執行數據
SqlDataReader sqlRead = sqlcmd.ExecuteReader();//讀取數據
if (sqlRead.Read()) { MessageBox.Show("登陸成功!"); } else { MessageBox.Show("用戶名或密碼錯誤!"); } } } }
以上就是所有的流程,如有紕漏,還望大家多多指教!
謝謝!