protected void UpdateData(int sex, string UserBirth, string telephone )
{ System.Data.SqlClient.SqlDataAdapter MyDataAdapter;
System.Data.SqlClient.SqlCommand MyCommand;
System.Data.SqlClient.SqlConnection MyConn;
MyDataAdapter = new System.Data.SqlClient.SqlDataAdapter();
MyCommand = new System.Data.SqlClient.SqlCommand();
MyConn = new System.Data.SqlClient.SqlConnection(Common.Class1.Connectstion);
//
// MyDataAdapter
//
MyDataAdapter.UpdateCommand = MyCommand;
MyDataAdapter.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "tbl_members", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("UserId", "UserId"),
new System.Data.Common.DataColumnMapping("UserType", "UserType"),
new System.Data.Common.DataColumnMapping("UserSex", "UserSex"),
new System.Data.Common.DataColumnMapping("UserBirthday", "UserBirthday"),
new System.Data.Common.DataColumnMapping("UserTelephone", "UserTelephone"),
})});
// MyCommand
MyCommand.CommandText = @"UPDATE tbl_members set UserId=@UserId,UserType=@UserType, UserSex= @UserSex, UserBirthday=@UserBirthday, UserTelephone=@UserTelephone, where UserId=@UserId";
MyCommand.Connection = MyConn;
MyCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserId", System.Data.SqlDbType.BigInt, 8, "UserId"));
MyCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserType", System.Data.SqlDbType.Int, 4, "UserType"));
MyCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserSex", System.Data.SqlDbType.Bit, 1, "UserSex"));
MyCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserBirthday", System.Data.SqlDbType.NVarChar, 50, "UserBirthday"));
MyCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserTelephone", System.Data.SqlDbType.NVarChar, 50, "UserTelephone")); MyCommand.Parameters["@UserId"].Value = GetID();
MyCommand.Parameters["@UserType"].Value = 1;
MyCommand.Parameters["@UserSex"].Value = sex;
MyCommand.Parameters["@UserBirthday"].Value = UserBirth;
MyCommand.Parameters["@UserTelephone"].Value = telephone;
MyConn.Open();
try
{
MyCommand.ExecuteNonQuery();
ErrorMsg("成功修改了用户", true);
}
catch (Exception Ex)
{
ErrorMsg("出现错误,请重试。" + Ex.Message.ToString(), false);
}
MyConn.Close();
protected void btnAdd_Click(object sender, System.EventArgs e)
{ int sex = System.Convert.ToInt32(DLSex.SelectedValue.ToString().Trim());
string UserBirth = txtUserBirthYear.Text.Trim()
+ "-"
+ DLBirthMonth.SelectedValue.ToString().Trim()
+ "-"
+ DLBirthDay.SelectedValue.ToString().Trim();
string telephone = txtTelephone.Text.Trim();
try
{
System.Convert.ToDateTime(UserBirth);
}
catch
{
ErrorMsg("生日填写有误,请重新填写。", false);
return;
} UpdateData(sex, UserBirth, telephone);
}
}
搞不懂为什么点击更新后,数据库里的数据并没有更新。代码也没有显示错误,而是显示成功修改了用户。其实没有改变,到底怎么回事啊,请大家看看代码有什么问题。我做插入数据库也是这样的,怎么就可以啊。当然插入insert有些不一样。但是都是差不多的。真的很奇怪啊。
{ System.Data.SqlClient.SqlDataAdapter MyDataAdapter;
System.Data.SqlClient.SqlCommand MyCommand;
System.Data.SqlClient.SqlConnection MyConn;
MyDataAdapter = new System.Data.SqlClient.SqlDataAdapter();
MyCommand = new System.Data.SqlClient.SqlCommand();
MyConn = new System.Data.SqlClient.SqlConnection(Common.Class1.Connectstion);
//
// MyDataAdapter
//
MyDataAdapter.UpdateCommand = MyCommand;
MyDataAdapter.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "tbl_members", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("UserId", "UserId"),
new System.Data.Common.DataColumnMapping("UserType", "UserType"),
new System.Data.Common.DataColumnMapping("UserSex", "UserSex"),
new System.Data.Common.DataColumnMapping("UserBirthday", "UserBirthday"),
new System.Data.Common.DataColumnMapping("UserTelephone", "UserTelephone"),
})});
// MyCommand
MyCommand.CommandText = @"UPDATE tbl_members set UserId=@UserId,UserType=@UserType, UserSex= @UserSex, UserBirthday=@UserBirthday, UserTelephone=@UserTelephone, where UserId=@UserId";
MyCommand.Connection = MyConn;
MyCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserId", System.Data.SqlDbType.BigInt, 8, "UserId"));
MyCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserType", System.Data.SqlDbType.Int, 4, "UserType"));
MyCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserSex", System.Data.SqlDbType.Bit, 1, "UserSex"));
MyCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserBirthday", System.Data.SqlDbType.NVarChar, 50, "UserBirthday"));
MyCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserTelephone", System.Data.SqlDbType.NVarChar, 50, "UserTelephone")); MyCommand.Parameters["@UserId"].Value = GetID();
MyCommand.Parameters["@UserType"].Value = 1;
MyCommand.Parameters["@UserSex"].Value = sex;
MyCommand.Parameters["@UserBirthday"].Value = UserBirth;
MyCommand.Parameters["@UserTelephone"].Value = telephone;
MyConn.Open();
try
{
MyCommand.ExecuteNonQuery();
ErrorMsg("成功修改了用户", true);
}
catch (Exception Ex)
{
ErrorMsg("出现错误,请重试。" + Ex.Message.ToString(), false);
}
MyConn.Close();
protected void btnAdd_Click(object sender, System.EventArgs e)
{ int sex = System.Convert.ToInt32(DLSex.SelectedValue.ToString().Trim());
string UserBirth = txtUserBirthYear.Text.Trim()
+ "-"
+ DLBirthMonth.SelectedValue.ToString().Trim()
+ "-"
+ DLBirthDay.SelectedValue.ToString().Trim();
string telephone = txtTelephone.Text.Trim();
try
{
System.Convert.ToDateTime(UserBirth);
}
catch
{
ErrorMsg("生日填写有误,请重新填写。", false);
return;
} UpdateData(sex, UserBirth, telephone);
}
}
搞不懂为什么点击更新后,数据库里的数据并没有更新。代码也没有显示错误,而是显示成功修改了用户。其实没有改变,到底怎么回事啊,请大家看看代码有什么问题。我做插入数据库也是这样的,怎么就可以啊。当然插入insert有些不一样。但是都是差不多的。真的很奇怪啊。
2 MyCommand.Command后面的语句你放到查询分析器里看有问题没?
检查这二处
2 MyCommand.Command后面的语句你放到查询分析器里看有问题没?
检查这二处
这一句
MyCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserId", System.Data.SqlDbType.BigInt这一句MyCommand.Parameters["@UserId"].Value = GetID();
当然还有这一句的UserId=@UserId,条件语句换成:UserType=@UserType.就可以了where @"UPDATE tbl_members set UserId=@UserId,UserType=@UserType, UserSex= @UserSex, UserBirthday=@UserBirthday, UserTelephone=@UserTelephone, where UserId=@UserId";