简体   繁体   English

如何创建一个简单的Windows窗体来访问SQL Server数据库?

[英]How do I create a simple Windows form to access a SQL Server database?

I believe this is a very novice question, and if I'm using the wrong forum to ask, please advise. 我相信这是一个非常新手的问题,如果我使用错误的论坛询问,请告知。

I have a basic understanding of databasing with MS SQL Server, and programming with C++ and C#. 我对使用MS SQL Server进行数据库的基本了解,以及使用C ++和C#进行编程。 I'm trying to teach myself more by setting up my own database with MS SQL Server Express 2008 R2 and accessing it via Windows forms created in C# Express 2010. At this point, I just want to keep it to free or Express dev tools (not necessarily Microsoft though). 我正在尝试通过使用MS SQL Server Express 2008 R2建立自己的数据库并通过在C#Express 2010中创建的Windows表单访问它来自学更多。此时,我只想将其保留为免费或Express开发工具(不一定微软虽然)。

Anyway, I created a database using the instructions provided here and I set the data types appropriately for each column (no errors in setup at least). 无论如何,我使用这里提供的指令创建了一个数据库并且我为每列适当地设置了数据类型(至少在设置中没有错误)。

Now I'm designing the GUI in C# Express but I've kind of hit a wall as far as the database connection. 现在,我正在使用C#Express设计GUI,但就数据库连接而言,我有点碰壁。

Is there a simple way to access the database I created locally using C# Express? 有没有一种简单的方法来访问我使用C#Express在本地创建的数据库? Can anyone suggest a guide that has all this spelled out already? 任何人都可以建议一个已经说明了这一切的指南吗?

I am a self-learner so I look forward to teaching myself how to use these applications, but any pointers to start me off in the right direction would be greatly appreciated. 我是一个自学者,所以我期待自己教会如何使用这些应用程序,但是任何能够指引我朝正确方向前进的指针都将非常感激。

I'd start by taking a look at this tutorial: 我首先来看看这个教程:

http://msdn.microsoft.com/en-us/library/ms178371.aspx http://msdn.microsoft.com/en-us/library/ms178371.aspx

The basic ideas is to create a connection string that will define a number of things, including: where your db server is, what the database name is, and how you are connecting to it. 基本思想是创建一个连接字符串 ,它将定义许多内容,包括:数据库服务器所在的位置,数据库名称以及连接方式。

Once you have your connection string defined you will probably use the SqlConnection , SqlCommand and SqlDataReader classes for all interactions with the database. 一旦定义了连接字符串,就可以使用SqlConnectionSqlCommandSqlDataReader类进行与数据库的所有交互。 If you click on the links for those three classes and look at the documentation you will see they have pretty good examples. 如果单击这三个类的链接并查看文档,您将看到它们有很好的示例。

You probably want to use old school ADO.NET if you are more comfortable in SQL than C#. 如果您在SQL中比C#更舒服,您可能想要使用旧式ADO.NET。

MSDN intro to ADO.NET MSDN介绍ADO.NET

Far and away the easiest way to do this is with LINQ in Visual Studio 最简单的方法是使用Visual Studio中的LINQ

You might want to check out this tutorial which will guide you through all the steps you need. 您可能需要查看本教程,它将指导您完成所需的所有步骤。

http://www.codegod.de/WebAppCodeGod/tutorial-linq-to-sql---part-1-AID466.aspx http://www.codegod.de/WebAppCodeGod/tutorial-linq-to-sql---part-1-AID466.aspx

its simple 这很简单

use following code 使用以下代码

SqlConnection con = 
 new SqlConnection(@"server=.\SQLEXPRESS;AttachDbFilename=path of database file;Integrated Security=True;User Instance=True");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建数据库并为Windows Mobile 5应用程序访问数据库? - How do i create a database and access it for my windows mobile 5 app? 如何在不实际使用数据库软件(例如Access / local SQL)的情况下创建简单数据库? - How can I create a simple database without actually using a database software (like Access / local SQL)? 如何以编程方式创建Windows窗体? - How do I programmatically create a windows form? 如何在简单的 Web 项目中连接到 .mdf(Microsoft SQL Server 数据库文件)? - How do I connect to an .mdf (Microsoft SQL Server Database File) in a simple web project? 如何在不安装 SQL 服务器工作室的情况下运行 windows 表单应用程序,但从它的数据库(服务器)访问实时数据 - How to run a windows form application without instailling SQL server studio but access live data from it's database(Server) 如何在没有物理访问的情况下将SQL Server数据库“安装”到远程服务器? - How do I “Install” a SQL Server Database to a Remote Server Without Physical Access? C#Windows窗体如何将Datagridview到数据库(SQL Server)? - C# Windows Form How to Datagridview to Database (Sql Server)? 如何在另一台计算机上运行带有SQL Server数据库的Windows窗体应用程序 - How to Run Windows Form Application with SQL Server Database on Another Computer 如何在启动时同步Windows服务对SQL Server数据库的访问? - How to Synchronize Windows Service Access to SQL Server Database on Startup? 如何创建具有SQL连接和查询功能的类并将其调用到Windows窗体按钮? - How do I create a class with SQL connection and query functions and calling it to my windows form buttons?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM