当前位置:网站首页>C # connection to database

C # connection to database

2022-06-23 17:12:00 Hua doesn't care

One 、 Components used in the connection

(1) A button "Button"
(2) One “DataGridView”( Used to store forms transferred from the database )

Two 、 Connection code

(1)form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;// The header file ( Be careful to write )
using System.Windows.Forms;

//Author dahua
//Data by 2022.6.23
namespace WindowsFormsApp1
{
    
    public partial class Form1 : Form
    {
    
        public Form1()
        {
    
            InitializeComponent();
        }

        private void Btnconn_Click(object sender, EventArgs e)
        {
    
            // Instantiate objects 
            SqlConnection con = new SqlConnection();
            // Generate connection string 
            //string connectString = "Data Source=.\\LAPTOP-F8H1G1BE; database=Q;User ID=root;password=963456";
            // Read configuration file 
            string connectString = System.Configuration.ConfigurationSettings.AppSettings["SQLconnectString"];
            connectString=System.Configuration.ConfigurationManager.AppSettings["SQLconnectString"];
            con.ConnectionString = connectString;
            // Establishing a connection 
            try
            {
    
                con.Open();

                // Prompt whether the connection is successful 
                if (con.State == ConnectionState.Open)
                {
    
                    MessageBox.Show(" Database connection successful !");
                }
            }
            catch
            {
    
                MessageBox.Show(" Database connection failed !");
            }
            // Create objects 
            SqlCommand sqlcommand = new SqlCommand();
            sqlcommand.Connection = con;
            sqlcommand.CommandText = "select * from [dbo].[student]";
            // perform Sql Command and return the result to the interface IDataReader type ider in 
            IDataReader idr = sqlcommand.ExecuteReader();
            DataTable dt = new DataTable();
            // take idr Fill in the data in DataTable type dt in 
            dt.Load(idr);
            dataGridView1.DataSource = dt.DefaultView;
            // Close the connection 
            idr.Close();
            con.Close();
            if (connectString != null)
            {
    
                con.Close();
            }
        }
    }
}

(2) App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
  <appSettings>
    // Here's what I'm using sql Authentication login ( Select on demand )
    <add key="SQLconnectString1" value="Data Source=.\LAPTOP-F8H1G1BE; database=master;User ID=user1;password=;"/>
    <add key="OrleconnectString" value="Data Source=.\LAPTOP-F8H1G1BE; database=Q;User ID=root;password=xxxxx"/>
    // What's used here is window Authentication login 
    <add key="SQLconnectString" value="server=.;database=master;integrated security=SSPI"/>
    
  </appSettings>
</configuration>

3、 ... and 、 Running results

(1) Database connection success result chart  Please add a picture description
(2) Database connection to the result graph of the form
 Please add a picture description
【 notes 】 Use App.config The reason why the configuration file places the database connection is
(1) Convenient will “.exe” After the file is packaged and sent to the user , Users on their own computers When the configuration , You can directly modify the user name and password of the database , You can use this file directly .
(2) Sometimes you need to connect to multiple databases , Put the database connection in the configuration file , It is convenient for unified management or modification in the later stage .

原网站

版权声明
本文为[Hua doesn't care]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231622149815.html

随机推荐