SQL Server connection string - c#

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;";

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.

How to Run Windows Form Application with SQL Server Database on Another Computer

I'm using Visual Studio 2015 and I just created a Windows Forms application with a SQL Server database. I finished the program and tried to run the application on the other computer but it didn't run. I also tried to install .Net Framework v4.0 and SQL Server Express on that computer and also put the database in path C:\ so that the SqlConnection path on my computer to another computer will be the same.
The code is like this:
SqlConnection cn;
SqlCommand cm;
SqlDataReader dr;
string connection = #"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Database1.mdf;Integrated Security=True";
But I got an error that is the database I placed in path C:\ is not writable whatsoever. I want to know what I can do in order to run the application.
You must install SQL Express on the computer/server where you want to have the database and then attach the MDF file.
Important:
- Remember to open port 1433 in the Windows Firewall.
- If you are going to use SQL Server authentication, you must create and / or activate the user with the desired password.
- To connect you can use the IP of the computer/server, preceded by the instance of sql express. Example: YourIP\SQLExpress
Good luck!
you need to create sharing server for database and after this, you can access your DB to anywhere and any computer
Thanks
So much misunderstanding and misinformation around LocalDB.
While it was being developed, your application was using an edition of SQL Server called LocalDB. This is a low admin lightweight edition that lets you reference the MDF file directly (contrary to some comments here)
When you deploy your app you decide whether you want to stick with LocalDB or upgrade to a more scaleable edition of SQL Server.
In your case, I suggest you stick with LocalDB
There's some background here and a link to a video if you have time
LocalDB deployment on client PC
https://blogs.msdn.microsoft.com/sqlexpress/2011/07/12/introducing-localdb-an-improved-sql-express/
Now if you really want to an answer you need to post the exact error message that you see, not a vague recollection of it.
The problem might be that LocaDB is not installed, or it might be that there is some SQL Server access issue. We can't tell because you did not post the error message

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.

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

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

What is required to use a Windows form application use a SQL Server database through network?

What is required to use a Windows form application use a SQL Server database thrugh network?
I want to install a SQL Server on 1 PC that has a Windows XP OS as a server. And, through a network I want the application to connect to a database.
So, I know that I should setup .Net Framework 4.0 to the application and I did a deployment to my project. I don't know if I need any application to install to do this connection like a server application or not?
Thanks in advance
The usual suspects / problems to access SQL remotely are:
Enable TCP/IP protocol in SQL server
Open up firewall to let SQL traffic through (usually port 1433)
Configure SQL server to allow remote connections, run Browser Service
Use the right credentials to address your SQL server instance
There are four basic things that are required...
There are limits on installing
server software on a desktop OS
(Traditional SQL server will not
install on XP) So you need to understand what you can install and where.
In your application there are choices as to "how to connect", but my experience is you want to use the .Net SQLClient
You need an understanding of how to
invoke an sql statement on the
server through direct sql or stored
procedures.
You need to understand what to expect in return from a query (nothing, scalar value, returned parameters, recordset...) and what to do with it when you get it...
For more information, please post examples of what you have tried, what the result was and what (specifically) you are trying to accomplish.
Edit after comment from #Marziana
As I read you comment I believe there are two questions:
How do I deploy the database to a
new server?
How do I connect my window forms
application to that new database?
1) Out of your original question, installing MS SQL server (standard edition) will NOT install on MS Windows XP. You need a Windows Server Software installation onto which you may install SQL Server. Your other option is to install a lesser edition of SQL Server to install on XP (such as SQL Desktop Edition, Evaluation Edition or Personal Edition).
2) The "Connection String" is the directive to the connection object telling what server to locate (and what instance of SQL on that server), which database to attach to on that server (the "initial catalog"), the security model to implement and the credentials (Username/password) to use if not using "integrated" security). If you "hard coded" your connection string then that makes it impossible to change the connection string to adapt to a new database server/dbname/credentials.
Beyond that your question is overly broad for a more direct answer.
SqlConnection myConnection = new SqlConnection("user id=username;" +
"password=password;server=serverurl;" +
"Trusted_Connection=yes;" +
"database=database; " +
"connection timeout=30");
http://www.codeproject.com/KB/database/sql_in_csharp.aspx
Try that.

Categories