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 WindowsFormsApplication1
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
if (textBox2.Text.Trim().Length == 0 || textBox3.Text.Trim().Length == 0 || textBox4.Text.Trim().Length == 0)
{
MessageBox.Show("请输入完整!");
return;
} if (textBox3.Text.Trim() != textBox4.Text.Trim())
{
MessageBox.Show("两次密码输入不一致!");
return;
} String ConnString = "Data Source=.;Initial Catalog=WageManage;User ID=sa;password=147258369;";
SqlConnection conn = new SqlConnection(ConnString);
DataTable dt = new DataTable(); String sql = "SELECT * FROM stuff where StuffID= StuffID and Password = " + textBox2.Text.Trim(); try
{
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
da.Fill(dt);
}
catch (Exception Err)
{
MessageBox.Show(Err.Message);
} if (dt.Rows.Count == 0)
{
MessageBox.Show("旧密码输入错误!"); dt.Dispose();
}
else
{
sql = "UPDATE Stuff set Password ='" + textBox3.Text.Trim() + "' where StuffID= '" + "'"; SqlCommand cmd = new SqlCommand();
cmd.CommandText = sql;
cmd.Connection = conn;
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (Exception Err)
{
MessageBox.Show("更新出现异常!");
}
finally
{
conn.Close();
} MessageBox.Show("密码更新成功!"); }
} private void button2_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
this.Hide();
if (frm2.ShowDialog() == DialogResult.OK)
{
this.Show();
}
} private void Form5_Load(object sender, EventArgs e)
{ }
}
}
点了修改之后提示成功但是数据库数据却没有变化 求大神指点
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 WindowsFormsApplication1
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
if (textBox2.Text.Trim().Length == 0 || textBox3.Text.Trim().Length == 0 || textBox4.Text.Trim().Length == 0)
{
MessageBox.Show("请输入完整!");
return;
} if (textBox3.Text.Trim() != textBox4.Text.Trim())
{
MessageBox.Show("两次密码输入不一致!");
return;
} String ConnString = "Data Source=.;Initial Catalog=WageManage;User ID=sa;password=147258369;";
SqlConnection conn = new SqlConnection(ConnString);
DataTable dt = new DataTable(); String sql = "SELECT * FROM stuff where StuffID= StuffID and Password = " + textBox2.Text.Trim(); try
{
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
da.Fill(dt);
}
catch (Exception Err)
{
MessageBox.Show(Err.Message);
} if (dt.Rows.Count == 0)
{
MessageBox.Show("旧密码输入错误!"); dt.Dispose();
}
else
{
sql = "UPDATE Stuff set Password ='" + textBox3.Text.Trim() + "' where StuffID= '" + "'"; SqlCommand cmd = new SqlCommand();
cmd.CommandText = sql;
cmd.Connection = conn;
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (Exception Err)
{
MessageBox.Show("更新出现异常!");
}
finally
{
conn.Close();
} MessageBox.Show("密码更新成功!"); }
} private void button2_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
this.Hide();
if (frm2.ShowDialog() == DialogResult.OK)
{
this.Show();
}
} private void Form5_Load(object sender, EventArgs e)
{ }
}
}
点了修改之后提示成功但是数据库数据却没有变化 求大神指点
sql = "UPDATE Stuff set Password ='" + textBox3.Text.Trim() + "' where StuffID= '" + "'";StuffID=后的条件不完整吧。程序里可以做判断的。cmd.ExecuteNonQuery();会返回数据库响应的行数。如果大于0就说明数据库里有数据被更新了。int Exec=cmd.ExecuteNonQuery();
if(Exec>0)
{执行提示}
else
{插入错误日志}