InsertQuery from C# to MySql
本文关键字:MySql to from InsertQuery | 更新日期: 2023-09-27 18:12:29
我是一个编程新手,希望有人能帮助我。我想把我在c#中收到的数据存储到MySql:表应该是可变的根据我收到的数据
string myInsertQuery = string。格式("插入卷(日期,时间,bidvol, bidprice, askvol, lastprice, volume) VALUES({0},{1},{2},{3},{4},{5},{6},{7})",now_date_marketlastnow_time_marketlast, bidvol, bidprice, askvol, ePrice1,e.Volume);
可以工作,但是如何定义表变量。如果我将volumen替换为{0}和,则table不起作用。请帮帮我。
首先,您可能不希望执行您正在尝试的操作,因为这会使您的代码容易受到SQL注入攻击。有一些方法可以使用SqlCommand对象动态创建不允许SQL注入的SQL语句。
也就是说,当您用{0}替换volumen时,除非要将表名添加到值列表的开头,否则将插入now_date_marketlast作为表名。正确的写法是:
string myInsertQuery = String.Format("INSERT INTO {0} (date, time, bidvol, bidprice, askprice, askvol, lastprice, volume) VALUES({1}, {2}, {3}, {4}, {5}, {6}, {7}, {8})", "volumen", now_date_marketlast, now_time_marketlast, bidvol, bidprice, askprice, askvol, ePrice1, e.Volume);
仍然需要提供表名,只是在代码的不同位置执行。由于SQL注入,这不是一个好主意。
try this:
var tableName = "volumen";
string myInsertQuery = String.Format("INSERT INTO {8} (date, time, bidvol, bidprice, askprice, askvol, lastprice, volume) VALUES({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7})", now_date_marketlast, now_time_marketlast, bidvol, bidprice, askprice, askvol, ePrice1, e.Volume, tableName);
如果要分配表名。你可以这样做:
string myInsertQuery = String.Format("INSERT INTO {8} (date, time, bidvol, bidprice, askprice, askvol, lastprice, volume) VALUES({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7})", now_date_marketlast, now_time_marketlast, bidvol, bidprice, askprice, askvol, ePrice1, e.Volume, volumen);
Volumen是你的表名