当前位置:职场发展 > vs2019 prewview评估期已过_C#SQL操作学习记录001——VS2019连接数据库查询数据并通过dataGridView...

vs2019 prewview评估期已过_C#SQL操作学习记录001——VS2019连接数据库查询数据并通过dataGridView...

  • 发布:2023-09-23 13:48

一、环境

工具:VS2019

项目类型:C# Windows 窗体应用(.NET Framework)

框架:.NET Framework 4

控件:窗体、按钮(Button)、dataGridView

功能简述:点击按钮,连接数据库并查询数据库内的数据,将查询结果返回并通过dataGridView

显示出来

界面:(后面背景一样的大窗体是设计时候的窗体,前面的小窗体是实际运行时候的窗体)

参考链接:vs2013 c#连接mysql数据库并显示查询结果到DataGridView上

? ? ? ? ? ? ? ? ? C#中连接使用Windows身份验证的sql server数据库

二、详细步骤

前面创建“C# Windows 窗体应用(.NET Framework)”的过程此处省略,没有什么要注意的地方。然后,在窗体上放一个”Button“和一个“dataGridView”,类似我前面给的界面图。接下来......

(1)加入连接SQLServer 数据库要用到的引用集

using System.Data.SqlClient;

(2)双击 按钮,编写按钮的动作,如下图所示

private void button2_Click(object sender, EventArgs e)

{

string ConStr = "server=CAM-01\\SQLDEMO;database=SWManager;Trusted_Connection=SSPI";

string SqlStr = string.Format("select * from obj120"); //引号内是数据库查询的语句

SqlDataAdapter adapter = new SqlDataAdapter(SqlStr, ConStr);

DataTable DTable = new DataTable(); //创建数据集对象

adapter.Fill(DTable); //用返回的结果集填充数据集

this.dataGridView1.DataSource = DTable;

}

数据库的连接那块我是用“Windows身份验证” 方式连接的数据库:

参考链接:C#中连接使用Windows身份验证的sql server数据库

string connString = "server=CAM-01\\SQLDEMO;database=GroupInfo;Trusted_Connection=SSPI";

(3)运行

?三、附言

关于数据库连接的详细说明:c#连接sql数据库并调用

数据库常用语句:数据库基础(常用SQL语句)

??提示:如果VS要使用报表功能,在VS下需要安装两种插件,分别是Microsoft RDLC Report Designer(报表设计器)和ReportView

? ? ??目前经常小伙伴们会遇到安装ReportView后,虽然工具箱中显了ReportViewer,但拖动或双击到窗体使用时,却没有反应,反而在下方窗体显示ReportViewer1。

? ? ??出现这种情况的原因,多数应该是目前安装的版本有兼容性问题导致的。 ? ? ??平常小伙伴们应该都是按网上的教程安装的,如: ? ? ??执行命令:Install-Package Microsoft.ReportingServices.ReportViewerControl.WinForms -Pre ? ? ??这正是问题所在,因为到目前为止这种安装的方式,安装的版本是不能和VS2019兼容的,它会自动安装最新的版本。

? ? ??这个有问题的版本为:Microsoft.ReportingServices.ReportViewerControl.Winforms.150.1484.0(安装时间点:2021-12-30)

解决的办法如下:

? ? ? 安装低版本能兼容的即可。

? ? ??具体安装方法: ? ? ??? ? ??1、打开vs2019,选择“工具” >>“ Nuget包管理器” >> “程序包管理器控制台” ? ? ??? ? ??2、执行命令:Install-Package Microsoft.ReportingServices.ReportViewerControl.Winforms?-Version 140.340.80 ? ? ??? ? ??3、在工具箱中点击右键,选择“选择项” ,(任意位置右击即可) ? ? ??? ? ??4、在.net framework组建选项卡中选择“浏览”,加载DLL ? ? ??? ? ??5、在你创建的项目或工程根目录下找到 “\packages\Microsoft.ReportingServices.ReportViewerControl.Winforms.140.340.80\lib\net40” 选择“Microsoft.ReportViewer.WinForms.dll”。 ? ? ??? ? ??6、在VS2019工具箱-->组件可看到ReportViewer控件

? ? ??经上述步骤设置后,即可到在窗体工具栏上正常使用ReportView控件。

  ReportView用到的引用:

  Microsoft.ReportViewer.Common

  Microsoft.ReportViewer.DataVisualization

  www.sychzs.cn

  Microsoft.ReoprtViewer.ProcessingObjectModel

  Microsoft.ReoprtViewer.WinForms

? ? ??测试环境 ? ? ??系统:Win10x64 21H2 ? ? ??IDE:Visual Studio 2019 16.11.8 ? ? ??目标架框:.Net Framework 4.6.1 ? ? ??扩展:Microsoft RDLC Report Designer 15.3.1

? ? ?目前的注意问题: ? ? ? ?1.140版本控件可以在窗体上显示,但生成报表后报表显示不居中。 ? ? ? ?2.150版本控件不能在窗体上显示,但生成报表后报表居中显示。

首先,在CxxxView类中添加一个CButton成员m_btn (xxx表示你创建的工程名)

?

其次,右键View类>>属性,如下图选择Windows Message 添加OnCreate函数

?

然后,在OnCreate函数中添加button

(注:如果创建button函数Create中使用了WS_VISIBLE那么可以不适用ShowWindow函数)

(注:图中使用了GetParent 函数,说明创建的button还是在框架类范围,改为this 后才是真正的使button显示在视窗范围内,读者自己改下即可)

运行,看到添加的button显示在视类窗口范围,(根据自己的需要调整button的大小及位置)

?

重点:关于CButton类提供的成员函数Create 和CButton的父类CWnd提供的ShowWindow说明如下:

Create Creates the Windows button control and attaches it to the CButton object. ?

?

CButton::CreateExample? See Also? Send Feedback

?

?

Creates the Windows button control and attaches it to the CButton object.

? virtual BOOL Create(

LPCTSTR lpszCaption,

DWORD dwStyle,

const RECT& rect,

CWnd* pParentWnd,

UINT nID

);

Parameters

lpszCaption

Specifies the button control's text.

dwStyle

Specifies the button control's style. Apply any combination of button styles to the button.

rect

Specifies the button control's size and position. It can be either a CRect object or a RECT structure.

pParentWnd

Specifies the button control's parent window, usually a CDialog. It must not be NULL.

nID

Specifies the button control's ID.

Return Value

Nonzero if successful; otherwise 0.

Remarks

You construct a CButton object in two steps. First, call the constructor and then call Create, which creates the Windows button control and attaches it to the CButton object.

If the WS_VISIBLE style is given, Windows sends the button control all the messages required to activate and show the button.

Apply the following window styles to a button control:

WS_CHILD???Always WS_VISIBLE???Usually WS_DISABLED???Rarely WS_GROUP???To group controls WS_TABSTOP???To include the button in the tabbing order

Example

Visual C++?Copy Code CButton myButton1, myButton2, myButton3, myButton4;

// Create a push button.

myButton1.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,

CRect(10,10,100,30), pParentWnd, 1);

// Create a radio button.

myButton2.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_RADIOBUTTON,

CRect(10,40,100,70), pParentWnd, 2);

// Create an auto 3-state button.

myButton3.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_AUTO3STATE,

CRect(10,70,100,100), pParentWnd, 3);

// Create an auto check box.

myButton4.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX,

CRect(10,100,100,130), pParentWnd, 4);

MFC Library ReferenceCWnd::ShowWindowExample? See Also? Send Feedback

?

?

Sets the visibility state of the window.

? BOOL ShowWindow(

int nCmdShow

);

Parameters

nCmdShow

Specifies how the CWnd is to be shown. It must be one of the following values:

SW_HIDE???Hides this window and passes activation to another window. SW_MINIMIZE???Minimizes the window and activates the top-level window in the system's list. SW_RESTORE???Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position. SW_SHOW???Activates the window and displays it in its current size and position. SW_SHOWMAXIMIZED???Activates the window and displays it as a maximized window. SW_SHOWMINIMIZED???Activates the window and displays it as an icon. SW_SHOWMINNOACTIVE???Displays the window as an icon. The window that is currently active remains active. SW_SHOWNA???Displays the window in its current state. The window that is currently active remains active. SW_SHOWNOACTIVATE???Displays the window in its most recent size and position. The window that is currently active remains active. SW_SHOWNORMAL???Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position.

Return Value

Nonzero if the window was previously visible; 0 if the CWnd was previously hidden.

Remarks

ShowWindow must be called only once per application for the main window with CWinApp::m_nCmdShow. Subsequent calls to ShowWindow must use one of the values listed above instead of the one specified by CWinApp::m_nCmdShow.

Example

Visual C++?Copy Code // Uses CalcWindowRect to determine size for new CFrameWnd

// based on the size of the current view. The end result is a

// top level frame window of the same size as CMdiView's frame.

void CMdiView::OnMyCreateFrame()

{

CFrameWnd* pFrameWnd = new CFrameWnd;

CRect myRect;

GetClientRect(myRect);

pFrameWnd->Create(NULL, _T("My Frame"));

pFrameWnd->CalcWindowRect(&myRect, CWnd::adjustBorder);

pFrameWnd->MoveWindow(0, 0, myRect.Width(), myRect.Height());

pFrameWnd->ShowWindow(SW_SHOW);

}

?

相关文章