Home > Database > Mysql Tutorial > Why Does Connecting to an MDF Database in C# Throw an 'ArgumentException: 'DataSource' Keyword Not Supported'?

Why Does Connecting to an MDF Database in C# Throw an 'ArgumentException: 'DataSource' Keyword Not Supported'?

Susan Sarandon
Release: 2025-01-04 15:06:39
Original
644 people have browsed it

Why Does Connecting to an MDF Database in C# Throw an

Connecting to an MDF Database File

When attempting to connect a C# application to an MDF database for the first time, an ArgumentException indicating that the 'DataSource' keyword is not supported may be encountered. To resolve this issue, ensure proper formatting of the connection string.

The problematic code in the provided question is:

con.ConnectionString = "DataSource=.\SQLEXPRESS;
Copy after login

The correct connection string format is:

 con.ConnectionString = @"Data Source=.\SQLEXPRESS;
Copy after login

Note: The addition of space between "Data" and "Source".

Here is the updated code with the correct connection string:

con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=SampleDatabase.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
Copy after login

After making this modification, the application should be able to successfully connect to the MDF database.

The above is the detailed content of Why Does Connecting to an MDF Database in C# Throw an 'ArgumentException: 'DataSource' Keyword Not Supported'?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template