zoukankan      html  css  js  c++  java
  • 连接SQL Server数据库

    To achieve this requirement, you need to create a "connectionstring" to connect them. Here is a simple demo that read data to "DataGridView".

    private void Form1_Load(object sender, EventArgs e)
    {
        // builder connection string
        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
        // set server
        builder.DataSource = @"datasource name";
        // set database
        builder.InitialCatalog = @"catalog name";
        // access the database using the existing windows security certificate
        builder.IntegratedSecurity = true;
    
        //// if you want to use user id and password
        //// set user name
        //builder.UserID = "Kyle";
        //// set password
        //builder.Password = "123456";
    
        using (SqlConnection sqlconn = new SqlConnection(builder.ConnectionString))
        {
            SqlCommand sqlcomm = new SqlCommand("select * from Table_1", sqlconn);
            SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlcomm);
            DataSet ds = new DataSet();
            sqlAdapter.Fill(ds, "Table_1");
            dataGridView1.DataSource = ds.Tables["Table_1"];
        }
    }

    Properties in SQL Server:

    Result:

  • 相关阅读:
    384. 最长无重复字符的子串
    406. 和大于S的最小子数组
    159. 寻找旋转排序数组中的最小值
    62. 搜索旋转排序数组
    20. 骰子求和
    125. 背包问题 II
    92. 背包问题
    1295. 质因数统计
    471. 最高频的K个单词
    1339. 最大区间
  • 原文地址:https://www.cnblogs.com/jizhiqiliao/p/10329986.html
Copyright © 2011-2022 走看看