asp.net mysql update,在ASP.net UPDATE SQL语句之后数据库没有更新

I currently have a problem attepting to update a record within my database. I have a webpage that displays in text boxes a users details, these details are taken from the session upon login. The aim is to update the details when the user overwrites the current text in the text boxes.

I have a function that runs when the user clicks the 'Save Details' button and it appears to work, as i have tested for number of rows affected and it outputs 1. However, when checking the database, the record has not been updated and I am unsure as to why.

I've have checked the SQL statement that is being processed by displaying it as a label and it looks as so:

UPDATE [users]

SET [email] = @email,

[firstname] = @firstname,

[lastname] = @lastname,

[promo] = @promo

WHERE [users].[user_id] = 16

The function and other relevant code is:

Sub Page_Load(sender As Object, e As EventArgs)

usernameLabel.text = session.contents.item("UserName")

if usernameLabel.text = "" then

logoutButton.Visible = False

loggedInAsLabel.Visible = False

else

labelGuest.Visible = False

linkLogin.Visible = False

linkRegister.Visible = False

end if

emailBox.text = session.contents.item("Email")

firstBox.text = session.contents.item("FirstName")

lastBox.text = session.contents.item("LastName")

promoBox.text = session.contents.item("Promo")

End Sub

Sub Button1_Click(sender As Object, e As EventArgs)

changeDetails(emailBox.text, firstBox.text, lastBox.text, promoBox.text)

End Sub

Function changeDetails(ByVal email As String, ByVal firstname As String, ByVal lastname As String, ByVal promo As String) As Integer

Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\Documents an"& _

"d Settings\Paul Jarratt\My Documents\ticketoffice\datab\ticketoffice.mdb"

Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)

Dim queryString As String = "UPDATE [users] SET [email]=@email, [firstname]=@firstname, [lastname]=@lastname, "& _

"[promo]=@promo WHERE ([users].[user_id] = " + session.contents.item("ID") + ")"

Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand

dbCommand.CommandText = queryString

dbCommand.Connection = dbConnection

Dim dbParam_email As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter

dbParam_email.ParameterName = "@email"

dbParam_email.Value = email

dbParam_email.DbType = System.Data.DbType.[String]

dbCommand.Parameters.Add(dbParam_email)

Dim dbParam_firstname As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter

dbParam_firstname.ParameterName = "@firstname"

dbParam_firstname.Value = firstname

dbParam_firstname.DbType = System.Data.DbType.[String]

dbCommand.Parameters.Add(dbParam_firstname)

Dim dbParam_lastname As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter

dbParam_lastname.ParameterName = "@lastname"

dbParam_lastname.Value = lastname

dbParam_lastname.DbType = System.Data.DbType.[String]

dbCommand.Parameters.Add(dbParam_lastname)

Dim dbParam_promo As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter

dbParam_promo.ParameterName = "@promo"

dbParam_promo.Value = promo

dbParam_promo.DbType = System.Data.DbType.[String]

dbCommand.Parameters.Add(dbParam_promo)

Dim rowsAffected As Integer = 0

dbConnection.Open

Try

rowsAffected = dbCommand.ExecuteNonQuery

Finally

dbConnection.Close

End Try

labelTest.text = rowsAffected.ToString()

if rowsAffected = 1 then

labelSuccess.text = "* Your details have been updated and saved"

else

labelError.text = "* Your details could not be updated"

end if

End Function

Any help would be greatly appreciated.

解决方案

Does your page have a RequiresTransaction property? If so, check that there are no exceptions thrown elsewhere during the request which might cause the transaction to roll back - leaving the data unchanged.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用以下代码实现asp.net连接MySQL数据库: ``` using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using MySql.Data.MySqlClient; public class MySQLDBHelper { private static string connectionString = "server=localhost;user id=root;password=123456;database=test;Charset=utf8;"; public static DataTable ExecuteDataTable(string commandText, CommandType commandType, params MySqlParameter[] parameters) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand command = new MySqlCommand(commandText, connection)) { command.CommandType = commandType; if (parameters != null) { command.Parameters.AddRange(parameters); } MySqlDataAdapter adapter = new MySqlDataAdapter(command); DataTable dataTable = new DataTable(); adapter.Fill(dataTable); return dataTable; } } } public static int ExecuteNonQuery(string commandText, CommandType commandType, params MySqlParameter[] parameters) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand command = new MySqlCommand(commandText, connection)) { command.CommandType = commandType; if (parameters != null) { command.Parameters.AddRange(parameters); } connection.Open(); int result = command.ExecuteNonQuery(); return result; } } } } ``` 其中,MySQLDBHelper是一个帮助类,提供了两个静态方法,一个用于执行SELECT语句并返回DataTable,另一个用于执行INSERT/UPDATE/DELETE等操作并返回受影响的行数。需要将connectionString变量替换为自己的MySQL连接字符串。使用示例: ``` MySqlParameter[] parameters = new MySqlParameter[] { new MySqlParameter("@name", "张三"), new MySqlParameter("@age", 20) }; string sql = "INSERT INTO student (name, age) VALUES (@name, @age)"; int result = MySQLDBHelper.ExecuteNonQuery(sql, CommandType.Text, parameters); if (result > 0) { Response.Write("添加成功!"); } else { Response.Write("添加失败!"); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值