• 大小: 686KB
    文件类型: .zip
    金币: 2
    下载: 4 次
    发布日期: 2021-11-18
  • 语言: C#
  • 标签: 西门子PLC  

资源简介

C#和西门子PLC以太网通讯,上位机代码

资源截图

代码片段和文件信息

using System;
using System.Data;
using System.Configuration;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClientTest
{
    /// 
    /// AccessHelper 的摘要说明
    /// 

    class AccessHelper
    {
        #region 变量
        public  OleDbConnection conn = new OleDbConnection();
        public  OleDbCommand comm = new OleDbCommand();
        public  string connectionString = @““;
        #endregion

        #region 构造函数
        /// 
        /// 构造函数
        /// 

        public AccessHelper()
        {

        }
        #endregion

        #region 打开数据库
        /// 
        /// 打开数据库
        /// 

        private  void openConnection()
        {
            if (conn.State == ConnectionState.Closed)
            {
                conn.ConnectionString = connectionString;
                comm.Connection = conn;
                try
                {
                    conn.Open();
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }
        #endregion

        #region 关闭数据库
        /// 
        /// 关闭数据库
        /// 

        private void closeConnection()
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
                conn.Dispose();
                comm.Dispose();
            }
        }
        #endregion

        #region 执行sql语句
        /// 
        /// 执行sql语句
        /// 

        public void ExecuteSql(string sqlstr)
        {
            try
            {
                openConnection();
                comm.CommandType = CommandType.Text;
                comm.CommandText = sqlstr;
                comm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                closeConnection();
            }
        }
        #endregion

        #region 返回指定sql语句的OleDbDataReader对象使用时请注意关闭这个对象。
        /// 
        /// 返回指定sql语句的OleDbDataReader对象使用时请注意关闭这个对象。
        /// 

        public  OleDbDataReader DataReader(string sqlstrstring nconnectionStr)
        {
            OleDbDataReader dr = null;
            try
            {
                connectionString = nconnectionStr;
                openConnection();
                comm.CommandText = sqlstr;
                comm.CommandType = CommandType.Text;

                dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch
            {
                try
                {
                    dr.Close();
                    closeConnection();
                

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-04-13 15:35  suSocket2017_4_13\
     目录           0  2017-04-13 15:35  suSocket2017_4_13\ClientTest\
     文件        9525  2017-04-13 10:50  suSocket2017_4_13\ClientTest\AccessHelper.cs
     文件         189  2017-04-11 15:25  suSocket2017_4_13\ClientTest\App.config
     目录           0  2017-04-13 15:35  suSocket2017_4_13\ClientTest\bin\
     目录           0  2017-04-13 15:35  suSocket2017_4_13\ClientTest\bin\Debug\
     文件       23040  2017-04-13 15:20  suSocket2017_4_13\ClientTest\bin\Debug\ClientTest.exe
     文件         189  2017-04-11 15:25  suSocket2017_4_13\ClientTest\bin\Debug\ClientTest.exe.config
     文件       36352  2017-04-13 15:20  suSocket2017_4_13\ClientTest\bin\Debug\ClientTest.pdb
     文件       22696  2017-04-12 17:10  suSocket2017_4_13\ClientTest\bin\Debug\ClientTest.vshost.exe
     文件         189  2017-04-11 15:25  suSocket2017_4_13\ClientTest\bin\Debug\ClientTest.vshost.exe.config
     文件         490  2016-07-16 19:44  suSocket2017_4_13\ClientTest\bin\Debug\ClientTest.vshost.exe.manifest
     文件     1253376  2017-04-13 15:23  suSocket2017_4_13\ClientTest\bin\Debug\CurceData.mdb
     文件       33280  2017-04-13 15:01  suSocket2017_4_13\ClientTest\bin\Debug\DynamicCurce.dll
     文件       58880  2017-04-13 15:01  suSocket2017_4_13\ClientTest\bin\Debug\DynamicCurce.pdb
     文件        6656  2017-04-13 15:15  suSocket2017_4_13\ClientTest\bin\Debug\SocketClient.dll
     文件       13824  2017-04-13 15:15  suSocket2017_4_13\ClientTest\bin\Debug\SocketClient.pdb
     文件      253952  2017-04-13 09:45  suSocket2017_4_13\ClientTest\bin\Debug\员工信息.mdb
     目录           0  2017-04-13 10:00  suSocket2017_4_13\ClientTest\bin\Release\
     文件        4544  2017-04-13 14:41  suSocket2017_4_13\ClientTest\ClientTest.csproj
     目录           0  2017-04-13 15:35  suSocket2017_4_13\ClientTest\obj\
     目录           0  2017-04-13 15:35  suSocket2017_4_13\ClientTest\obj\Debug\
     文件        3472  2017-04-13 15:20  suSocket2017_4_13\ClientTest\obj\Debug\ClientTest.csproj.FileListAbsolute.txt
     文件        1017  2017-04-13 15:20  suSocket2017_4_13\ClientTest\obj\Debug\ClientTest.csproj.GenerateResource.Cache
     文件       10533  2017-04-13 15:20  suSocket2017_4_13\ClientTest\obj\Debug\ClientTest.csprojResolveAssemblyReference.cache
     文件       23040  2017-04-13 15:20  suSocket2017_4_13\ClientTest\obj\Debug\ClientTest.exe
     文件       36352  2017-04-13 15:20  suSocket2017_4_13\ClientTest\obj\Debug\ClientTest.pdb
     文件        1942  2017-04-13 15:20  suSocket2017_4_13\ClientTest\obj\Debug\ClientTest.plcSocketTest.resources
     文件         180  2017-04-13 15:20  suSocket2017_4_13\ClientTest\obj\Debug\ClientTest.Properties.Resources.resources
     文件        1464  2017-04-12 11:16  suSocket2017_4_13\ClientTest\obj\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        7270  2017-04-13 15:01  suSocket2017_4_13\ClientTest\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
............此处省略157个文件信息

评论

共有 条评论