您的位置:首页 > 编程语言 > Python开发

基于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等类的使用,希望能帮到大家。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐