C#下WinForm编程:登录窗体的设计

以下是引用片段: 复制代码 代码如下:
ImportsSystem.Data ImportsSystem.Data.SqlClient PublicClassForm1
InheritsSystem.Windows.Forms.Form
PrivateConnectionStringAsString=”DataSource=.;InitialCatalog=;UserId=sa;Password=;”
PrivatereaderAsSqlDataReader=Nothing PrivateconnAsSqlConnection=Nothing
PrivatecmdAsSqlCommand=Nothing
PrivateAlterTableBtnAsSystem.Windows.Forms.Button
PrivatesqlAsString=Nothing
PrivateCreateOthersBtnAsSystem.Windows.Forms.Button
#Region”Windows窗体设计器生成的代码” ‘窗体重写处置以清理组件列表。
ProtectedOverloadsOverridesSubDispose(ByValdisposingAsBoolean)
IfdisposingThen IfNot(componentsIsNothing)Then components.Dispose()
EndIf EndIf MyBase.Dispose(disposing) EndSub PublicSubNew() MyBase.New()
InitializeComponent() EndSub
PrivatecomponentsAsSystem.ComponentModel.IContainer
FriendWithEventsDataGrid1AsSystem.Windows.Forms.DataGrid
FriendWithEventsCreateDBBtnAsSystem.Windows.Forms.Button
FriendWithEventsCreateTableBtnAsSystem.Windows.Forms.Button
FriendWithEventsCreateSPBtnAsSystem.Windows.Forms.Button
FriendWithEventsCreateViewBtnAsSystem.Windows.Forms.Button
FriendWithEventsbtnAlterTableAsSystem.Windows.Forms.Button
FriendWithEventsbtnCreateOthersAsSystem.Windows.Forms.Button
FriendWithEventsbtnDropTableAsSystem.Windows.Forms.Button
FriendWithEventsbtnViewDataAsSystem.Windows.Forms.Button
FriendWithEventsbtnViewSPAsSystem.Windows.Forms.Button
FriendWithEventsbtnViewViewAsSystem.Windows.Forms.Button
PrivateSubInitializeComponent() 复制代码
代码如下:Me.CreateDBBtn=NewSystem.Windows.Forms.Button()
Me.CreateTableBtn=NewSystem.Windows.Forms.Button()
Me.CreateSPBtn=NewSystem.Windows.Forms.Button()
Me.CreateViewBtn=NewSystem.Windows.Forms.Button()
Me.btnAlterTable=NewSystem.Windows.Forms.Button()
Me.btnCreateOthers=NewSystem.Windows.Forms.Button()
Me.btnDropTable=NewSystem.Windows.Forms.Button()
Me.btnViewData=NewSystem.Windows.Forms.Button()
Me.btnViewSP=NewSystem.Windows.Forms.Button()
Me.btnViewView=NewSystem.Windows.Forms.Button()
Me.DataGrid1=NewSystem.Windows.Forms.DataGrid()
CType(Me.DataGrid1,System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout() ‘ ‘CreateDBBtn ‘
Me.CreateDBBtn.Location=NewSystem.Drawing.Point(19,9)
Me.CreateDBBtn.Name=”CreateDBBtn”
Me.CreateDBBtn.Size=NewSystem.Drawing.Size(104,23)
Me.CreateDBBtn.TabIndex=0 Me.CreateDBBtn.Text=”创建数据库” ‘
‘CreateTableBtn ‘
Me.CreateTableBtn.Location=NewSystem.Drawing.Point(139,9)
Me.CreateTableBtn.Name=”CreateTableBtn” Me.CreateTableBtn.TabIndex=1
Me.CreateTableBtn.Text=”创建表” ‘ ‘CreateSPBtn ‘
Me.CreateSPBtn.Location=NewSystem.Drawing.Point(230,9)
Me.CreateSPBtn.Name=”CreateSPBtn”
Me.CreateSPBtn.Size=NewSystem.Drawing.Size(104,23)
Me.CreateSPBtn.TabIndex=2 Me.CreateSPBtn.Text=”创建存储过程” ‘
‘CreateViewBtn ‘
Me.CreateViewBtn.Location=NewSystem.Drawing.Point(350,9)
Me.CreateViewBtn.Name=”CreateViewBtn”
Me.CreateViewBtn.TabIndex=3中国网管联盟 Me.CreateViewBtn.Text=”创建视图”
‘ ‘btnAlterTable ‘
Me.btnAlterTable.Location=NewSystem.Drawing.Point(441,9)
Me.btnAlterTable.Name=”btnAlterTable” Me.btnAlterTable.TabIndex=4
Me.btnAlterTable.Text=”修改表” #p# 复制代码 代码如下: ‘btnCreateOthers ‘
Me.btnCreateOthers.Location=NewSystem.Drawing.Point(17,43)
Me.btnCreateOthers.Name=”btnCreateOthers”
Me.btnCreateOthers.Size=NewSystem.Drawing.Size(104,23)
Me.btnCreateOthers.TabIndex=5 Me.btnCreateOthers.Text=”创建规则和索引”
feedom.net ‘ ‘btnDropTable ‘
Me.btnDropTable.Location=NewSystem.Drawing.Point(138,43)
Me.btnDropTable.Name=”btnDropTable” Me.btnDropTable.TabIndex=6
Me.btnDropTable.Text=”删除表” ‘ ‘btnViewData ‘
Me.btnViewData.Location=NewSystem.Drawing.Point(351,43)
Me.btnViewData.Name=”btnViewData” Me.btnViewData.TabIndex=7
Me.btnViewData.Text=”查看数据” ‘ ‘btnViewSP ‘
Me.btnViewSP.Location=NewSystem.Drawing.Point(230,43) feedom.net
Me.btnViewSP.Name=”btnViewSP”
Me.btnViewSP.Size=NewSystem.Drawing.Size(104,23) Me.btnViewSP.TabIndex=8
Me.btnViewSP.Text=”查看存储过程” ‘ ‘btnViewView ‘
Me.btnViewView.Location=NewSystem.Drawing.Point(443,43)
Me.btnViewView.Name=”btnViewView” Me.btnViewView.TabIndex=9
Me.btnViewView.Text=”查看视图” ‘ ‘DataGrid1 ‘ Me.DataGrid1.DataMember=””
Me.DataGrid1.HeaderForeColor=System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location=NewSystem.Drawing.Point(20,76)54com.cn
Me.DataGrid1.Name=”DataGrid1″
Me.DataGrid1.Size=NewSystem.Drawing.Size(500,183)
Me.DataGrid1.TabIndex=10 ‘ ‘Form1 ‘
Me.AutoScaleBaseSize=NewSystem.Drawing.Size(5,13)
Me.ClientSize=NewSystem.Drawing.Size(538,281)
Me.Controls.AddRange(NewSystem.Windows.Forms.Control(){Me.DataGrid1,Me.btnViewView,_
Me.btnViewSP,Me.btnViewData,Me.btnDropTable,Me.btnCreateOthers,Me.btnAlterTable,_
复制代码 代码如下:
Me.CreateViewBtn,Me.CreateSPBtn,Me.CreateTableBtn,Me.CreateDBBtn})
Me.Name=”Form1″
Me.Text=”动态创建SQLServer数据库、表、存储过程等架构信息”
CType(Me.DataGrid1,System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False) EndSub #EndRegion ‘创建数据库
PrivateSubCreateDBBtn_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)_
HandlesCreateDBBtn.Click conn=NewSqlConnection(ConnectionString)
‘打开连接 Ifconn.StateConnectionState.OpenThen conn.Open() EndIf
‘MyDataBase为数据库名称
DimsqlAsString=”CREATEDATABASEMyDataBaseONPRIMARY(Name=MyDataBase_data,filename=”+_
54com.cn
“‘D:\MyDataBase.mdf’,size=3,”+”maxsize=5,filegrowth=10%)logon”+”(name=MyDataBase_log,”+_
“filename=’D:\MyDataBase.ldf’,size=3,”+”maxsize=20,filegrowth=1)”
cmd=NewSqlCommand(sql,conn) Try cmd.ExecuteNonQuery()
CatchaeAsSqlException MessageBox.Show(ae.Message.ToString()) EndTry
EndSub ‘创建表
PrivateSubCreateTableBtn_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)_
HandlesCreateTableBtn.Click conn=NewSqlConnection(ConnectionString)
‘打开连接 Ifconn.State=ConnectionState.OpenThen conn.Close() EndIf
ConnectionString=”DataSource=.;InitialCatalog=MyDataBase;UserId=sa;Password=;”
conn.ConnectionString=ConnectionString conn.Open()
sql=”CREATETABLEmyTable”+”(myIdINTEGERCONSTRAINTPKeyMyIdPRIMARYKEY,”+_
“myNameCHAR(50)NOTNull,myAddressCHAR(255),myValuesFLOAT)”
cmd=NewSqlCommand(sql,conn) Try cmd.ExecuteNonQuery() ‘添加纪录 54com.cn
sql=”INSERTINTOmyTable(myId,myName,myAddress,myValues)”+_
“VALUES(1001,_’之一’,”,100)” cmd=NewSqlCommand(sql,conn)
cmd.ExecuteNonQuery()
sql=”INSERTINTOmyTable(myId,myName,myAddress,myValues)”+_
“VALUES(1002,’之二’,’_lover/’,99)” #p# 复制代码 代码如下: cmd=NewSqlCommand(sql,conn)
cmd.ExecuteNonQuery()
sql=”INSERTINTOmyTable(myId,myName,myAddress,myValues)”+_
“VALUES(1003,’之三’,”,99)” cmd=NewSqlCommand(sql,conn)
cmd.ExecuteNonQuery()
sql=”INSERTINTOmyTable(myId,myName,myAddress,myValues)”+_
“VALUES(1004,’之四’,’_lover/’,100)” cmd=NewSqlCommand(sql,conn)
cmd.ExecuteNonQuery() CatchaeAsSqlException
MessageBox.Show(ae.Message.ToString()) EndTry EndSub ‘创建存储过程
PrivateSubCreateSPBtn_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)_
HandlesCreateSPBtn.Click
sql=”CREATEPROCEDUREmyProcAS”+”SELECTmyName,myAddressFROMmyTableGO”
ExecuteSQLStmt(sql) EndSub ‘创建视图
PrivateSubCreateViewBtn_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)_
HandlesCreateViewBtn.Click
sql=”CREATEVIEWmyViewASSELECTmyNameFROMmyTable” ExecuteSQLStmt(sql)
EndSub ‘修改表
PrivateSubbtnAlterTable_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)_
54com.cn HandlesbtnAlterTable.Click
sql=”ALTERTABLEMyTableADDnewColdatetimeNOTNULLDEFAULT(getdate())”
ExecuteSQLStmt(sql) EndSub ‘创建规则和索引
PrivateSubbtnCreateOthers_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)_
HandlesbtnCreateOthers.Click
sql=”CREATEUNIQUEINDEX”+”myIdxONmyTable(myName)” ExecuteSQLStmt(sql)
sql=”CREATERULEmyRule”+”AS@myValues=90AND@myValues9999″
ExecuteSQLStmt(sql) EndSub ‘删除表
PrivateSubbtnDropTable_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)_
HandlesbtnDropTable.Click DimsqlAsString=”DROPTABLEMyTable”
ExecuteSQLStmt(sql) EndSub 复制代码
代码如下: ‘浏览表数据
PrivateSubbtnViewData_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)_
HandlesbtnViewData.Click conn=NewSqlConnection(ConnectionString)
Ifconn.State=ConnectionState.OpenThen conn.Close() EndIf
ConnectionString=”DataSource=.;InitialCatalog=MyDataBase;UserId=sa;Password=;”
conn.ConnectionString=ConnectionString conn.Open()
DimdaAsNewSqlDataAdapter(“SELECT*FROMmyTable”,conn)
DimdsAsNewDataSet(“myTable”) da.Fill(ds,”myTable”)
DataGrid1.DataSource=ds.Tables(“myTable”).DefaultView EndSub
‘浏览存储过程
PrivateSubbtnViewSP_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)_
HandlesbtnViewSP.Click conn=NewSqlConnection(ConnectionString)
Ifconn.State=ConnectionState.OpenThen conn.Close() EndIf
ConnectionString=”DataSource=.;InitialCatalog=MyDataBase;UserId=sa;Password=;”
conn.ConnectionString=ConnectionString conn.Open()
DimdaAsNewSqlDataAdapter(“myProc”,conn) DimdsAsNewDataSet(“SP”)
da.Fill(ds,”SP”) DataGrid1.DataSource=ds.DefaultViewManager EndSub
‘浏览视图
PrivateSubbtnViewView_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)_
HandlesbtnViewView.Click conn=NewSqlConnection(ConnectionString)
Ifconn.State=ConnectionState.OpenThen conn.Close() EndIf
ConnectionString=”DataSource=.;InitialCatalog=MyDataBase;UserId=sa;Password=;”
conn.ConnectionString=ConnectionString conn.Open()
DimdaAsNewSqlDataAdapter(“SELECT*FROMmyView”,conn) DimdsAsNewDataSet()
da.Fill(ds) DataGrid1.DataSource=ds.DefaultViewManager EndSub
PrivateSubExecuteSQLStmt(ByValsqlAsString)
conn=NewSqlConnection(ConnectionString) ‘打开连接
Ifconn.State=ConnectionState.OpenThen conn.Close() EndIf
ConnectionString=”DataSource=.;InitialCatalog=MyDataBase;UserId=sa;Password=;”
conn.ConnectionString=ConnectionString conn.Open()
cmd=NewSqlCommand(sql,conn) Try cmd.ExecuteNonQuery()
CatchaeAsSqlException MessageBox.Show(ae.Message.ToString()) EndTry
EndSub EndClass

我在csdn里搜索了很久,也没有找到符合我要求的login文档,我这次把自己的心得和自己做的成果拿出来和大家分享一下,希望对后来的人能有一些帮助。我初次做,可能代码写的不是很规范,思路也不是很清晰,但是它能达到我要的效果就行了’     
希望哪位兄弟帮忙完善一下我的代码。

我在数据库里有一个   users  的表,如下:

ID     UserName           UserPasswd

1        admin                    admin

2        user                        user

3        guest                     guest

我准备这样做,先判断输入的用户名是否和表里的UserName相同,如果相同,再比较相同UserName下的UserPasswd,如果这些都正确了,就可以进入系统了。

全部代码如下(我把我写的部分用黑体):

Form1的代码:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace login
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.TextBox txtUser;
  private System.Windows.Forms.TextBox txtPasswd;
  private System.Windows.Forms.Button btnOK;
  private System.Windows.Forms.Button btnCancel;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

  
   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 – 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.label1 = new System.Windows.Forms.Label();
   this.label2 = new System.Windows.Forms.Label();
   this.txtUser = new System.Windows.Forms.TextBox();
   this.txtPasswd = new System.Windows.Forms.TextBox();
   this.btnOK = new System.Windows.Forms.Button();
   this.btnCancel = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // label1
   //
   this.label1.Font = new System.Drawing.Font(“宋体”, 10.5F,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,
((System.Byte)(134)));
   this.label1.Location = new System.Drawing.Point(40, 24);
   this.label1.Name = “label1”;
   this.label1.Size = new System.Drawing.Size(64, 23);
   this.label1.TabIndex = 0;
   this.label1.Text = “用户名:”;
   //
   // label2
   //
   this.label2.Font = new System.Drawing.Font(“宋体”, 10.5F,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,
((System.Byte)(134)));
   this.label2.Location = new System.Drawing.Point(40, 72);
   this.label2.Name = “label2”;
   this.label2.Size = new System.Drawing.Size(56, 23);
   this.label2.TabIndex = 1;
   this.label2.Text = “密码:”;
   //
   // txtUser
   //
   this.txtUser.Location = new System.Drawing.Point(152, 24);
   this.txtUser.Name = “txtUser”;
   this.txtUser.TabIndex = 2;
   this.txtUser.Text = “”;
   //
   // txtPasswd
   //
   this.txtPasswd.Location = new System.Drawing.Point(152, 72);
   this.txtPasswd.Name = “txtPasswd”;
   this.txtPasswd.PasswordChar = ‘*’;
   this.txtPasswd.TabIndex = 3;
   this.txtPasswd.Text = “”;
   //
   // btnOK
   //
   this.btnOK.Location = new System.Drawing.Point(40, 120);
   this.btnOK.Name = “btnOK”;
   this.btnOK.Size = new System.Drawing.Size(72, 23);
   this.btnOK.TabIndex = 4;
   this.btnOK.Text = “OK”;
   this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
   //
   // btnCancel
   //
   this.btnCancel.DialogResult =
System.Windows.Forms.DialogResult.Cancel;
   this.btnCancel.Location = new System.Drawing.Point(176, 120);
   this.btnCancel.Name = “btnCancel”;
   this.btnCancel.TabIndex = 5;
   this.btnCancel.Text = “Cancel”;
   this.btnCancel.Click += new
System.EventHandler(this.btnCancel_Click);
   //
   // Form1
   //
   this.AcceptButton = this.btnOK;
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.CancelButton = this.btnCancel;
   this.ClientSize = new System.Drawing.Size(298, 167);
   this.Controls.Add(this.btnCancel);
   this.Controls.Add(this.btnOK);
   this.Controls.Add(this.txtPasswd);
   this.Controls.Add(this.txtUser);
   this.Controls.Add(this.label2);
   this.Controls.Add(this.label1);
   this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedDialog;
   this.MaximizeBox = false;
   this.MaximumSize = new System.Drawing.Size(304, 192);
   this.MinimizeBox = false;
   this.MinimumSize = new System.Drawing.Size(304, 192);
   this.Name = “Form1”;
   this.ShowInTaskbar = false;
   this.Text = “Login”;
   this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>

  private void btnCancel_Click(object sender, System.EventArgs e)
  {
   Application.Exit();
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
   this.SetDesktopLocation(280,180);

  
  }

发表评论

电子邮件地址不会被公开。 必填项已用*标注