Can't connect to SQL Server: "Login failed for user "."
Solution 1
In your connection string, you haven't specified whether you want Windows Authentication or SQL Authentication. For SQL auth it should be (obviously replace x and y with your username and password):
<add name ="AutoLotSqlProvider" connectionString =
"Data Source=.\SQLEXPRESS;User ID=x;Password=y;AttachDbFilename=C:\...\AutoLot.mdf"/>
For Windows auth it should be:
<add name ="AutoLotSqlProvider" connectionString =
"Data Source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDbFilename=C:\...\AutoLot.mdf"/>
Solution 2
Your connection string is missing an authentication scheme. You need to either pass a username/password or use integrated security when creating a connection.
Solution 3
You need to specify a Username and Password to login to Sql database.
For a list of connection strings check ConnectionStrings
OLEDB connection string for sql 2008 is Provider=SQLNCLI10;Server=myServerAddress;Database=myDataBase;Uid=myUsername; Pwd=myPassword;
Tim
Updated on June 15, 2022Comments
-
Tim almost 2 years
I'm new with .NET and have hit a brick wall. I'm writing code in C# to access a Microsoft SQL Server 2008. This is the code from my app.config file
<configuration> <appSettings> <add key="provider" value="System.Data.SqlClient" /> </appSettings> <connectionStrings> <add name ="AutoLotSqlProvider" connectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\AutoLot.mdf"/> <add name ="AutoLotOleDbProvider" connectionString = "Provider=SQLOLEDB;Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\AutoLot.mdf"/> </connectionStrings> </configuration>
When I debug the C# program I'm getting this error message:
System.Data.SqlClient.SqlException { Login failed for user "." }
I cannot find a user name in the database
This is my program code:
public class Program { static void Main(string[] args) { // Get Connection string/provider from *.config. Console.WriteLine("***** Fun with Data Provider Factories *****\n"); string dp = ConfigurationManager.AppSettings["provider"]; string cnStr = ConfigurationManager.ConnectionStrings["AutoLotSqlProvider"].ConnectionString; // Get the factory provider. DbProviderFactory df = DbProviderFactories.GetFactory(dp); // Now make connection object. using (DbConnection cn = df.CreateConnection()) { Console.WriteLine("Your connection object is a: {0}", cn.GetType().Name); cn.ConnectionString = cnStr; cn.Open(); if (cn is SqlConnection) { // Print out which version of SQL Server is used. Console.WriteLine(((SqlConnection)cn).ServerVersion); } // Make command object. DbCommand cmd = df.CreateCommand(); Console.WriteLine("Your command object is a: {0}", cmd.GetType().Name); cmd.Connection = cn; cmd.CommandText = "Select * From Inventory"; // Print out data with data reader. using (DbDataReader dr = cmd.ExecuteReader()) { Console.WriteLine("Your data reader object is a: {0}", dr.GetType().Name); Console.WriteLine("\n***** Current Inventory *****"); while (dr.Read()) Console.WriteLine("-> Car #{0} is a {1}.", dr["CarID"], dr["Make"].ToString()); } } } }