将数据附加到sql server数据库中的现有数据 [英] Appending data to an existing data in sql server database

查看:112
本文介绍了将数据附加到sql server数据库中的现有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子,其中包含学生姓名学生标记学生成绩的字段



如果已经有一行 - >在那张桌子上



学生姓名 - 学生标记--- studentgrade

--- ram ----------- - 80 ------------ A -------





如果我想要通过更新学生标记来编辑这一行(现有学生标记+80 =新学生标记)然后该怎么办



我试过



更新studtable set studentmark = studentmark + 80,其中studentname = textbox1.text



我希望实现类似追加数据的功能到行中的现有数据:(





这不起作用:(我不知道为什么:(请帮帮我..或者任何人都可以告诉我是否使用更新sql中的where子句是错误的:


:(请帮我解决紧急问题:(

I have a table with contains the fields student name student marks student grade

If aldready there is a row -> in that table

Studentname -- studentmarks --- studentgrade
---ram ------------- 80 ------------ A-------


If i want to edit this row by updating the student mark as ( "existing student mark+80=new student mark") then what shall i do

I tried

Update studtable set studentmark=studentmark+80 where studentname=textbox1.text

I want to achieve something like appending data to existing data in the row :(


this is not working :( i dont know why :( please help me out ... or else can anyone tell me whether

using "where clause in Update sql is wrong :( please help me out urgent :(

推荐答案

首先,请研究SQL注入。你的代码非常容易受到攻击。你需要传递文本框中的内容作为参数i而不是在字符串中添加它。



您缺少的是字符串值周围的单引号。那么,可行的是更新studtable set studentmark = studentmark + 80,其中studentname =''bill''。但是,做这样的事情。

First off, please research SQL injections. Your code is extremely vulnerable. You need to pass what is in the textbox as a parameter instead of adding it in the string.

What you are missing are single quotes around the string value. So, what would work is Update studtable set studentmark=studentmark+80 where studentname=''bill''. However, do something like this.
SqlCommand cmd = new SqlCommand();
cmd.Connection = sqlConn; // already setup somewhere.
cmd.CommandType = CommandType.Text
cmd.CommandText = "Update studtable set studentmark=studentmark+80 where studentname=@studentName" 
cmd.Parameters.AddWithValue("@studentName", textbox1.Text);
cmd.ExecuteNonQuery();


这篇关于将数据附加到sql server数据库中的现有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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