首页 > 数据库技术 > 详细

连接SQL Server数据库

时间:2019-01-28 14:32:57      阅读:153      评论:0      收藏:0      [点我收藏+]

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:

技术分享图片

连接SQL Server数据库

原文:https://www.cnblogs.com/jizhiqiliao/p/10329986.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!