“Cannot open user default database. Login failed.” after installing SQL Server Management Studio Express

This may not be answering your question specifically, but it may help others with similar issue caused by different problem In my case the problem was my user is defaulted to a database which is not accessible for any reason (can be renamed, removed, corrupted or …) To solve the issue just follow the following … Read more

Enable tcp\ip remote connections to sql server express already installed database with code or script(query)

I tested below code with SQL Server 2008 R2 Express and I believe we should have solution for all 6 steps you outlined. Let’s take on them one-by-one: 1 – Enable TCP/IP We can enable TCP/IP protocol with WMI: set wmiComputer = GetObject( _ “winmgmts:” _ & “\\.\root\Microsoft\SqlServer\ComputerManagement10”) set tcpProtocols = wmiComputer.ExecQuery( _ “select * … Read more

SQL Insert Query Using C#

I assume you have a connection to your database and you can not do the insert parameters using c #. You are not adding the parameters in your query. It should look like: String query = “INSERT INTO dbo.SMS_PW (id,username,password,email) VALUES (@id,@username,@password, @email)”; SqlCommand command = new SqlCommand(query, db.Connection); command.Parameters.Add(“@id”,”abc”); command.Parameters.Add(“@username”,”abc”); command.Parameters.Add(“@password”,”abc”); command.Parameters.Add(“@email”,”abc”); command.ExecuteNonQuery(); Updated: … Read more

SQL Server named instance with Visual Studio 2017 Installer project

Summary: In essence, the below states: 1) Disable the custom action to run the SQL Server setup.exe in your current MSI. 2) Create a basic WiX Burn Bundle to kick off the SQL Server setup.exe first, and then kick off your Visual Studio Installer Project-generated MSI afterwards. Or better yet, make the whole MSI in … Read more

SQL command INSERT is working but the data not appear in table

If your c# code executes without any exceptions, it updates the database too. You have probably used AttachDbFilename=|DataDirectory|\yourDB.mdf in your ConnectionString, that means the databse that is updated is located in the subfolder BIN\DEBUG folder of your project. If you want to see the updated data just attach the database located in the bin/debug folder … Read more

SQL Express connection string: mdf file location relative to application location

Thanks everyone, I used a combination of your responses. In my app.config file my connection string is defined as follows <add name=”MyConnectionString” connectionString=”Server=.\SQLExpress;AttachDbFilename=|DataDirectory|\MyDatabase.mdf;Database=MyDatabaseForTesting;Trusted_Connection=Yes;” /> In my unit test class I set the DataDirectory property using the following [TestInitialize] public void TestInitialize() { AppDomain.CurrentDomain.SetData(“DataDirectory”, System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, “Databases”)); // rest of initialize implementation … }