c#中使用mysql查询语句_遇到@符合怎么办_在C#和MySQL中对变量使用查询

I need a following set of statements executed , currently it gives Fatal Error

I need the query output of following query which uses two variables

Database is MySQL and language is C#

I am trying to use this in c# code using

MyReader=new MySQLCommand(this query ,connection object).ExecuteReader()

SET @lastItem := 0, @lastValue := 0;

SELECT CONCAT (

DATE (t5.InventoryDate)

,t5.SKUorItem

) AS PK,

t5.CustomerID, t5.entityID, t5.inventoryDate, t5.SKUorItem, t5.Category, t5.inventory FROM

(

SELECT

'3' as CustomerID,

'90' as entityID,

t1.InventoryDate as inventoryDate,

t1.idItem as SKUorItem,

t4.categoryInventary as Category,

ifnull(t1.itemQty,0) as sales, ifnull(t2.buyQty,0) as StockMove,

@lastValue := if( @lastItem = t1.idItem, @lastValue + ifnull(t2.buyQty,0) - ifnull(t1.itemQty,0), ifnull(t2.buyQty,0) - ifnull(t1.itemQty,0) ) as inventory,

@lastItem := t1.idItem

FROM

(

select date(date) as inventorydate, idItem, sum(quantity) as itemqty

from subway.saleitem

group by idItem, date(date)

) as t1

LEFT OUTER JOIN

(

select date(date) as InventoryDate, idItem, Sum(initialBuyQuantity) as buyqty

FROM Subway.InvoiceStock

GROUP BY idItem, date(date)

) as t2

ON t1.InventoryDate = t2.InventoryDate and t1.idItem = t2.idItem

INNER JOIN subway.Item as t3 on t1.iditem = t3.iditem

LEFT JOIN subway.CategoryInventary as t4 on t3.idCategoryInventary = t4.idCategoryInventary

order by t1.iditem, t1.InventoryDate

) t5

Solutions1

Wrap these queries into an Stored Procedure or UDF. Simply call that from c# code.

C#,将MySQL数据库的数据信息查询出来并在DataViewGrid(通常指Windows Forms的DataGridView控件)控件显示,可以通过以下步骤实现: 1. 首先,确保你的项目已经添加了对MySQL数据库的访问支持,通常这需要引入MySQL的.NET Connector。 2. 使用`MySqlConnection`类来建立到MySQL数据库的连接。 3. 创建`MySqlCommand`对象,用于执行SQL查询语句。 4. 使用`MySqlDataAdapter`对象来填充`DataSet`或`DataTable`。 5. 将`DataTable`对象设置为DataGridView的DataSource属性。 下面是具体的代码示例: ```csharp using System; using System.Data; using MySql.Data.MySqlClient; // 确保引入了MySQL的.NET Connector命名空间 public void LoadDataToDataGridView(string connectionString, string query) { // 创建数据库连接 using (MySqlConnection connection = new MySqlConnection(connectionString)) { // 创建DataAdapter和DataSet MySqlDataAdapter adapter = new MySqlDataAdapter(); DataSet dataSet = new DataSet(); try { // 打开连接 connection.Open(); // 设置DataAdapter的SelectCommand属性 adapter.SelectCommand = new MySqlCommand(query, connection); // 使用DataAdapter填充DataSet adapter.Fill(dataSet); // 假设你要将数据填充到名为dataGridView1的DataGridView控件 dataGridView1.DataSource = dataSet.Tables[0]; } catch (MySqlException ex) { // 异常处理 MessageBox.Show("数据库操作错误: " + ex.Message); } finally { // 关闭连接 connection.Close(); } } } ``` 在实际应用,你需要替换`connectionString`和`query`变量的值为你的数据库连接字符串和想要执行的查询语句
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值