基于MySQL数据库的C#与Python语言交互调用
2013-08-05 14:11
447 查看
由于工作需要了解了一些关于C#与Python语言的交互调用知识,以下程序代码的功能主要包括:
1.C#读取MySQL中的Blob字段,Blob字段中存的是Python脚本
2.在C#中执行Python脚本
3.Python脚本中又调用了C#函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronPython.Hosting;
using IronPython.Compiler;
using IronPython.Runtime;
using Microsoft.Scripting;
using System.Runtime.Remoting;
using Microsoft.Scripting.Hosting;
using System.IO;
using System.Collections;
using System.Data;
using MySql.Data.MySqlClient;
namespace ironpythonTest
{
public class Program
{
public static byte[][] buffer = new byte[5][];
static void Main(string[] args)
{
childClass temp = new childClass();
int i = 0;
while (i < 4)
{
byte[][] bytestr = ReadMySql();
Parse(bytestr[i], temp);
i++;
}
Console.ReadKey();
}
public static byte[][] ReadMySql()
{
MySqlConnection conn = null;
conn = new MySqlConnection("Server=192.168.10.79;Database=book;Uid=root;Pwd=asdQWE123");
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = conn;
cmd.CommandText = "Select * from test1";
conn.Open();
MySqlDataReader reader = cmd.ExecuteReader();
int j = 0;
while ((reader.Read())&&(j<4))
{
buffer[j]=(byte[])reader["TESTCONTENT"];
j++;
}
return buffer;
}
public static void Parse(byte[] byteArray, object obj)
{
string strExpression = System.Text.Encoding.Default.GetString(byteArray);
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
scope.SetVariable("object", obj);
scope.SetVariable("Math", new Mathmatic());
engine.CreateScriptSourceFromString(strExpression).Execute(scope);
}
}
public class Mathmatic
{
public int add(int a,int b)
{
return a+b;
}
}
public class CSharpClass
{
public bool test()
{
return true ;
}
public bool test1()
{
return false;
}
}
public class childClass : CSharpClass
{
public bool childTest()
{
return false;
}
public bool childTest1()
{
return true;
}
}
}
Python脚本的内容如下:
print object.test() and object.childTest()
print object.test1() or object.childTest1()
print add(2,3)
其中C#函数的功能特别简单,主要用于测试父类能否识别并调用子类的所有方法,如有需要可自行扩充。
需要安装IronPython-2.7.msi和MySQL Connector Net 6.7.4,从这个例子的学习中我了解到了fileStream、 MySqlConnection等类的使用,希望能帮到大家。
1.C#读取MySQL中的Blob字段,Blob字段中存的是Python脚本
2.在C#中执行Python脚本
3.Python脚本中又调用了C#函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronPython.Hosting;
using IronPython.Compiler;
using IronPython.Runtime;
using Microsoft.Scripting;
using System.Runtime.Remoting;
using Microsoft.Scripting.Hosting;
using System.IO;
using System.Collections;
using System.Data;
using MySql.Data.MySqlClient;
namespace ironpythonTest
{
public class Program
{
public static byte[][] buffer = new byte[5][];
static void Main(string[] args)
{
childClass temp = new childClass();
int i = 0;
while (i < 4)
{
byte[][] bytestr = ReadMySql();
Parse(bytestr[i], temp);
i++;
}
Console.ReadKey();
}
public static byte[][] ReadMySql()
{
MySqlConnection conn = null;
conn = new MySqlConnection("Server=192.168.10.79;Database=book;Uid=root;Pwd=asdQWE123");
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = conn;
cmd.CommandText = "Select * from test1";
conn.Open();
MySqlDataReader reader = cmd.ExecuteReader();
int j = 0;
while ((reader.Read())&&(j<4))
{
buffer[j]=(byte[])reader["TESTCONTENT"];
j++;
}
return buffer;
}
public static void Parse(byte[] byteArray, object obj)
{
string strExpression = System.Text.Encoding.Default.GetString(byteArray);
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
scope.SetVariable("object", obj);
scope.SetVariable("Math", new Mathmatic());
engine.CreateScriptSourceFromString(strExpression).Execute(scope);
}
}
public class Mathmatic
{
public int add(int a,int b)
{
return a+b;
}
}
public class CSharpClass
{
public bool test()
{
return true ;
}
public bool test1()
{
return false;
}
}
public class childClass : CSharpClass
{
public bool childTest()
{
return false;
}
public bool childTest1()
{
return true;
}
}
}
Python脚本的内容如下:
print object.test() and object.childTest()
print object.test1() or object.childTest1()
print add(2,3)
其中C#函数的功能特别简单,主要用于测试父类能否识别并调用子类的所有方法,如有需要可自行扩充。
需要安装IronPython-2.7.msi和MySQL Connector Net 6.7.4,从这个例子的学习中我了解到了fileStream、 MySqlConnection等类的使用,希望能帮到大家。
相关文章推荐
- 基于python智慧树刷课脚本在C#语言下的设计与实现
- C#中调用python脚本语言
- 4. Selenium2 自动化测试实战-基于Python语言-跨目录模块调用
- c#与python 交互(调用第三方库失败,自己奋战了三天以失败而结尾,但是还是有些干货)-如果有人弄出来请告诉我
- C#调用Python 脚本语言
- C、C++、python、Java、php、C#六种语言连接mysql数据库代码
- 1.Selenium2 自动化测试实战-基于Python语言-设置窗口大小、调用JS调整滚动条、截取图片
- android客户端与服务器交互数据(基于SAOP协议的远程调用标准,通过webservice可以将不同操作系统平台,不同语言,不同技术整合)
- atitit.js javascript 调用c# java php后台语言api html5交互的原理与总结p97
- 3.Selenium2 自动化测试实战-基于Python语言-模块调用
- C#调用Python语言
- Python脚本调用C#代码数据交互示例(hello world)
- 六种流行的语言大餐---C、C++、python、Java、php、C#你更喜欢哪一个呢?
- Python 语言 Oracle 数据库基于 SQLAlchemy + Flask + Ajax 的 Json 前后端分离开发博客系统遇到的坑汇总
- C# 调用C 语言
- C#下的webservcie 实现代码和 在vc和python下的调用实现(原创)
- C 语言调用python 脚本函数
- python与c#的交互模块pythonnet
- 异构语言交互访问 解决方案及例子(三)--利用JNA从Java调用C++
- 用C#基于WCF创建TCP的Service供Client端调用