I'm very new to SQL and made a small program where a user can input some data, click submit and the data is then stored in a table in the database.
I know want to move the application onto a friends computer, which i'm assuming has no SQL software installed, what would be the easiest way to do this, when obviously the connection string is unique to my computer and the database is stored on my computer.
You will have to install SQL Server on their machine first and foremost. Once this is done, you can obtain a relevant connection string. Note, for the 'Server name' part of the connection string, if you are using SQL Express, instead of using 'localhost', or the name of the server instance (i.e. 'MyMachine'), you would use 'localhost\SQLEXPRESS'/'MyMachine\SQLEXPRESS'.
After setting up the SQL Server instance on the new machine, to copy the required database, first detach the database to avoid any corruption. Now you are free to merely copy the file from your machine to theirs and go through the usual attachment process using SQL Server Management Studio (SQLMS).
I hope this helps.
You can use SQL CE or other file databases. On this way you need to install SQL CE(you can include SQl CE installer into your program installer) on target computer and after that you can easy copy db-file from you computer to target computer.
Also, you can use relative path to db-file from your exe file instead of fixed connection string:
string dbDirPath=Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"DB" );
private const string CONN_STR_TEMPLATE = "Data Source={0};Persist Security Info=False;";
string dbFilePath = Path.Combine(dbDirPath, "my.sdf");
_connStr =String.Format(CONN_STR_TEMPLATE,dbFilePath);
You cannot simply copy your database since SQL Server database is NOT a standalone database as SQL Compact edition/MS Access.
You may configure your router to remote access SQL Server instance over the Internet by forwarding the port
Accessing SQL Server Instance through NAT
Related
I have worked with EF for a while now and i have always used LocalDb's for storing data. I want to start working with SQL Server databases instead but I'm having some issues setting up the connection string.
https://msdn.microsoft.com/en-us/library/jj653752(v=vs.110).aspx#sse
https://www.connectionstrings.com/
and looked over google but none of the answers made it work in my case so I must be doing something wrong (some of the connections strings throw an exception others didn't but wouldn't insert anything either into the database neither)
My question is when working with EF & SQL Server, should I use both the connection string in the App.config & setting the path of the DB in the CTOR of the context (by using AppDomain.CurrentDomain.SetData("DataDirectory", path);) or is the app.config sufficient ?
I have tried the following connection strings:
Data Source=.\GURUBEAST-PC\GURUSQL;Initial Catalog=iManager;Trusted_Connection=True;MultipleActiveResultSets=True;
Data Source=.\GURUBEAST-PC\GURUSQL;Database=iManager;Integrated Security=True;Trusted_Connection=True;MultipleActiveResultSets=True;
Data Source=.\GURUBEAST-PC\GURUSQL;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL11.GURUSQL\MSSQL\DATA\iManager.mdf;Database=iManager;Trusted_Connection=True;MultipleActiveResultSets=True;
Data Source=.\GURUBEAST-PC\GURUSQL;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL11.GURUSQL\MSSQL\DATA\iManager.mdf;Database=iManager;Trusted_Connection=True;
Data Source=.\GURUBEAST-PC\GURUSQL;Database=iManager;Trusted_Connection=True;
Data Source=.\GURUBEAST-PC\GURUSQL;Initial Catalog=iManager;Integrated Security=SSPI;
Data Source=.\GURUBEAST-PC\GURUSQL;Initial Catalog=iManager;User id=GURUBEAST-PC\GuruBeast;
Where "iManager" is the name of the database. I use Windows auth for my SQL Server instance.
What am I doing wrong ? Should I set my path to the program files folder or the App_Data (I have seen both and tried both but both didn't work)?
Kind regards!
The Data Source key is used to find the machine on which the Sql Server instance runs.
You can have different strings for it but the most common used in a LAN environment is composed using the name of the server machine followed by an eventual instance name.
So, if your local PC is named GURUBEAST-PC and, at install time, you haven't specified any instance name, the connectionstring Data Source contains only the name of the machine GURUBEAST-PC. If you have an instance name then you should add that instance name to you Data Source key. GURUBEAST-PC\GURUSQL
This will guarantee to all the PC in the same LAN the possibility to have the same connectionstring also if the connection is made from the same PC where the SQL Server runs.
If the Data Source points at the local pc, you can use many shortcuts to represent the local PC:
(LOCAL)
localhost
.
\.
and eventually add the instance name to these shortcuts without repeating the PC name
Once you get your host name figured out, Entity Framework will generate your connection string for you. Here's a sample of what your connection string could look like if you were attempting to connect to AdventureWorks database hosted on your local instance of SQL Server 2014 aptly named sql2014.
<connectionStrings>
<add name="AdventureWorksEntities" connectionString="metadata=res://*/DataModels.AdventureWorksDb.csdl|res://*/DataModels.AdventureWorksDb.ssdl|res://*/DataModels.AdventureWorksDb.msl;provider=System.Data.SqlClient;provider connection string="data source=.\sql2014;initial catalog=AdventureWorks;persist security info=True;user id=App_AdventureWorks;password=asdasdfasdfasdf;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
Your db context would then look something like this.Again, EF generates this for you.
public partial class AdventureWorksEntities : DbContext
{
public AdventureWorksEntities()
: base("name=AdventureWorksEntities")
{
}
I have created an application using Entity Framework 6 code-first, in ASP.NET MVC 5, and am bin deploying it to my server. Everything works fine, except for the operations/controller actions that involve database usage.
I am uploading the once-generated database file from my computer to the App_Data folder of the server.
Upon deploying, I changed the connection string in my web.config file from:
connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-NERC_Main-20160104065223.mdf;Initial Catalog=aspnet-NERC_Main-20160104065223;Integrated Security=True"
to
connectionString="Data Source=.;AttachDbFilename=|DataDirectory|\aspnet-NERC_Main-20160104065223.mdf;Initial Catalog=aspnet-NERC_Main-20160104065223;Database=aspnet-NERC_Main-20160104065223.mdf;Trusted_Connection=Yes;Integrated Security=True;"
which throws an error of
CREATE DATABASE permission denied in database 'master'.
A probable cause of this is because the present database file is not being attached, and the entity framework is trying to generate a new database in some other, restricted directory.
I read that the AttachDbFileName is valid for SQL Server Express instances only, which in my case doesn't exist.
How can I modify the connection string so that my current, already uploaded database is utilized.
Note:
The remote server has a full installation of SQL Server 2008 R2. The server does not support user instances. My database is not password protected, and hence I've set the Integrated Security property value to True. I'll provide any other information if required.
If you have a full version of SQL Server, you cannot just use AttachDbFileName. Instead, you need to copy the .mdf (and .ldf) to the SQL Server data file location and then you need to attach the database in SQL Server Management Studio to the server instance.
From that point on, you can reference that database in your connection string using the server name, and the logical database name - something like this:
Server=.;Database=aspnet-NERC_Main-20160104065223;Integrated Security=True;
Depending on your database setup, you may or may not be able to use the Integrated Security - maybe you'll need to have a specific SQL Server login and specify that login (and its password) in your connection string instead:
Server=.;Database=aspnet-NERC_Main-20160104065223;User ID=YourUserName;Password=YourPassword
See this site here for a ton of sample of how to build valid connection strings for SQL Server.
I have C# application which uses SQL Server R2 as its database. That database is on a separate PC by the name of SERVER. SQL Server's instance name also server.
My windows application uses DataSet to communicate with the database. Now my SQL installed PC name is change to another name, ex SERVERHP. Now all my coding works want to change my connection string. Are there any other easy way to do it ?
I am to tried to edit hosts file, but it does not work for me.
This is my coding style (http://goo.gl/FQrkp). I am using DataSet with DataAdapter with IDE designers.
I have 100 ~ 150 forms. Now I cannot compile all codings. I want to easy method to connect that SQL Server database.
I want to have a way to hide the change of the hostname of the computer
You have a couple of options:
Globally replace all server names with IP address in connection strings in the app
Globally replace all server names with the new server name in the app
Add a CNAME record to the DNS table on the server (assuming of course you're in the same network, which you are if you're using computer names)
Add an entry to the LMHOSTS file (you can add as many names as you'd like that point to the same IP)
As I understand, it is SqlConnection used to connect to database, or something like that. Why don't you use SqlConnectionStringBuilder? Then you can dinamically construct connection string you need. Also, to get list of servers, you can use SqlDataSourceEnumerator, from namespace System.Data.Sql.
It sounds like you want to have a way to hide the change of the hostname of the computer (Server/Desktop/Virtualized instance of Windows, whatever) that is running your SQL Server.
This isn't my area of expertise, but I can't think of a way to do it that only involves your application code and just the computer.
If you control the local DNS you can create a CNAME entry with the old name that "points" at the new one. Depending on how your connection strings are stored, you might have to edit them or you might not. But you won't have to worry about the location of your SQL Server changing again because you can always edit your CNAME to point at the new location.
Note for the future - not your current problem: If you continue to use MS SQL Server in particular you'll want to be careful about moving it to a computer where it isn't the default instance because then you need to put the instance name in the connection string as well, which might force you to edit all of your application web.configs and app.configs again.
Are you using Visual Studio 2012 with your development efforts? I ran into a conflict with the mini SQL db VS installs (Using Premium version) and had to modify the ConnectionStrings section of my Machine.config file to point to my SQL database. For whatever reason, VS will write references to the mini db in the Machine.Config file (for whatever version of .Net you are leveraging) throwing potetial conflicts.
The file can be found in %systemroot%\Microsoft.Net\Framework64\dot net version\Config
If you're using an x86 processor the Framework64 folder is just called 'Framework'.
How can I connect my Trgovina.mdf with dataGridView?
I follow this tutorial, but it seems that program doesn't find my database.
Connection string looks like that:
string connString = #"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Klemen\documents\visual studio 2012\Projects\Trgovina\Trgovina\Trgovina.mdf;Integrated Security=True";
Everything else is the same as tutorial example.
Error string is An OLE DB Provider was not specified in the ConnectionString. An example would be, 'Provider=SQLOLEDB;'
Full code looks like this.
The tutorial you talk about in your question use an Access Database and thus uses the OleDB engine to reach and work with the database. Instead your connection string use the syntax reserved for SQLServer LocalDB.
You should change your objects to SqlConnection (instead of OleDbConnection), SqlCommand (instead of OleDbCommand) and so on...
With these changes you should be able to connect to the automatic instance of SqlServer LocalDB. The rest of the tutorial could work or not, depending on what is present in the MDF file used.
You trying to connect to database .mdf file, but you have a wrong provider.
An MDF is a Microsoft SQL Server database not a Jet Database like
Access (*.mdb). You cannot just connect to the flat file and read it.
You would need to mount the database in an instance of SQL Server.
You could install SQL Server 2005 Express
Source
Note: Just download MS SQL Server 2005 Express or later and you must use the System.Data.SqlClient instead of OLE DB to solve your problem.
I would like to be able to run an on demand backup of a .Net MVC app's SQL Express 2008 database to eg a flash stick plugged into the machine running the app.
I tried
QuickstemDataContext db = new QuickstemDataContext();
string quickstem_path = Path.Combine(save_path, "quickstem.backup");
db.ExecuteCommand(string.Format("BACKUP DATABASE {1} TO DISK = '{0}' WITH COMPRESSION;", quickstem_path, db.Mapping.DatabaseName));
But get the exception
Database 'quickstem' does not exist. Make sure that the name is entered correctly. BACKUP DATABASE is terminating abnormally
I am using the following connection string.
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\quickstem.mdf;Integrated Security=True;User Instance=True"
Do I need to attach the DB using something like Express Management Studio and give it a name etc. Ideally I want to keep the app deploy very simple without having to setup sql management studio etc. Can this attaching be done another way or can a Backup be done with out needing to attach
I tried giving it the full path of the .mdf file instead of the database name but got a syntax error on c:
You'll find that if you add Database=Quickstem to your connection string, your backup code will work just fine.
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\quickstem.mdf;Integrated Security=True;User Instance=True;Database=Quickstem