如何在第一行修复语法错误? [英] How do I fix the syntax error in the first line?

查看:105
本文介绍了如何在第一行修复语法错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在1. Line中有一个MySQL语法错误。



服务器和用户名为此更改。



以下是代码

I have a MySQL Syntax Error in the 1. Line.

Server and Username are changed for this.

Here is the Code

private void button2_Click(object sender, EventArgs e)
{
  string constring = "datasource = 123.456.789 ;port = 3306;username = xxx;password = xxx";
  string query = "INSERT  INTO mysqlcshap.Leitstelle(Auftragsnummer,Objekt,Etage/Station,Straße,HNR) VALUES('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "');";

  MySqlConnection Dateneingabe =new MySqlConnection(constring);
  MySqlCommand cmd = new MySqlCommand(query, Dateneingabe);
  MySqlDataReader myReader;

  try
  {
    Dateneingabe.Open();
    myReader = cmd.ExecuteReader();
    MessageBox.Show("Einsatz angelegt!");

    while (myReader.Read()) { }
  }
  catch (Exception ex)
  { 
    MessageBox.Show(ex.Message); 
  }
}





我的尝试:



我试图将它从服务器更改为数据源。



What I have tried:

I've tried to Change it from Server to Datasource.

推荐答案

就C#而言,没有语法错误 - 但它看起来不像一个有效的连接字符串: MySQL :: MySQL Connector / NET Developer Guide :: 5.1.1创建连接器/ NET连接字符串 [ ^ ]

我建议你看看你在哪里那,并看看其他地方是否有一个workign示例。



[edit]

并且不要这样做!

永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。总是使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

There is no syntax error as far as C# - but it doesn't look like a valid connection string: MySQL :: MySQL Connector/NET Developer Guide :: 5.1.1 Creating a Connector/NET Connection String[^]
I'd suggest that you look at where you got that, and see if there is a workign example somewhere else.

[edit]
And don't do it like that!
Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。您是否定期进行备份,不是吗?

[/ edit]

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
[/edit]


您的字段名称包含斜杠字符。然后必须引用字段名称。请参阅 MySQL :: MySQL 8.0参考手册:: 9.2架构对象名称 [ ^ ]:

You have a field name containing a slash character. Then the field name must be quoted. See MySQL :: MySQL 8.0 Reference Manual :: 9.2 Schema Object Names[^]:
Quote:

不带引号的标识符中允许的字符:



ASCII:[0- 9,az,AZ

Permitted characters in unquoted identifiers:

ASCII: [0-9,a-z,A-Z


_](基本拉丁字母,数字0-9,美元,下划线)



扩展: U + 0080 .. U + FFFF

_] (basic Latin letters, digits 0-9, dollar, underscore)

Extended: U+0080 .. U+FFFF

避免使用字段和表名称这样的字符。所以我建议如果可能的话更改字段名称。

Avoid such characters for field and table names. So I suggest to change the field name if possible.


这篇关于如何在第一行修复语法错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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