VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > Objective-C编程 >
  • C#运行SQL语句实例2

实战练习二:“密码修改模块示例代码:

“密码修改”窗体详细设置(在InitializeComponent();处击右键,选择“转到定义”获取的代码)
private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.txt_OldPwd = new System.Windows.Forms.TextBox();
            this.txt_NewPwd = new System.Windows.Forms.TextBox();
            this.txt_ConfirmPwd = new System.Windows.Forms.TextBox();
            this.btn_OK = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(17, 15);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(53, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "旧密码:";
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(17, 51);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(53, 12);
            this.label2.TabIndex = 1;
            this.label2.Text = "新密码:";
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(17, 87);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(65, 12);
            this.label3.TabIndex = 2;
            this.label3.Text = "密码确认:";
            //
            // txt_OldPwd
            //
            this.txt_OldPwd.Location = new System.Drawing.Point(99, 12);
            this.txt_OldPwd.Name = "txt_OldPwd";
            this.txt_OldPwd.PasswordChar = '*';
            this.txt_OldPwd.Size = new System.Drawing.Size(100, 21);
            this.txt_OldPwd.TabIndex = 3;
            //
            // txt_NewPwd
            //
            this.txt_NewPwd.Location = new System.Drawing.Point(99, 48);
            this.txt_NewPwd.Name = "txt_NewPwd";
            this.txt_NewPwd.PasswordChar = '*';
            this.txt_NewPwd.Size = new System.Drawing.Size(100, 21);
            this.txt_NewPwd.TabIndex = 4;
            //
            // txt_ConfirmPwd
            //
            this.txt_ConfirmPwd.Location = new System.Drawing.Point(99, 84);
            this.txt_ConfirmPwd.Name = "txt_ConfirmPwd";
            this.txt_ConfirmPwd.PasswordChar = '*';
            this.txt_ConfirmPwd.Size = new System.Drawing.Size(100, 21);
            this.txt_ConfirmPwd.TabIndex = 5;
            //
            // btn_OK
            //
            this.btn_OK.Location = new System.Drawing.Point(124, 121);
            this.btn_OK.Name = "btn_OK";
            this.btn_OK.Size = new System.Drawing.Size(75, 23);
            this.btn_OK.TabIndex = 6;
            this.btn_OK.Text = "确定";
            this.btn_OK.UseVisualStyleBackColor = true;
            this.btn_OK.Click += new System.EventHandler(this.btn_OK_Click);
            //
            // Form2
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(222, 153);
            this.Controls.Add(this.btn_OK);
            this.Controls.Add(this.txt_ConfirmPwd);
            this.Controls.Add(this.txt_NewPwd);
            this.Controls.Add(this.txt_OldPwd);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Name = "Form2";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "密码修改";
            this.Load += new System.EventHandler(this.Form2_Load);
            this.ResumeLayout(false);
            this.PerformLayout();
        }
 
“确定”按钮源码:
private void btn_OK_Click(object sender, EventArgs e)
        {
 
            if (txt_OldPwd.Text == "")
            {
                MessageBox.Show("请输入旧密码","系统提示");
                txt_OldPwd.Focus();
            }
            else if (txt_NewPwd.Text == "")
            {
                MessageBox.Show("请输入新密码", "系统提示");
                txt_NewPwd.Focus();
            }
            else if (txt_ConfirmPwd.Text == "")
            {
                MessageBox.Show("请输入确认密码", "系统提示");
                txt_ConfirmPwd.Focus();
            }
            else if (txt_ConfirmPwd.Text != txt_NewPwd.Text)
            {
                MessageBox.Show("确认密码必须和新密码相同!", "系统提示");
                txt_ConfirmPwd.Focus();
            }
            else
            {
               
                //定义连接字符串              
                string strConnection = "Server=168.10.10.86;initial catalog=Tele_Sale;user id=sa;password=sd;Connect Timeout=30";
 
                //用连接字符串创建一个数据库连接对象
                SqlConnection objConnection = new SqlConnection(strConnection);
 
                //使用数据库连接对象连接数据库
                objConnection.Open();
 
                //先判断旧密码是否正确
                SqlCommand SqlCmd_Select = new SqlCommand("select PassWord from MissInfo where MissID='"+MissID+"'", objConnection);
                string TempPass = SqlCmd_Select.ExecuteScalar().ToString();
 
                // MessageBox.Show(TempPass, "");
 
                if (TempPass != txt_OldPwd.Text)
                {
                    MessageBox.Show("对不起,您输入的旧密码不正确!", "系统提示");
                    txt_OldPwd.Focus();
                }
                else
                {
 
                    //基于数据库连接对象创建一个sql命令
                    SqlCommand SqlCmd = new SqlCommand("update MissInfo set MissName='技术主管' where MissID='234'", objConnection);
 
                    //执行不返回结果的sql命令
                    SqlCmd.ExecuteNonQuery();
 
                    MessageBox.Show("密码修改成功!", "系统提示");
                }
 
                //关闭数据库连接
                objConnection.Close();
            }
          
        }


相关教程