使用addwithvalue进行更新 [英] Using addwithvalue to update

查看:83
本文介绍了使用addwithvalue进行更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



如何使用AddWithValue更新语句?为了插入新数据,没关系。但是为了更新,它不会更新arrImg和arrLogo,它在mysql上变为null。以下是我的更新代码。请查看并指导我。



Hi all,

How to use AddWithValue for updating statement? For insert new data, it's ok. But for updating, it not updating the arrImg and arrLogo where it become null on mysql. Below is my code for updating. please see and guide me.

strSQL = "UPDATE malldir.tbldirectory SET dirno=@dirno,dirname=@dirname,dirdetails=@dirdetails,category=@category,level=@level,lotno=@lotno,website=@website,phoneno=@phoneno,dirinitial=@dirinitial,dir_image=@dirimage,dir_logo=@dirlogo WHERE dirname='" & strcTmpDir.strShopOldName & "';"

            mysqlconn = New MySqlClient.MySqlConnection(MyConnectionString)
            mysqlconn.Open()
            With mysqlcmd
                .CommandText = strSQL
                .Connection = mysqlconn
                .Parameters.AddWithValue("@dirno", strcTmpDir.strShopNo)
                .Parameters.AddWithValue("@dirname", strcTmpDir.strShopName)
                .Parameters.AddWithValue("@dirdetails", strcTmpDir.strShopDetails)
                .Parameters.AddWithValue("@category", strcTmpDir.strCategory)
                .Parameters.AddWithValue("@level", strcTmpDir.strFloor)
                .Parameters.AddWithValue("@lotno", strcTmpDir.strLotNo)
                .Parameters.AddWithValue("@website", strcTmpDir.strWebsite)
                .Parameters.AddWithValue("@phoneno", strcTmpDir.strPhoneNo)
                .Parameters.AddWithValue("@dirinitial", strcTmpDir.strInit)
                .Parameters.AddWithValue("@dirimage", strcTmpDir.arrImg)
                .Parameters.AddWithValue("@dirlogo", strcTmpDir.arrLogo)
                intRec = .ExecuteNonQuery()
            End With





我的sql语句好吗?有什么建议吗?



Is my sql statement is good?any suggestion?

推荐答案

你可能需要处理如下的空值< br $>


.Parameters.AddWithValue(@ dirimage,(object)strcTmpDir.arrImg ?? (对象)DBNull.Value)



并且还使用所有值的参数(你没有使用dirname的参数)

您需要使用语句
you may need to handle null values like below

.Parameters.AddWithValue("@dirimage", (object)strcTmpDir.arrImg?? (object)DBNull.Value)

and also use parameters for all the values (you haven't use parameter for dirname)
You need to wrap the Command and the Connection in using statements

这篇关于使用addwithvalue进行更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆