简体   繁体   English

C#语法'“ + stringName +”'

[英]C# syntax ' “ + stringName + ” '

What does ' " + stringName + " ' mean in c# ? 在c#中' " + stringName + " '是什么意思? I thought in programming if you enclose something withing quotes it will be treated as string.Some detail would be appreciated as i have just started learning c# 我认为在编程中,如果您用引号将其引起来,则将其视为string.I的一些细节将不胜感激,因为我刚刚开始学习C#

Code : 代码:

string sql_insertQuery = "INSERT into UserData(username,password,country) VALUES ('"+Usn.Text+"','lpxt','l.Text')";

Usn is the Id of a textbox, I am just testing right now but i know inputting information like this is not recommended because of SQL Injection Usn是文本框的ID,我现在正在测试,但是我知道由于SQL注入,不建议输入这样的信息

Edit : I understand the answers provided below about concatenation but why do i get error if i use 编辑:我理解下面提供的有关级联的答案,但是为什么我使用时会出错

string sql_insertQuery = "INSERT into UserData(username,password,country) VALUES ("+Usn.Text+",'lpxt','l.Text')";

double quotes only 仅双引号

and why does using single quotes pass +Usn.Text+ as the input string 以及为什么使用单引号将+ Usn.Text +用作输入字符串

 string sql_insertQuery = "INSERT into UserData(username,password,country) VALUES ('+Usn.Text+','lpxt','l.Text')";

In this case the text inside the "Usn"-Control will be included into your SQL-query. 在这种情况下,“ Usn” -Control内部的文本将包含在您的SQL查询中。 Yet the String within the query will be surrounded by ''. 但是查询中的字符串将被''包围。

Like in this example: 像这个例子:

    string stringVariable = "myString";
    Console.WriteLine("'" + stringVariable + "'");

Here the output will be: 这里的输出将是:

'myString' '的myString'

Notice how the output is surrounded by the single quotes. 注意输出如何用单引号引起来。

Usn.Text is enclosed within one single quote and one double quote(double quotes within single quotes) Usn.Text包含在一个单引号和一个双引号中(双引号包含在单引号中)

No. 没有。

This value is enclosed only single quotes since your username column is character typed. 由于您的username名列是字符键入的,因此此值仅用单引号引起来。

But double quotes are for string concatenation for this these 3 strings; 但是双引号是针对这三个字符串的字符串连接;

"INSERT into UserData(username,password,country) VALUES ('"
Usn.Text
"','lpxt','l.Text')"

As you said, you should always use parameterized queries . 如您所说,您应该始终使用参数化查询 This kind of string concatenations are open for SQL Injection attacks. 这类字符串连接对SQL注入攻击开放。

Edit : I understand the answers provided below about concatenation but why do i get error if i use 编辑:我理解下面提供的有关级联的答案,但是为什么我使用时会出错

You didn't even tell us what error you get but I assume you try to insert your username character typed column, you have to use single quotes with it. 甚至没有告诉我们你得到了什么错误,但我相信你尝试插入您的username键入的字符列,您必须使用单引号吧。 Of course, if you use prepared statements , you can skip those quotes. 当然,如果使用准备好的语句 ,则可以跳过这些引号。

and why does using single quotes pass +Usn.Text+ as the input string 以及为什么使用单引号将+ Usn.Text +用作输入字符串

Because when you write VALUES ('+Usn.Text+','lpxt','l.Text')" , you insert your +Usn.Text+ as a string literal , It will not insert it's Text value, it will insert +Usn.Text+ as a string. 因为当您编写VALUES ('+Usn.Text+','lpxt','l.Text')" ,您将+Usn.Text+作为字符串文字插入,不会插入Text值,而是会插入+Usn.Text+作为字符串。

For example, if Usn.Text = "foo"; 例如,如果Usn.Text = "foo"; you will not insert that foo string, you will insert +Usn.Text+ in such a case. 不会插入该foo字符串,在这种情况下将插入+Usn.Text+

The plus operator is used for string concatenation . plus运算符 用于 字符串连接
More can be read at the documentation/guide. 可以在文档/指南中阅读更多内容。
Excerpt from https://msdn.microsoft.com/en-us/library/ms228504.aspx : 摘自https://msdn.microsoft.com/zh-cn/library/ms228504.aspx

Concatenation is the process of appending one string to the end of another string. 串联将一个字符串附加到另一字符串末尾的过程 When you concatenate string literals or string constants by using the + operator , t he compiler creates a single string. 使用+运算符连接字符串文字或字符串常量时, 编译器将创建单个字符串。 No run time concatenation occurs. 没有运行时级联发生。 However, string variables can be concatenated only at run time. 但是,字符串变量只能在运行时连接。 In this case, you should understand the performance implications of the various approaches. 在这种情况下,您应该了解各种方法对性能的影响。

EDIT (since the question has been edited): 编辑(因为问题已被编辑):
Since the double qotes and the + are in qotes, they won't be processed by the compiler and therefore be handled as strings. 由于double qote和+是qotes,因此编译器不会对其进行处理,因此将其作为字符串进行处理。 so you would literally add " + stringName + " to the database 因此您将在字面上添加" + stringName + "到数据库中

A good explanation of single and double qotes can be found here: 可以在此处找到有关单行和双行的详细说明:
https://stackoverflow.com/a/602035/3948598 https://stackoverflow.com/a/602035/3948598

Single quotes encode a single character (data type char), while double quotes encode a string of multiple characters. 单引号编码单个字符(数据类型为char),而双引号编码多个字符的字符串。 The difference is similar to the difference between a single integer and an array of integers. 差异类似于单个整数和整数数组之间的差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM