Ventuz-MySQL8.0数据连接操作说明 方法介绍 Ventuz连接数据库有两种方法: 第一种:采用database节点连接query查询 第二种:写c#脚本连接
方法一:
前期准备 工具:ventuz designer5 MySQL8.0 设置节点: 图1-1 ventuz节点图 C#脚本 双击c#脚本节点,设置input output Input:点击+号键添加Method1 和reset方法 Output:点击+号键添加String1变量用于输出数据库值(方法数量、方法体及变量类型根据需要设定) 图1-2 input设置 脚本代码:
using System;
- using Ventuz.Kernel;
- using System.Data.Odbc;
- using System.Data;
-
- public class Script : ScriptBase, System.IDisposable
- {
- // This member is used by the Validate() method to indicate
- // whether the Generate() method should return true or false
- // during its next execution.
- private bool changed;
-
- // This Method is called if the component is loaded/created.
- public Script()
- {
- // Note: Accessing input or output properties from this method
- // will have no effect as they have not been allocated yet.
- }
-
- // This Method is called if the component is unloaded/disposed
- public virtual void Dispose()
- {
- }
-
- // This Method is called if an input property has changed its value
- public override void Validate()
- {
- // Remember: set changed to true if any of the output
- // properties has been changed, see Generate()
- }
-
- // This Method is called every time before a frame is rendered.
- // Return value: if true, Ventuz will notify all nodes bound to this
- // script node that one of the script's outputs has a
- // new value and they therefore need to validate. For
- // performance reasons, only return true if output
- // values really have been changed.
- public override bool Generate()
- {
- changed = true;
- return true;
- }
-
- // This Method is called if the function/method Method1 is invoked by the user or a bound event.
- // Return true, if this component has to be revalidated!
- public bool OnMethod1(int arg)
- {
- String1 = "";
- string strconn = "DRIVER={MySQL ODBC 8.0 Unicode Driver};SERVER=localhost;DATABASE=databaseName;USER=userName;PASSWORD=password;OPTION=3;";
- string sql = "select name from table";
- OdbcConnection con = new OdbcConnection(strconn); //建立连接对象
- con.Open();
- OdbcCommand com = new OdbcCommand(sql, con); //执行查询语句赋值给com对象
- OdbcDataReader reader = com.ExecuteReader();
- //读到数据时写入String1
- while (reader.Read())
- {
- String1 += reader[0].ToString() + " ";
- }
- con.Close();
- return true;
- }
-
- // This Method is called if the function/method reset is invoked by the user or a bound event.
- // Return true, if this component has to be revalidated!
- public bool Onreset(int arg)
- {
- String1 = "";
- return false;
- }
- }
执行 点击图示方法即可显示数据库内容 图1-3 执行结果
方法二
前期准备 设置节点: 图2-1 ventuz节点图 连接字符串:可以查询官网:https://www.connectionstrings.com/ 我的数据库版本是mysql8.0 采用ODBC连接 图2-2 odbc数据源设置
附我的ConnectionString供参考:
DRIVER={MySQL ODBC 8.0 Unicode Driver};SERVER=localhost; DATABASE=databaseName; USER=userName; PASSWORD=password;OPTION=3;
细节操作 Query节点 Query节点用于书写sql语句,点击执行即可查看输出结果 图2-3 query节点设置 Row节点 Ventuz通过query节点输出dataset,row节点就用来显示dataset 图2-4 row节点input设置 要实现数据绑定,要选择output的数据类型并设置数据名称,要与数据库定义对应 图2-5 row节点output设置