简体   繁体   English

如何在Visual Studio 2017中使用数据库

[英]How to use a database in Visual Studio 2017

I'm a novice programmer, and I'm working with somebody to create a simple application in Visual Studio 2017 Community that uses a local embedded database for storing data. 我是一名新手程序员,并且正在与某人合作在Visual Studio 2017社区中创建一个简单的应用程序,该应用程序使用本地嵌入式数据库存储数据。 We've been having a lot of trouble getting a database to work, so I wanted to ask a few questions so that we could stop spinning our wheels. 要使数据库正常工作,我们一直遇到很多麻烦,所以我想问几个问题,以便我们停止运转。

  1. What's the specific type of database that Visual Studio uses by default? 默认情况下,Visual Studio使用什么特定类型的数据库? I assume it's some form of SQL, but am unsure of what. 我认为这是某种形式的SQL,但不确定是什么。 Specifically, does the default type support Upsert? 具体来说,默认类型是否支持Upsert?

  2. What is the exact process for connecting a database to a DataGridView so that it can be queried and its table data viewed? 将数据库连接到DataGridView以便查询数据库和查看其表数据的确切过程是什么? I've tried cobbling this information together from various tutorials on the internet, but to no success, and Microsoft's documentation is pretty terrible in this regard. 我曾尝试从互联网上的各种教程中收集这些信息,但没有成功,在这方面,Microsoft的文档非常糟糕。

Any help would be greatly appreciated! 任何帮助将不胜感激!

First, as usual, we use sqlserver localdb in visual studio. 首先,像往常一样,我们在Visual Studio中使用sqlserver localdb。 Of course, it supports Upsert. 当然,它支持Upsert。

Second, you can try the following code to connect database and select data from the table. 其次,您可以尝试使用以下代码连接数据库并从表中选择数据。

  public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string connectionstring = @"connectionstring";
            SqlConnection connection = new SqlConnection(connectionstring);
            connection.Open();
            string sql = "select * from Student";
            DataSet dataSet = new DataSet();
            SqlDataAdapter adapter = new SqlDataAdapter(sql, connection);
            adapter.Fill(dataSet);
            connection.Close();
            dataGridView1.DataSource = dataSet.Tables[0];
        }
    }

Result: 结果:

在此处输入图片说明

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM