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;
The correct connection string format is:
con.ConnectionString = @"Data Source=.\SQLEXPRESS;
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";
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!