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);
}
};
}

  

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

  1. asp.net编译中出现 数据库 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\test1.mdf' 已存在。请选择其他数据库名称。

    关于asp.net编译中出现数据库 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\test1.mdf ...

  2. 数据库 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\test1.mdf' 已存在。请选择其他数据库

    关于asp.net编译中出现 数据库 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\test1.md ...

  3. 文件 "c:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\ttt.mdf" 已压缩,但未驻留在只读数据库或文件组中。必须将此文件解压缩。 CREATE DATABASE 失败。无法创建列出的某些文件名。请查看相关错误。 (.Net SqlClient Data Provider)

    问题: 文件 "c:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\ttt.mdf" 已压缩,但 ...

  4. 元数据管理器中存在错误。 实例化来自文件“\\?\C:\Program Files\Microsoft SQL Server\MSAS11.MSSQLSERVER\OLAP\Data\Tfs_Analysis.0.db\vDimTestCaseOverlay.874.dim.xml”的元数据对象时出错。

    一.发现问题 启动SQLSERVER的数据分析服务失败 查看系统日志错误如下: 双击错误后显示详细错误: 元数据管理器中存在错误. 实例化来自文件“\\?\C:\Program Files\Micro ...

  5. Importing/Indexing database (MySQL or SQL Server) in Solr using Data Import Handler--转载

    原文地址:https://gist.github.com/maxivak/3e3ee1fca32f3949f052 Install Solr download and install Solr fro ...

  6. Data Base System.Data.OracleClient requires Oracle client software version 8.1.7 or greater解决方案

    System.Data.OracleClient requires Oracle client software version 8.1.7 or greater解决方案 一.问题: 1.通过Syst ...

  7. sql server 里面的 dynamic Data Masking

    有时候啊,当我们存放在数据库里面的数据,需要明文存放,但是对于前台查询出来的,又不太希望明文放出去的时候(比方说客户的手机啊,邮箱啊)之类有点敏感的信息,之前通常是保存个明文,然后在前台展现的时候再特 ...

  8. at System.Data.EntityClient.EntityConnection.GetFactory(String providerString)

    最近在做一个WinForm的项目. 使用vs2013开发. 数据库使用的是oracle. 在本地写了一个webservice .测试正常.发布到服务器的时候.就是提示了错误. 打开服务器上的日志.看到 ...

  9. VS2010 使用 EntityFramework For SQL Server Compact 4.0

    1.安装 SSCERuntime_x64-ENU.exe 或 SSCERuntime_x86-ENU.exe 2.安装 SSCEVSTools-CHS.msi 3.然后就可以通过 SQL Server ...

随机推荐

  1. js里slice,substr和substring的区别

    概要: string.slice(start, end)提取一个字符串 string.substring(start, end)提取一个字符串,end不支持负数 string.substr(start ...

  2. Java程序员的日常 —— 注册工厂的妙用

    注册工厂是一种很常用的框架书写方法,它适合于快速创建相同类型的对象. 举个栗子 比如一个家具工厂,有沙发.椅子.茶几等等,正常的编程模式是这样的: //创建 class 沙发{} class 椅子{} ...

  3. 为什么说基于TCP的移动端IM仍然需要心跳保活?

    1.前言 很多人认为,TCP协议自身先天就有KeepAlive机制,为何基于它的通讯链接,仍然需要在应用层实现额外的心跳保活?本文将从移动端IM实践的角度告诉你,即使使用的是TCP协议,应用层的心跳保 ...

  4. DBCC SHOW_STATISTICS 查看统计信息

    使用DBCC Show_Statistics 能够查看 表或Indexed view上的统计信息.Query optimizer使用统计信息进行estimate,生成高质量的qeury plan.统计 ...

  5. 深入理解CSS定位中的堆叠z-index

    × 目录 [1]定义 [2]堆叠规则 [3]堆叠上下文[4]兼容 前面的话 对于所有定位,最后都不免遇到两个元素试图放在同一位置上的情况.显然,其中一个必须盖住另一个.但,如何控制哪个元素放在上层,这 ...

  6. 【目录】Newlife XCode组件相关文章目录

    本博客所有文章分类的总目录链接:本博客博文总目录-实时更新  1.Newlife XCode组件相关文章目录  1.Newlife XCode组件资源目录汇总[2013年版]    2.Newlife ...

  7. 邻接矩阵有向图(三)之 Java详解

    前面分别介绍了邻接矩阵有向图的C和C++实现,本文通过Java实现邻接矩阵有向图. 目录 1. 邻接矩阵有向图的介绍 2. 邻接矩阵有向图的代码说明 3. 邻接矩阵有向图的完整源码 转载请注明出处:h ...

  8. 使用office制作图章公章

    制作公章的软件非常多,随便到网上一搜就有成千成百的软件或小工具,常用的有PS.Coreldraw.Ai.Word等,拥有一款office可以使用word来制作,方法挺简单,功能挺强大.寥寥数笔难以形容 ...

  9. Linux:文件权限

    Linux:文件权限 1.文件权限 ☆文件所有者 ☆用户组 ☆其他人 ☆ROOT 说明: Linux系统中默认所有系统上的账号与一般身份用户,还有那个root的相关信息记录在/etc/passwd文件 ...

  10. HTML5第二节

    第二回合:HTML5的新特性 与之前的HTML4.01相比,HTML5增加了非常多的改变: ① 新的语义元素:<article>.<aside>.<figure>. ...