简体   繁体   English

从C#桌面应用程序向远程服务器上的MySql数据库添加数据

[英]Adding of data to MySql database on remote server from C# desktop application

I have a big updated list of strings which must be uploaded with update of each row from 0 to last index by rewriting of exist records and adding of new rows to MySql database on remote server each time user calls function. 我有一个很大的字符串更新列表,每次用户调用函数时,都必须通过重写现有记录并将新行添加到远程服务器上的MySql数据库中,以从0到最后一个索引的每一行的更新来上载该字符串。

The adding of data string by string takes a lot of time even if not hangs by process: 即使不按进程挂起,逐个字符串地添加数据也要花费很多时间:

 foreach (string str in myList)
 {
     string Query = "insert into tab(a) values(@a);";
     MySqlConnection conn = new MySqlConnection(connString);
     MySqlCommand conn_ = new MySqlCommand(Query, conn);
     conn.Open();
     conn_.ExecuteNonQuery();
     conn.Close();      
}

My goal is to figure out, what should be most proper way to do this fast. 我的目标是找出最快速执行此操作的正确方法。 Maybe I should create and update table locally and then somehow upload it to database. 也许我应该在本地创建和更新表,然后以某种方式将其上传到数据库。

I have a List<string> myList = new List<string>(); 我有一个List<string> myList = new List<string>(); which contains about 5000 rows and I have table in database on remote server: 其中包含约5000行,我在远程服务器上的数据库中有表:

id |  user   | nickname
_____________________
0  |  record | record
1  |  ...    | ...   

My desired result is to update all records from 0 to highest index with adding of new records and removing of extra records in case if current upload contains less records then previous each time from 0 index, of course no maters if index will come with gap between removed rows. 我期望的结果是通过添加新记录将所有记录从0更新到最高索引,并删除多余的记录,以防万一如果当前上载的记录少于每次从0索引开始的记录,那么如果索引之间存在间隔,则当然不会发生问题删除的行。

You claim: 您声称:

Adding of data to MySql database on remote server 将数据添加到远程服务器上的MySql数据库

Which implies, you have multiple clients who know the connection string to the remote database. 这意味着您有多个客户端,这些客户端知道到远程数据库的连接字符串。 This is a security desaster! 这是一场安全灾难! Stop even thinking about it! 甚至不用再考虑了! Also, what happens if the connection string to the database changes? 此外,如果数据库的连接字符串发生更改,会发生什么? You need to update every client. 您需要更新每个客户端。 The only exception would be, if you are in an trusted environment with trusted connections, but I suspect this, since you are using MySQL . 唯一的例外是,如果您处于具有可信连接的可信环境中,但是我怀疑这是因为您使用的是MySQL

To your actual problem: 对于您的实际问题:

Your main problem is, for every item in your loop you create an connection, send something to the server and close your connection. 您的主要问题是,对于循环中的每个项目,您都创建一个连接,将某物发送到服务器并关闭连接。 And again, and again. 一遍又一遍。 Basicly you want to send on big command to the server, instead of multiple created by your loop (SQL can handle multiple insert statements at one SQL Command) . 基本上,您想向服务器发送大命令,而不是由循环创建多个命令(SQL可以在一个SQL Command上处理多个insert语句

The better (more secure way) : 更好(更安全的方式)

Create an Application for your server, which accepts myList as JSON for example and save it there. 为您的服务器创建一个应用程序,该应用程序例如接受myList作为JSON并将其保存在其中。 Probably you need to handle authorization here. 可能您需要在此处处理授权。

Your Client sends a Save Request with a myList to the Application, I have mentioned above. 如上所述,您的客户向应用程序发送了一个带有myList的保存请求。

We have some Technologies for it: 我们有一些技术:


Warning: Also, from a first look, you seem to have a problem with SQL Injections. 警告:而且,乍看之下,您似乎在SQL注入方面遇到了问题。 See what they are , and how you can prevent it . 看看它们是什么 ,以及如何防止它

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

相关问题 从C#应用程序中删除远程服务器上的MySql数据库表 - Remove MySql database table on remote server from C# application 无法从C#应用程序连接远程服务器mysql数据库 - Unable to connect remote server mysql database from C# application 从C#应用程序访问mysql远程服务器 - mysql remote server access from C# application 将数据从我的远程网站中的javascript函数传递到桌面应用程序上的ac#应用程序 - Passing data from a javascript function in my remote website to a c# application on my desktop application 如何在C#中的MessageBox中显示来自远程MySQL数据库的数据 - How to show data from remote MySQL database in a MessageBox in C# 在C#应用程序中添加数据库服务器信息 - Adding database server information in C# application 如何将 MySql 在线数据库连接到 C# 桌面应用程序 - How can connect MySql Online Database to C# desktop application 使用 SQL Server 数据库发布 C# 桌面应用程序 - Publish C# Desktop Application with SQL Server database 使用C#桌面应用程序控制对SQL Server数据库文件的访问 - Controlling access to SQL server database file with C# desktop application C#以管理员身份运行远程桌面应用程序 - C# Run Remote Desktop Application as Administrator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM