简体   繁体   中英

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. 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? I assume it's some form of SQL, but am unsure of what. Specifically, does the default type support 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? 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.

Any help would be greatly appreciated!

First, as usual, we use sqlserver localdb in visual studio. Of course, it supports 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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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