C++: read SQL server data using System::Data::SqlClient;

2023-11-09

stdafx.h:

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
#pragma once
#using <mscorlib.dll>
#using <System.dll>
#using <System.Data.dll>
#using <System.Xml.dll>
// TODO: reference additional headers your program requires here

Form1.h

#pragma once
 
 
namespace SQLServer2008R2inCCLI {
 
    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    using namespace System::Configuration;
    using namespace ADODB;//c:\Program Files\Common Files\System\ADO\msado15.dll
 
    using namespace System::Data::SqlClient;
 
    /// <summary>
    /// Summary for Form1
    ///
    /// WARNING: If you change the name of this class, you will need to change the
    ///          'Resource File Name' property for the managed resource compiler tool
    ///          associated with all .resx files this class depends on.  Otherwise,
    ///          the designers will not be able to interact properly with localized
    ///          resources associated with this form.
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }
 
    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
            private: System::Windows::Forms::DataGridView^ dataGridView1;
        private: System::Windows::Forms::Label^ label1;
        private: System::Windows::Forms::ComboBox^ comboBoxTables;
        private: System::Windows::Forms::Button^ buttonOK;
        private: System::Windows::Forms::ToolTip^ toolTip1;
    private: System::ComponentModel::IContainer^  components;
    protected:
 
    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
 
 
#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->components = (gcnew System::ComponentModel::Container());
            this->dataGridView1 = (gcnew System::Windows::Forms::DataGridView());
            this->label1 = (gcnew System::Windows::Forms::Label());
            this->comboBoxTables = (gcnew System::Windows::Forms::ComboBox());
            this->buttonOK = (gcnew System::Windows::Forms::Button());
            this->toolTip1 = (gcnew System::Windows::Forms::ToolTip(this->components));
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->dataGridView1))->BeginInit();
            this->SuspendLayout();
            //
            // dataGridView1
            //
            this->dataGridView1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom)
                | System::Windows::Forms::AnchorStyles::Left)
                | System::Windows::Forms::AnchorStyles::Right));
            this->dataGridView1->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
            this->dataGridView1->Location = System::Drawing::Point(12, 44);
            this->dataGridView1->Name = L"dataGridView1";
            this->dataGridView1->ShowCellToolTips = false;
            this->dataGridView1->Size = System::Drawing::Size(493, 266);
            this->dataGridView1->TabIndex = 0;
            this->dataGridView1->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::dataGridView1_MouseDown);
            this->dataGridView1->DataError += gcnew System::Windows::Forms::DataGridViewDataErrorEventHandler(this, &Form1::dataGridView1_DataError);
            //
            // label1
            //
            this->label1->Location = System::Drawing::Point(12, 9);
            this->label1->Name = L"label1";
            this->label1->Size = System::Drawing::Size(65, 32);
            this->label1->TabIndex = 1;
            this->label1->Text = L"Select table to show:";
            //
            // comboBoxTables
            //
            this->comboBoxTables->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
            this->comboBoxTables->FormattingEnabled = true;
            this->comboBoxTables->Location = System::Drawing::Point(83, 12);
            this->comboBoxTables->Name = L"comboBoxTables";
            this->comboBoxTables->Size = System::Drawing::Size(165, 21);
            this->comboBoxTables->TabIndex = 2;
            //
            // buttonOK
            //
            this->buttonOK->Location = System::Drawing::Point(254, 12);
            this->buttonOK->Name = L"buttonOK";
            this->buttonOK->Size = System::Drawing::Size(56, 21);
            this->buttonOK->TabIndex = 3;
            this->buttonOK->Text = L"OK";
            this->buttonOK->UseVisualStyleBackColor = true;
            this->buttonOK->Click += gcnew System::EventHandler(this, &Form1::buttonOK_Click);
            //
            // toolTip1
            //
            this->toolTip1->AutoPopDelay = 5000;
            this->toolTip1->InitialDelay = 0;
            this->toolTip1->IsBalloon = true;
            this->toolTip1->ReshowDelay = 0;
            this->toolTip1->ToolTipIcon = System::Windows::Forms::ToolTipIcon::Warning;
            this->toolTip1->UseAnimation = false;
            this->toolTip1->UseFading = false;
            //
            // Form1
            //
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(517, 322);
            this->Controls->Add(this->buttonOK);
            this->Controls->Add(this->comboBoxTables);
            this->Controls->Add(this->label1);
            this->Controls->Add(this->dataGridView1);
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
            this->FormClosed += gcnew System::Windows::Forms::FormClosedEventHandler(this, &Form1::Form1_FormClosed);
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->dataGridView1))->EndInit();
            this->ResumeLayout(false);
 
        }
#pragma endregion
        String ^ServerName, ^DBName;
 
        SqlConnection ^conn;
        SqlDataAdapter ^adapter;
        DataTable ^dtMain;
 
    private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
                 ServerName = "GEOVINDU-PC\\GEOVIN";
                 DBName = "DuVehicle";
 
 
 
                 // create database if not exists
                 //try {
                    // ADODB::Connection="";//c:\Program Files\Common Files\System\ADO\msado15.dll
 
                                     // connect to server
                 // to database "master" to check if our database exists
                 // to create it if it isn't exists
                 //conn = gcnew SqlConnection("Server=" + ServerName + ";Database=DuVehicle;UID=sa;PWD=geovindu;");
                 //conn->Open();
                     //SqlCommand ^cmd = gcnew SqlCommand(String::Format("CREATE DATABASE [{0}] ON (" +
      //                                              "    NAME = {0}, " +
                        //                          "    FILENAME = '" + Application::StartupPath + "\\{0}.mdf'" +
      //                                              ");",
      //                                              DBName), conn);
      //               cmd->ExecuteNonQuery();
                     //delete cmd;
                /* }
                 catch (Exception ^ex) { }*/
 
 
                 // create table "Table 1" if not exists
                 //SqlCommand ^cmd = gcnew SqlCommand(String::Format(
     //                                 "IF NOT EXISTS (" +
     //                                 "    SELECT [name] " +
     //                                 "    FROM sys.tables " +
     //                                 "    WHERE [name] = '{0}'" +
     //                                 ") " +
     //                                 "CREATE TABLE [{0}] (" +
     //                                 "    id [INT] IDENTITY(1,1) PRIMARY KEY CLUSTERED, " +
     //                                 "    [text column] [TEXT] NULL, " +
     //                                 "    [int column] [INT] NULL " +
     //                                 ")",
     //                                 "Table 1"), conn);
     //          cmd->ExecuteNonQuery();
                 //delete cmd;
                 conn = gcnew SqlConnection("Server=" + ServerName + ";Database=DuVehicle;UID=sa;PWD=geovindu;");
                 conn->Open();
                 // get all tables from DB
                 DataTable ^dt = conn->GetSchema("Tables");
                 for (int i = 0; i < dt->Rows->Count; i++) {
                    if (dt->Rows[i]->ItemArray[dt->Columns->IndexOf("TABLE_TYPE")]->ToString() == "BASE TABLE") {
                        comboBoxTables->Items->Add(dt->Rows[i]->ItemArray[dt->Columns->IndexOf("TABLE_NAME")]->ToString());
                    }
                 }
                 delete dt;
             }
private: System::Void buttonOK_Click(System::Object^  sender, System::EventArgs^  e) {
             if (comboBoxTables->SelectedItem == nullptr) return;
 
             adapter = gcnew SqlDataAdapter("SELECT * FROM [" + comboBoxTables->SelectedItem->ToString() + "]", conn);
 
             gcnew SqlCommandBuilder(adapter);
 
             dtMain = gcnew DataTable();
             adapter->Fill(dtMain);
             dtMain->Columns["id"]->ReadOnly = true; // deprecate id field edit to prevent exceptions
             dataGridView1->DataSource = dtMain;
         }
private: System::Void Form1_FormClosed(System::Object^  sender, System::Windows::Forms::FormClosedEventArgs^  e) {
             if (adapter == nullptr) return;
 
             adapter->Update(dtMain);
         }
         // show tooltip (not intrusive MessageBox) when user trying to input letters into INT column cell
private: System::Void dataGridView1_DataError(System::Object^  sender, System::Windows::Forms::DataGridViewDataErrorEventArgs^  e) {
             if (dtMain->Columns[e->ColumnIndex]->DataType == Int64::typeid ||
                 dtMain->Columns[e->ColumnIndex]->DataType == Int32::typeid ||
                dtMain->Columns[e->ColumnIndex]->DataType == Int16::typeid)
            {
                Rectangle ^rectColumn;
                rectColumn = dataGridView1->GetColumnDisplayRectangle(e->ColumnIndex, false);
 
                Rectangle ^rectRow;
                rectRow = dataGridView1->GetRowDisplayRectangle(e->RowIndex, false);
 
                toolTip1->ToolTipTitle = "This field is for numbers only.";
                toolTip1->Show(" ",
                          dataGridView1,
                          rectColumn->Left, rectRow->Top + rectRow->Height);
 
                delete rectColumn;
                delete rectRow;
            }
         }
private: System::Void dataGridView1_MouseDown(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
             toolTip1->Hide(dataGridView1);
         }
};
}


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++: read SQL server data using System::Data::SqlClient; 的相关文章

  • 是否有与 posix_memalign 对应的 C++ 版本?

    当我打电话时posix memalign http man7 org linux man pages man3 posix memalign 3 html为类型的对象分配对齐的内存Foo在我的 C 代码中 我需要做一个reinterpret
  • C++ 维护子类对象的混合集合

    如果我在这里错过了一个相当基本的概念 我很抱歉 但我正在尝试弄清楚如何维护多个类类型的集合 所有类类型都派生自同一个父类 并且在检索它们时仍然可以访问它们的特定于子类的方法从集合中 作为上下文 我有一个基类 BaseClass 和许多类 例
  • 静态只读字符串数组

    我在我的 Web 应用程序中使用静态只读字符串数组 基本上数组有错误代码 我将所有类似的错误代码保存在一个数组中并检查该数组 而不是检查不同常量字符串中的每个错误代码 like public static readonly string m
  • 在c#中执行Redis控制台命令

    我需要从 Redis 控制台获取 客户端列表 输出以在我的 C 应用程序中使用 有没有办法使用 ConnectionMultiplexer 执行该命令 或者是否有内置方法可以查找该信息 CLIENT LIST是 服务器 命令 而不是 数据库
  • 为什么pow函数比简单运算慢?

    从我的一个朋友那里 我听说 pow 函数比简单地将底数乘以它的指数的等价函数要慢 例如 据他介绍 include
  • C++ 是否可以在 MacOS 上与 OpenMP 和 boost 兼容?

    我现在已经尝试了很多事情并得出了一些结论 也许 我监督了一些事情 但似乎我无法完成我想要的事情 问题是 是否有可能使用 OpenMP 和 boost 在 MacOS High Sierra 上编译 C 一些发现 如果我错了请纠正我 Open
  • 查找进程的完整路径

    我已经编写了 C 控制台应用程序 当我启动应用程序时 不使用cmd 我可以看到它列在任务管理器的进程列表中 现在我需要编写另一个应用程序 在其中我需要查找以前的应用程序是否正在运行 我知道应用程序名称和路径 所以我已将管理对象搜索器查询写入
  • 为什么在 WebApi 上下文中在 using 块中使用 HttpClient 是错误的?

    那么 问题是为什么在 using 块中使用 HttpClient 是错误的 但在 WebApi 上下文中呢 我一直在读这篇文章不要阻止异步代码 https blog stephencleary com 2012 07 dont block
  • 使用 LINQ to SQL 时避免连接超时的最佳实践

    我需要知道在 net 应用程序中使用 LINQ to SQL 时避免连接超时的最佳实践 特别是在返回时IQueryable
  • 告诉 Nancy 将枚举序列化为字符串

    Nancy 默认情况下在生成 JSON 响应时将枚举序列化为整数 我需要将枚举序列化为字符串 有一种方法可以通过创建来自定义 Nancy 的 JSON 序列化JavaScript 原始转换器 https github com NancyFx
  • 将 Long 转换为 DateTime 从 C# 日期到 Java 日期

    我一直尝试用Java读取二进制文件 而二进制文件是用C 编写的 其中一些数据包含日期时间数据 当 DateTime 数据写入文件 以二进制形式 时 它使用DateTime ToBinary on C 为了读取 DateTime 数据 它将首
  • 如何在 Qt 应用程序中通过终端命令运行分离的应用程序?

    我想使用命令 cd opencv opencv 3 0 0 alpha samples cpp cpp example facedetect lena jpg 在 Qt 应用程序中按钮的 clicked 方法上运行 OpenCV 示例代码
  • 为什么从字典中获取时会得到 Action<> 的克隆?

    我有以下字典 private Dictionary
  • 等待 IAsyncResult 函数直至完成

    我需要创建等待 IAsyncResult 方法完成的机制 我怎样才能做到这一点 IAsyncResult result contactGroupServices BeginDeleteContact contactToRemove Uri
  • String.Empty 与 "" [重复]

    这个问题在这里已经有答案了 可能的重复 String Empty 和 有什么区别 https stackoverflow com questions 151472 what is the difference between string
  • 将数组作为参数传递

    如果我们修改作为方法内参数传递的数组的内容 则修改是在参数的副本而不是原始参数上完成的 因此结果不可见 当我们调用具有引用类型参数的方法时 会发生什么过程 这是我想问的代码示例 using System namespace Value Re
  • 在 Windows Phone silverlight 8.1 上接收 WNS 推送通知

    我有 Windows Phone 8 1 silverlight 应用程序 我想使用新框架 WNS 接收通知 我在 package appxmanifest 中有
  • 使用 C 在 OS X 中获取其他进程的 argv

    我想获得其他进程的argv 例如ps 我使用的是在 Intel 或 PowerPC 上运行的 Mac OS X 10 4 11 首先 我阅读了 ps 和 man kvm 的代码 然后编写了一些 C 代码 include
  • Objective-C / C 给出枚举默认值

    我在某处读到过关于给枚举默认值的内容 如下所示 typedef enum MarketNavigationTypeNone 0 MarketNavigationTypeHeirachy 1 MarketNavigationTypeMarke
  • 是否可以在不连接数据库的情况下检索 MetadataWorkspace?

    我正在编写一个需要遍历实体框架的测试库MetadataWorkspace对于给定的DbContext类型 但是 由于这是一个测试库 我宁愿不连接到数据库 它引入了测试环境中可能无法使用的依赖项 当我尝试获取参考时MetadataWorksp

随机推荐

  • linux内核分析:进程通讯方式

    信号 一旦有信号产生 我们就有下面这几种 用户进程对信号的处理方式 1 执行默认操作 Linux 对每种信号都规定了默认操作 例如 上面列表中的 Term 就是终止进程的意思 Core 的意思是 Core Dump 也即终止进程后 通过 C
  • 解决M1处理器安装PS闪退问题Photoshop 2021 fo mac(支持最新M1芯片处理器款mac)

    去年苹果在2020年11月11日突然发布了搭载自研M1芯片处理器的最新款Mac 由于这次新版mac系列史无前例的采用arm架构的芯片 导致很多之前为旧版mac开发的软件安装后不兼容无法使用 这其中就包括著名的Adobe系列软件 之前很多刚买
  • ppocrlabel简单教学

    前言 给我们小白成员的快速上手ppocrlabel的指南 1 ppocr环境配置 建议是先创建一个虚拟环境 直接参考 https blog csdn net weixin 42708301 article details 119864744
  • HDMI的DDC是什么

    DDC 是什么 DDC Display Data Channel 显示数据通道 在 HDMI 协议中用于 Source 和 Sink 两端进行数据交换 通常是基于 I2C 标准的一套通讯机制 在实际使用过程中 Source 端的 HDMI
  • 前端自动化测试之葵花宝典

    作者 京东零售 杜兴文 首先聊一下概念 Web 前端自动化测试是一种通过编写代码来自动化执行 Web 应用程序的测试任务的方法 它通常使用 JavaScript 和测试框架 如 Selenium Appium 等 来实现 Web 前端自动化
  • IRQL 和 分页内存

    IRQL是Interrupt ReQuest Level 中断请求级别 一个由windows虚拟出来的概念 划分在windows下中断的优先级 这里中断包括了硬中断和软中断 硬中断是由硬件产生 而软中断则是完全虚拟出来的 处理器在一个IRQ
  • python中把list列表所有或者部分的数变成整数,或者浮点数,字符串等等

    第一种 简单形式列表中是数字型 list x 1624865249825 0 316 0 351 0 32 0 107 0 4 0 1 7187 2970 0 1 0 list y 5249825 4 0 925 0 3903 1 7187
  • STM32HAL库 (cubemx) 两个HC05蓝牙模块相互通信相关问题的解决 数组串口发送与接受的方法

    主要问题 1 蓝牙模块的连接问题 2 蓝牙模块的工作模式 3 CUBEMX 配置串口注意事项 4 两个模块数据传输异常 前言 因为最近都在做基于STM32 MPU6050的手势控制机器人 遇到了无线数据传输的问题 正好手上有几个蓝牙模块 就
  • Latex系列2---段落编写+标题编写+目录生成

    接着上一节的简单中文文本 这节阐述的是一篇小规模文章的编写 段落编写 分段 写文章少不了分段的情况 latex中如何分段 先看一段代码和效果图 在这里我们看到代码中对于文章的分段有两种方式 1 空行 2 使用 par 空格 的形式 对于空行
  • blender基础笔记

    1 下载与安装 官网下载 官网下载 setam下载 steam下载 个人推荐这个 方便 修改语言 左上角 edit preferences Interface Transtation Langlish 疲了 看图吧 懒得写了 2 基础操作
  • 源码看CoordinatorLayout.Behavior原理

    http blog csdn net qibin0506 article details 50377592 在上一篇博客CoordinatorLayout高级用法 自定义Behavior中 我们介绍了如何去自定义一个CoordinatorL
  • Java中变量的作用域详解

    作用域定义 字面解释 scope 域即一定范围内的较大的地方 顾名思义就是在一定的范围内起作用 大白话解释 父母在家的时候能控制你的玩与学习 出了家门说了也白说 老师在校的时候能够管理你的行为 出了学校你都想管管他 这就是说 不管什么样的指
  • 单表数据量达多少时才建议分库分表

    1 阿里巴巴开发手册建议是 推荐 单表行数超过500万行或者单表容量超过2GB 才推荐进行分库分表 说明 如果预计三年后的数据量根本达不到这个级别 请不要在创建表时就分库分表 2 实际情况还要根据机器的配置视情况而定 3 阿里巴巴开发手册下
  • oracle生成UUID

    oracle生成UUID uuid Universally Unique Identifier 全局唯一标识符 是指在一台机器上生成的数字 它保证对在同一时空中的所有机器都是唯一的 按照开放软件基金会 OSF 制定的标准计算 用到了以太网卡
  • leetcode题解:最长公共前缀

    leetcode题解 最长公共前缀 题目描述 编写一个函数来查找字符串数组中的最长公共前缀 如果不存在公共前缀 返回空字符串 示例 1 输入 flower flow flight 输出 fl 示例 2 输入 dog racecar car
  • [Docker]Elasticsearch启动报错:Format version is not supported

    如果之前安装过Elasticsearch 安装新的Elasticsearch之前需要清空宿主机器对应的挂载目录下的文件数据
  • 打百万拳,走万里路。

    自我介绍 CSDN的大家你们好啊 我是一名大一的学生 与CSDN的相识还要从大一一次次查找知识点开始 当时由于刚接触编程 做什么都一头雾水而又不想去问老师那些简单的知识 于是自己在网上查找 就发现了CSDN这个大学生聚集地 由于很多都是和我
  • Python单重循环练习题

    第一次学python 求大佬指正 1 有1020个西瓜 第一天卖掉总数的一半后又多卖出两个 以后每天卖剩下的一半多两个 问几天以后能卖完 8天后能卖完 sum 1020 day 0 while sum gt 0 day 1 sum sum
  • 解决tomcat 启动超过45秒时间限制

    当在eclipse运行一个javaweb项目时 出现了如下图片中的问题 解决方法 1在如下页面中找到Servers 找不到的话可以通过Window gt gt Show View放到下方 2 双击Servers进到如下页面 3 打开箭头所指
  • C++: read SQL server data using System::Data::SqlClient;

    stdafx h stdafx h include file for standard system include files or project specific include files that are used frequen