SQL connection string for embedded database in Visual Studio solution - c#

I am starting with SQL Server in Visual Studio and have a problem, most probably with my connection string.
I am not installing SQL Server on my computer (there are several reasons for that) but created inside my solution a SQL project. Then, in another project I am attempting to connect to this database using some googled examples, but can't manage to connect. I frequently see that there's a user and password used to connect to the db, but in my case, thus there is no installation but an embedded SQL project, I can't work it out...
My connection string looks like this:
sqlConnectionString = #"Data Source = MyComputerName;initial catalog=DatabaseStudents";
Can anyone let me know if this kind of installation is even possible?
Thanks

This should be in your web.config if you're looking for a simple answer
<add name="YourDBContext"
connectionString="Data Source=(LocalDb)\ServerName;Initial Catalog=DatabaseName;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\DatabaseFile.mdf"
providerName="System.Data.SqlClient" />

Related

Visual Studio - Connection String for SQL Server Database Project

What is the correct way to write a connection string for a SQL Server Database Project within the same solution?
My connection string defaults as
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-PokemonDayCare-20170701121228.mdf;Initial Catalog=aspnet-PokemonDayCare-20170701121228;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
That LocalDB doesn't actually exist, so it creates it once it needs it (I think it does because I'm using single user authentication?).
So I need to make sure it doesn't create another database if I get the connection string wrong.
Here are the properties for my database project (it sits in the same solution as my web app).
How do I create a connection string for the database project?
(sorry if it's stupid or painfully obvious)
In your solution explorer double click properties
Select the Debug Tab
Select Edit Under Target Connection String Setting

How to connect ASP.NET project with SQL Server database table in Visual Studio (C#)

I have created simple database table in Visual Studio 2015 called Users and updated it.
I want to connect it with my project, so after I fill in my text fields and press 'Sign Up' button, it can get saved to database. I am aware that I'll have to use string connections and other stuff to connect it, but how do I first connect this project with web.config ?
I have seen people dragging this table to Registration form code, so it would automatically create a connection with web-config, but it didn't work for me.
How do I reference my database from web.config correctly ?
Right-click on MyDatabase.mdf and in the properties panel you will see the Connection String. Copy that into the web config in the ConnectionStrings node:
<connectionStrings>
<add name="MyDatabase" connectionString="[Paste Connection String Here]"/>
</connectionStrings>

Umbraco: can't find developer. Database connect to visual studio

I have a project all in local.
I connected the database with visual studio(SQL Server and Visual Studio 2013), I think I had all right, but I can't found the developer section in umbraco.
in web.config
< add name="umbracoDbDSN" connectionString="Data Source=(LocalDB)\v11.0;Database=hotel;AttachDbFilename=|DataDirectory|hotel.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
In the section Users, I can't check developer because I can't see it.
I have no idea what I have to do, maybe is the db not connected?
the user is the same that I use in other project and always worked.
And doesn't work either if I had #developer in the link of umbraco
Some idea for show the developer section? I searched in google and in umbraco.our, but I didn't find the correct answere.
From what I understand, you have not completed the setup. Of if you have, the LocalDb is empty or corrupt. Try to follow the setup as described in the documentation.
The first user you have created is automatically administrator, and will be able to access the settings and developer section.
If you are able to connect to the database (e.g. from Visual Studio), verify that you have correct data in your umbracoUser2app table.
You are using the DataDirectory in your connectionstring, also check that your .mdf file is not overwritten when launching your website from visual studio.

AttachDBFileName is not working after deployment

I have defined connection string in my app.config
<connectionStrings>
<add name="StoreConnectionString"
connectionString="Data Source=(local);MultipleActiveResultSets=True;AttachDBFileName=C:\Users\Firdavs\Desktop\Data\StoreServer.mdf;Integrated Security=True;User Instance=True;"
providerName="System.Data.SqlClient" />
</connectionStrings>
I am using c# and sql express in my application. When I am running in Visual Studio in my machine it works fine. But after creating setup and deploying into other machine. I am editing .exe.config file showing AttachDBFileName=Path. Application is giving me error. Sql Express is installed in that machine. I guess attaching is not working. What do you advice?
Please show me direction!
the problem is here : "AttachDBFileName=C:\Users\Firdavs\Desktop\Data\StoreServer.mdf;"
when you deploy it to another machine, that absolute path has low changes to be the same...you need to give a virtual path...
you need to copy the mdf in the same directory. Then in visual studio make sure that when you select the mdf file , you change the property of "Copy to Output Directory" to be "Copy if newer" (you change it by double-clicking on the "do not copy" text already listed there)
and then change AttachDbFilename to be ... AttachDbFilename=|DataDirectory|StoreServer.mdf;....
As I've said before on this site - the whole User Instance and AttachDbFileName= approach is flawed - at best! Visual Studio will be copying around the .mdf file and most likely, your INSERT works just fine - but you're just looking at the wrong .mdf file in the end!
If you want to stick with this approach, then try putting a breakpoint on the myConnection.Close() call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there.
The real solution in my opinion would be to
install SQL Server Express (and you've already done that anyway)
install SQL Server Management Studio Express
create your database in SSMS Express, give it a logical name (e.g. StoreServer)
connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. In that case, your connection string would be something like:
Data Source=(local);Database=StoreServer;Integrated Security=True
and everything else is exactly the same as before...
When you deploy to a customer's PC - go through the same steps: install SQL Server Express, create the database on the server, connect to it using the server=(local) (or the server=machinename approach) - and be done with it!

sqlite is given me headache in my C#2.0 windows application

I'm struggling since this morning about this sqlite thing.after reading some good tutorials i decided to use it as my embedded database in my winform application.
i downloaded SQLite-1.0.61.0-setup that install the system.datal.sqlite for me and downloaded the firefox plugin sqlite manager and another management tool called wxSQLitePlus found here based on this tutorials found here (just being explicit)
Hoping i'm not posting any stupid stuff that will be ignored, here are my 2 challenges.
the first challenge is that i can't reference the database inside a folder let'say database
i have exception "Unable to open the database file".my app.config is as follow
<connectionStrings>
<add name="embcon" connectionString="Data Source=database\iagency.db;Version=3;"/>
but if the iagency.db is inside the root folder there is no problem at all.app.config like
<connectionStrings>
<add name="embcon" connectionString="Data Source=iagency.db;Version=3;"/>
the second chalenge is that object created via tools such as either firefox plugin or wxsqlite+
cannot be queried inside visual studio 2005 that i'm using.if it's a table when queried , i have an excetion the object doesn't exist even though both tools displays the same data.
and inversly table created by queries inside visual studio cannot be viewed by the tools.
so what's the deal?
is it a memory stuff or i'm just missing some points?
can enybody explain what is happening ?Thanks for reading.
Try using the full filepath as the data source (e.g. Data Source=c:\data\iagency.db)
See this post for more: datasource location in connection string
Try that :
<add name="embcon" connectionString="Data Source=|DataDirectory|\iagency.db;Version=3;"/>

Categories