How to connect SQL Server Express Database to VS 2017

I constantly run into problems trying to connect my C # program in Visual Studio 2017 Express to a small local database with one table created in Sql Server Express. I would like to link it as a data source in the Entity Framework in my solution (which is in C #). I searched MSDN and this site for several days, but did not find a way to solve this problem.

Now the problem is that when I look at the entity data model wizard (existing database) and proceed to create a new connection, select "Microsoft SQL Server Database File (SqlClient)" and go to my database - the test connection generates an error:

Cannot open the physical file "C: \ Program Files \ Microsoft SQL Server \ MSSQL13.SQLEXPRESS \ MSSQL \ DATA \ MyDatabase.mdf". Operating system error 5: "5 (Access denied.)". An attempt to connect a database with auto-name for the file C: \ Program Files \ Microsoft SQL Server \ MSSQL13.SQLEXPRESS \ MSSQL \ DATA \ MyDatabase.mdf failed. A database with the same name exists or the specified file cannot be opened or is located on a UNC share.

I run Visual Studio as an administrator (this went past a previous problem that did not even allow me to browse the database without asking for an error message that I did not have permission to open the database file).

I can view my database, its table and its data in the SQL Server Object Explorer inside Visual Studio - regardless of whether I run it as an Administrator or not. Not sure if this is important. I'm new to all of this, so sorry if I'm missing something obvious.

+4
source share
2 answers

: Visual Studio " " , , " Microsoft SQL Server", " Microsoft SQL Server ( Microsoft SQL Express ) .NET Framework SQL Server.

, , .

"".

".\SQLExpress" ( connectionstrings.com/sql-server/... @ryguy72!)

" " , , SSMS.

- !

+4

?

https://www.connectionstrings.com/sql-server/

( ).

Server = myServerAddress; Database = MyDatabase; Trusted_Connection = True;

# script, SQL Server .

Excel DGV:

        private void button1_Click(object sender, EventArgs e)
        {

            DataTable table = new DataTable();
            string strConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text\"",
            "C:\\Users\\Ryan\\Desktop\\Coding\\DOT.NET\\Samples C#\\Export DataGridView to SQL Server Table\\Import_List.xls");
            using (OleDbConnection dbConnection = new OleDbConnection(strConn))
            {
                using (OleDbDataAdapter dbAdapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", dbConnection)) //rename sheet if required!
                    dbAdapter.Fill(table);
                dataGridView1.DataSource = table;
                int rows = table.Rows.Count;
            }

            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.Columns["FName"].DataPropertyName = table.Columns["FName"].ColumnName;
            dataGridView1.Columns["LName"].DataPropertyName = table.Columns["LName"].ColumnName;
            dataGridView1.Columns["Age"].DataPropertyName = table.Columns["Age"].ColumnName;
            dataGridView1.DataSource = table;

            //IF THE ORDER DOSEN'T MATTER
            //DataTable table = new DataTable();
            //string strConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text\"", "C:\\Users\\Excel\\Desktop\\Coding\\DOT.NET\\Samples C#\\Export DataGridView to SQL Server Table\\Import_List.xls");
            //using (OleDbConnection dbConnection = new OleDbConnection(strConn))
            //{
            //    using (OleDbDataAdapter dbAdapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", dbConnection)) //rename sheet if required!
            //        dbAdapter.Fill(table);
            //    dataGridView1.DataSource = table;
            //    int rows = table.Rows.Count;
            //}

        }

Excel SQL Server:

        private void button3_Click(object sender, EventArgs e)
        {
            System.Data.OleDb.OleDbConnection ExcelConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\Excel\\Desktop\\Coding\\DOT.NET\\Samples C#\\Export DataGridView to SQL Server Table\\Import_List.xls;Extended Properties=Excel 8.0;");

            ExcelConnection.Open();

            string expr = "SELECT * FROM [Sheet1$]";
            OleDbCommand objCmdSelect = new OleDbCommand(expr, ExcelConnection);
            OleDbDataReader objDR = null;
            SqlConnection SQLconn = new SqlConnection();
            string ConnString = "Data Source=Excel-PC;Initial Catalog=Northwind.MDF;Trusted_Connection=True;";
            SQLconn.ConnectionString = ConnString;
            SQLconn.Open();

            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(SQLconn))
            {

                bulkCopy.DestinationTableName = "tblTest";

                try
                {
                    objDR = objCmdSelect.ExecuteReader();
                    bulkCopy.WriteToServer(objDR);
                    ExcelConnection.Close();

                    //objDR.Close()
                    SQLconn.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

        }

... Excel SQL Server:

private void button4_Click(object sender, EventArgs e)
{
    BindGrid();
}

protected void BindGrid()
{
    string path = "C:\\Users\\Excel\\Desktop\\Coding\\DOT.NET\\Samples C#\\Export DataGridView to SQL Server Table\\Import_List.xls";
    string jet = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0", path);
    OleDbConnection conn = new OleDbConnection(jet);
    OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", conn);
    DataTable dt = new DataTable();
    da.Fill(dt);

    dataGridView1.DataSource = dt;
    BulkUpload();
}

... DGV SQL Server:

private void button8_Click(object sender, EventArgs e)
{

    //SqlConnection connection = new SqlConnection("Data Source=Excel-PC;Initial Catalog=Northwind.MDF;Trusted_Connection=True;");
    DataTable dt = (DataTable)dataGridView1.DataSource;
    string connection = "Data Source=Excel-PC;Initial Catalog=Northwind.MDF;Trusted_Connection=True;";
    using (var conn = new SqlConnection(connection))
    {
        conn.Open();
        using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conn))
        {
            bulkCopy.ColumnMappings.Add(0, "Fname");
            bulkCopy.ColumnMappings.Add(1, "Lname");
            bulkCopy.ColumnMappings.Add(2, "Age");

            bulkCopy.BatchSize = 10000;
            bulkCopy.DestinationTableName = "Import_List";
            bulkCopy.WriteToServer(dt.CreateDataReader());
        }
    }

}

.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using Excel = Microsoft.Office.Interop.Excel;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Configuration;
using System.Data.SqlClient;
+1

Source: https://habr.com/ru/post/1673176/


All Articles