Connect to an ASP.NET SQL express db with another C# Project - c#

I'm currently working on a project that has a front end ASP.NET website where users will submit "orders". I then need to access a specific table in this database with another C# windows form application that interacts with multiple Serial Ports. (I'm not married to this idea, so if you think it's a better idea to have one website for both functions, i'm all ears)
Anyway, this question is regarding the ability to have two connection strings (one from ASP.NET, and the other from a local program. - Both of which will be run on the same computer)
On the C# end, i have a connection string of:
"AttachDbFilename=C:\\Users\\Jordan\\Documents\\Visual Studio 2012\\WebSites\\WebSite1\\App_Data\\MyxoData.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True;"
With this configuration, when one project is active, the other cannot access the database.
Cannot open user default database. Login failed. Login failed for user 'CMYK-164\Jordan'.
I have a feeling this is because I am using Integrated Security=true in my connection string and "User Instance=True" specifically only allows one user on a computer to log into a DB at a time...but I can't find anything that backs up my theory...or has a way around the problem.
Any suggestions would be much appreciated!
EDIT
The connection strings are not an issue. Both applications work perfectly when the other isn't active. The issue here is establishing two concurrent connections to one database, from two different C# applications.

Building on what Rafeal's response; you need to upgrade to SQL Server Express.
SQL Server Express LocalDB is a stripped-down version with a lot of limitations; one being that it does not support concurrent connections. See the link below - the part explaining the concurrent connection limitation can be found in the last sentence of the Permissions section.
http://technet.microsoft.com/en-us/library/hh510202.aspx
You can install SQL Server Express here:
http://www.microsoft.com/en-us/server-cloud/Products/sql-server-editions/sql-server-express.aspx#fbid=xWTelsiKWFm
And in Visual Studio you can use the Database Explorer to create a Data Connection to the new database to create your tables and any stored procedures you need.
If you are not familiar with .Net and SQL Server, you can connect to the SQL Server Express database easily by using the System.Data.SqlClient namespace. Since it's designed for SQL Server products, it is supposedly more efficient than using ADO.Net or other data providers. And, at least, I have found it to be quick and easy to use. For example, since the provider is explicit, you don't need to specify a provider in the connection string, resulting in a shorter string. For more information on the connection string, see this page:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=vs.110).aspx
And you can find C# and VB examples of opening a connection at the bottom of this page:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.aspx

Related

SQL Update Query for byte[] and images not working with C# application [duplicate]

Apparently, using AttachDbFilename and user instance in your connection string is a bad way to connect to a DB. I'm using SQL server express on my local machine and it all seems to work fine. But what's the proper way to connect to SQL server then?
Thanks for your explanation.
Using User Instance means that SQL Server is creating a special copy of that database file for use by your program. If you have two different programs using that same connection string, they get two entirely different copies of the database. This leads to a lot of confusion, as people will test updating data with their program, then connect to a different copy of their database in Management Studio, and complain that their update isn't working. This sends them through a flawed series of wild goose chase steps trying to troubleshoot the wrong problem.
This article goes into more depth about how to use this feature, but heed the very first note: the User Instance feature has been deprecated. In SQL Server 2012, the preferred alternatives are (in this order, IMHO):
Create or attach your database to a real instance of SQL Server. Your connection string will then just need to specify the instance name, the database name, and credentials. There will be no mixup as Management Studio, Visual Studio and your program(s) will all be connecting to a single copy of the database.
Use a container for local development. Here's a great starter video by Anna Hoffman and Anthony Nocentino, and I have some other resources here, here, and here. If you're on an M1 Mac, you won't be able to use a full-blown SQL Server instance, but you can use Azure SQL Edge if you can get by with most SQL Server functionality (the omissions are enumerated here).
Use SqlLocalDb for local development. I believe I pointed you to this article yesterday: "Getting Started with SQL Server 2012 Express LocalDB."
Use SQL Server Compact. I like this option the least because the functionality and syntax is not the same - so it's not necessarily going to provide you with all the functionality you're ultimately going to want to deploy. Compact Edition is also deprecated, so there's that.
Of course if you are using a version < SQL Server 2012, SqlLocalDb is not an option - so you should be creating a real database and using that consistently. I only mention the Compact option for completeness - I think that can be almost as bad an idea as using AttachDbFileName.
EDIT: I've blogged about this here:
Bad Habits : Using AttachDBFileName
In case someone had the problem.
When attaching the database with a connection string containing AttachDBFile
with SQLEXPRESS, I noticed this connection was exclusive to the ASP.NET application that was using the database. The connection did block the access to all other processes on the file level when made with System.Data.SqlClient as provider.
In order to assure the connection to be shareable with other processes
instead use DataBase to specify the database name in your connection string
Example or connection string :
Data Source=.\SQLEXPRESS;DataBase=PlaCliGen;User ID=XXX;password=ZZZ; Connect Timeout=30
,where PlaCliGen is the name (or logical name) by which SQLEXPRESS server knows the database.
By connecting to the data base with AttachDBFile giving the path to the .mdf file
(namely : replacing DataBase = PlacliGen by AttachDBFile = c:\vs\placligen\app_data\placligen.mdf) the File was connected exclusively and no other process could connect to the database.

Connect to LocalDB of other system [duplicate]

I am looking into using the new SQL Server Express LocalDB (I think it is code named "Denali") for a desktop application.
It is currently running with SQL Compact, but the user is wanting to share the database between multiple PCs on a network. Unfortunately this is not something that SQL Compact can do, so I am investigating other solutions.
The client requires the ability to send database files easily to other sites or to back them up to a flash disk, so I am avoiding going to SQL Express because there is quite a bit of "administrator" knowledge required to backup and restore.
So, my questions is, does the new SQL Express LocalDB support remote connections to the database over a network and/or through a shared network folder with the mdf file in it?
LocalDB does support supplying a path for an attached local DB in it's connect string (AttachDbFileName) hence the shared network folder option.
NOTE: This question pertains to "LocalDB" the new version of SQL Express 'Denali' and not to SQL Server Express 2008 or prior.
See article here announcing LocalDB's release: http://blogs.msdn.com/b/sqlexpress/archive/2011/07/12/introducing-localdb-a-better-sql-express.aspx
No, SQL Server Express LocalDB doesn't accept remote connections.
The idea with shared network folder might work, but only if you are able to make sure the LocalDB instance is shutdown before you try to copy the file. Also keep in mind that only one LocalDB instance can have any given database file open at the same time. and don't forget about the log files!
Additional security warning: unlike SQL Server Compact databases, SQL Server Express databases (including LocalDB ones) are not designed as secure data exchange format. For instance, they can contain malicious code in .NET assemblies embedded in them. So you should never open databases from untrusted source.
Maybe providing the customer with a simple tool that automates the backup process would be a better idea?
This isn't a fresh thread, but I would like to share my experience with SQL Server Express database LocalDB.
I have a WPF C# project using SQL database with LocalDb Engine. It is working fine no problem, I can use the database with the WPF app. I wanted this app to work on network with more PCs.
On the network another PC can use the database from my PC using UNC path in the connection string.
It seemed to me the remote connection is working. However when the remote PC is connected, I am not able to use the database with my local WPF app. If I run my app first the remote PC could not connect. So this tells me that the remote connection is working, but the multiple connection is not allowed.
OK, I didn't give up and I run the app from my PC twice and I saw it is working which tells me that the same SQL LocalDB engine can handle multiple connections locally only.
I hope this experience will help someone. Thanks.
In short, yes it can. Here is a tutorial on how to configure it.
Also, here is another post with a potential issue that might occur.
Both explain how to configure SQL Server Express to accept Remote Connections.

remote connection string from Webmatrix 3 to Microsoft SQL Server

This seems like a really easy problem to solve, from what I can gather online, but I cannot get it to work for the life of me:
I am trying to help out my dad on his website. It is an ASP.NET based site using Microsoft SQL Server Management Server. The code was developed using Visual Studio, and is hosted with Microsoft's IIS, so, in essence, it is a completely Microsoft-based site.
So far, I've gotten a test site to essentially work on WebMatrix 3; however, I cannot figure out what I need to put into the connection strings in order to get WebMatrix's SQL system to connect to the production server.
I also tried to duplicate the databases by extracting the structure from the production databases and running the query on WebMatrix, but the program needed service pack 2 for MS SQL 2008, and that install is failing for reasons I don't know why.
Best case scenario is I connect to the production server's databases.
Currently, the connection strings have:
Data Source = (ComputerName)\SQLEXPRESS
Initial Catalog = (db)
Persist Security Info = true
User ID = (uid)
Password = (password)
ProviderName= System.Data.SqlClient
So, to clarify,
Web Matrix is installed on my desktop computer, and the website is the code itself. The production site is the website code installed and running on a server somewhere I don't know where, while the test site is a copy of the website loaded onto webmatrix.
There is one working copy of the database, and it is on the production server.
I am trying to construct a connection string within WebMatrix that will connect to the server, through the proper port, and login to the database there to retrieve the data necessary to construct the site so that I can edit the code and test it.
So my question, in particular, is how does one accomplish this when there does not seem to be a slot to indicate a port in the WebMatrix connection wizard?
Webmatrix supplies a wizard to create a connection with a SQL Server. In the Databases workspace you must select Connections > New > Sql Server Connection an input your configuration data into the form.
Anyway the resulting connection string in the Web.config file should be like this:
<connectionStrings>
<add connectionString="Server=(ComputerName)\SQLEXPRESS;Database=(db);Uid=(uid);Pwd=(password)"
name="MailingLists" providerName="System.Data.SqlClient" />
</connectionStrings>
OK, my apologies; I have figured out how this works.
In the connection wizard, you can treat the "Server" box as if it were the "Data Source" option specified in a regular connection string. Then, you can add a comma and the port at which the SQL Server is set to listen.
However, now I am dealing with the problem where the host machine is actively refusing my connection.

Database access on other machine

I'm working on a program that will work very nicely with a database structure and using mysql. I could easy do this with a common server and common database. However I'm looking for a way to have my client(s) use the program on an offline machine without having to install any database managing software.
Basically I want the installation to set up the necessary tables on their machine and have them fill in the database with information relevant to them. Before I start though I wanted to know if this was possible to connect to and manage a database through a C# application with out installing sql software.
You could consider using SQL Server Compact Edition.
SQLite is another option.
Both of these can be deployed with your application and need no separate configuration.
Yes. It is possible. How to do it exactly depends on the database connection approach you're using. For example, this approach shows how to use DataReaders. That's a very old approach. Modern approaches are recommended to use LINQ to SQL, which can be configured to access remotely by setting up a DataContext to point to an external resource.
Basically, wherever you can define a connection string to connect to a DB, you can make that string point locally or externally. An external resource must obviously be available through some URL in order to connect, of course.
You can not connect to a mysql database without installing mysql.
However you can use in process database like sqlite or Compact SQL. They are not traditional server, but rather a library that keeps the database in a local file.

SQL Server connection string

I'm guessing you get hundreds of these questions, but here goes:
So I'm trying to get a Honeywell Dolphin 6100 mobile device (CE5.0, .NET Compact Framework) to talk to an SQL Server Express 2008 database installed on the machine I'm developing on.
I am a complete newbie to SQL Server and mobile development, and am still a little green in C# (yeah I know, jumped in the deep end here, eh? :D)
So far I have:
string sConnection = #"Data Source=JEZ-LAPTOP;DataBase=EMS_Main;Integrated Security=SSPI;";
SqlConnection sqc = new SqlConnection(sConnection);
sqc.Open();
The app deploys quite happily to the 6100, but the last line bugs out with a vague "SQL Exception" error.
I have tried changing the Data Source to include instance names, slashes and dots before it etc etc (even though server is just using the default instance), to no avail.
I can connect to the database in Management Studio with no problems.
So, is the connection string at fault, or is it something I haven't done correctly in SQL Server?
Thanks in advance.
This site is awesome btw, some very knowledgable guys here.
CE 5.0 does not support integrated security. I believe the first version to support it was mobile 6.1. In any case, you cannot use SSPI with your configuration. You'll have to create a SQL Server user and use that as your connection credentials.
Another thing to try, besides using a UID/PWD to connect is to refer to the server by IP. It's possible that DNS resolution is not taking place properly on your device. Hmm, that is a whole nother issue. Is your device on the same network as the SQL Server?
And for future reference, commit this handy URL to memory: http://connectionstrings.com
EDIT
Let's see something like... if Named SQL Server instance:
#"Data Source=192.168.0.56\SERVER_NAME;DataBase=EMS_Main;User Id=joe;Password=pwd;";
if not named SQL Server instance:
#"Data Source=192.168.0.56;DataBase=EMS_Main;User Id=joe;Password=pwd;";

Categories