How to Connect to an Mdf Database File

How do I connect to an MDF database file?

Add space between Data Source

 con.ConnectionString = @"Data Source=.\SQLEXPRESS;
AttachDbFilename=c:\folder\SampleDatabase.mdf;
Integrated Security=True;
Connect Timeout=30;
User Instance=True";

How to create connection for mdf files

Use SqlServeCe as a namespace and then give the following codings

     using System.Data.SqlServerCe;

SqlCeConnection conn = new SqlCeConnection(@"Data Source=D:\PROJECT\data\db.mdf;");

Connection String to Connect to .MDF

If you're using the *.mdf file in the App_Data folder of an ASP.NET app you can use this.

Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;

If it's not an ASP.NET application don't use the DataDirectory syntax and just use the full c:\... path.

Sql Connection string to my mdf database file

NVM found soluation . the problem was []

string dbPath = Application.StartupPath + "\\DATABASE1.MDF";
string strConnection =
@"Data Source =.\SQLEXPRESS; AttachDbFilename = " + dbPath + "; Integrated Security = True; Connect Timeout = 30; User Instance = True";

Connecting to mdf database with Web.config ASP.NET

I finally fixed it by going to Server Explorer > Connect to Database > Data source to "Microsoft SQL Server Database File (SqlClient)" and Db file name browse to the .mdf file you want to use. Use Windows Authentication.

in the Web.config use this string:

<add name="Datab1" connectionString="Data Source=(localdb)\v11.0;AttachDbFileName=path\to\folder\of\database\Datab1.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>

And replace Datab1 with your own database name of course. This worked for me so this question is solved!

How do I connect to an .mdf (Microsoft SQL Server Database File) in a simple web project?

So here's the answer from MSDN:

Choos[e] "Add New Data Source" from the
Data menu.[And follow the connection wizard]

Very easy, except that I have no Data menu. If you don't have a Data menu, do the following:

  • Click on Tools >> Connect to Database...
  • Select "Microsoft SQL Server Database File", take the default Data provider, and click OK
  • On the next screen, browse to your Database file, which will be in your VS Solution folder structure somewhere.

Test the connection. It'll be good. If you want to add the string to the web.config, click the Advanced button, and copy the Data Source line (at the bottom of the dialog box), and paste it into a connection string in the appropriate place in the web.config file. You will have to add the "AttachDbFilename" attribute and value. Example:

The raw text from the Advanced panel:

Data Source=.\SQLEXPRESS;Integrated Security=True;Connect Timeout=30;User Instance=True

The actual entry in the web.config:

<add name="SomeDataBase" connectionString="Data Source=.\SQLEXPRESS; 
AttachDbFilename=C:\Development\blahBlah\App_Data\SomeDataFile.mdf;
Integrated Security=True; Connect Timeout=30; User Instance=True" />

SQL Server: Importing database from .mdf?

See: How to: Attach a Database File to SQL Server Express

Login to the database via sqlcmd:

sqlcmd -S Server\Instance

And then issue the commands:

USE [master]
GO
CREATE DATABASE [database_name] ON
( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Data\<database name>.mdf' ),
( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Data\<database name>.ldf' )
FOR ATTACH ;
GO

How can i connect my .mdf database into vb.net?

The problem is you are using MySqlConnection, and that only must be used with MySql databases. You must use SqlConnection:

Imports System.Data.SqlClient;

Public Class Form1

Dim con As SqlConnection

Private Sub PictureBox1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
con = New SqlConnection
con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\almond\Desktop\TICKETING SYSTEM\TICKETING SYSTEM\Database1.mdf;Integrated Security=True;User Instance=True"
Try
con.Open()
MessageBox.Show("Connected!")
con.Close()
Catch ex As SqlException
MessageBox.Show(ex.Message)
Finally
con.Dispose()
End Try

End Sub


Related Topics



Leave a reply



Submit