更新ASP.NET中mysql数据库中的值 [英] Update a value in a mysql database in ASP.NET

查看:57
本文介绍了更新ASP.NET中mysql数据库中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够将数据存储在数据库中,但我无法更新其中的值。我有一个包含5个值的表,它们是名称,用户,密码,标题和项目,我需要能够通过输入用户来更新密码。

我试过这段代码:

 String connString = System.Configuration.ConfigurationManager.ConnectionStrings [web]。ToString(); 
conn = new MySql.Data.MySqlClient.MySqlConnection(connString);
conn.Open();
// queryStr =;
cmd = conn.CreateCommand();
cmd.CommandType = System.Data.CommandType.Text;
queryStr =UPDATE meal.claves set usuario ='+ user.Text +where clave ='+ pass +';
cmd = new MySql.Data.MySqlClient.MySqlCommand(update meal.claves Set usuario = @ user,clave = @ pass);
// cmd = new MySql.Data.MySqlClient.MySqlCommand(queryStr,conn);
//cmd.ExecuteReader();
cmd.ExecuteNonQuery();

conn.Close();



收到错误,说连接必须是开放且有效的。



我尝试过:



我试过这个视频中的代码:

< a href => https://www.youtube.com/watch?v=sggbk2ES8DM

解决方案

线索

 cmd =  new  ... 

。您的新cmd没有连接。

另外,不要连接字符串以形成sql命令字符串...使用命令参数


 queryStr =   UPDATE meal.claves set usuario =' + user.Text +  其中clave =' + pass +  ' ; 



不是你问题的解决方案,而是你遇到的另一个问题。

永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

按示例进行SQL注入攻击 [ ^ ]

PHP:SQL注入 - 手册 [ ^ ]

SQL注入预防备忘单 - OWASP [ ^ ]


I am able to store data in the database, but I am unable to update values in it. I have a table with 5 values, which are name, user, password, title, and project, and I need to be able to update the password, just by entering the user.
I tried this code:

String connString = System.Configuration.ConfigurationManager.ConnectionStrings["web"].ToString();
        conn = new MySql.Data.MySqlClient.MySqlConnection(connString);
        conn.Open();
        //queryStr = "";
        cmd = conn.CreateCommand();
        cmd.CommandType = System.Data.CommandType.Text;
        queryStr = "UPDATE meal.claves set usuario='" + user.Text+"where clave ='"+pass+"'";
        cmd = new MySql.Data.MySqlClient.MySqlCommand("update meal.claves Set usuario=@user,clave=@pass");
        //cmd = new MySql.Data.MySqlClient.MySqlCommand(queryStr, conn);
        //cmd.ExecuteReader();
            cmd.ExecuteNonQuery();
        
        conn.Close();


Got error saying connection must be open and valid.

What I have tried:

I tried the code in this video:
https://www.youtube.com/watch?v=sggbk2ES8DM

解决方案

Clue

cmd = new ... 

. Your new cmd does not have a connection.
Also, do not concatenate strings to form sql command strings...use command parameters


queryStr = "UPDATE meal.claves set usuario='" + user.Text+"where clave ='"+pass+"'";


Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]


这篇关于更新ASP.NET中mysql数据库中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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