C# DBMLs and SQL Server - c#

My dev machine has SQL Server 2005 on it and that is how I make my DBML file and define the tables in there and then make a WCF Service. When i go to host the WCF service, the server has SQL Server 2000, it finds the connection string fine but not the table im pointing it to. Is there a way to tell through the web.config file which SQL Server i am using at run time?
Thanks

sure, that's what the connection string is for, are you sure you have the right permissions on the server?

The name of the server and database should be in the connection string (in the config file); what does it currently look like? And what is the exact error message.
The exact connection string depends on your setup (for example, is the server a "named instance"?).
The other thing I can think of... are the objects in the right schema? i.e. are they "daniel.sometable" on your machine, but "dbo.sometable" on the server? This matters, since the dbml includes the schema. Fortunately, you can edit the dbml (it is just xml) and use "replace all" (i.e. ctrl+h) to fix this...

Related

How to connect to a SQL Server instance without knowing the Initial Catalog?

I'm writing a portable program that can be used on other computers as well as mine and It also connects to the SQL Database on that computer.
So the problem is that I don't have the database name of that computer. How can my program connect to that Database without previously knowing the database name?
In other words, I don't have enough information to create a complete ConnectionString.
Connect to SQL Server's master database first.
Then, using this query, you can get data from databases in your database:
SELECT name FROM master.dbo.sysdatabases
Using the site below, you can find the right Connection strings for you :
https://www.connectionstrings.com/
You can use a dot in the connection string to represent.tbe local server.
Create a txt file, rename the extension to UDL. Open the UDL file and put a dot, a full stop:
If you have installed a SQL Instance (ie not the default instance) it will be .\InstanceName
Choose the Database drop down and click Test Connect.
Finally close the URL file and open it with NotePad and you will see the connection string.

Is there a universal connection string for different computers to connect to a local database using my program developed by vs2010

newbie here.
I have a local db in my program. Whilst I was developing the program I used the SQL
Connection string :
SqlConnection sconn = new SqlConnection(#"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\leemk_000\Documents\......Integrated Security=True;User Instance=True;");
Now If I want to load this program onto a different computer I am sure that this connection will no longer work simply because it will still be looking for Users\Lee_000\
I have tried to remove Lee_000 but I get this following error:
An attempt to attach an auto-named database for file C:\Users\Documents..... failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
What can I do to get a connection string to work on different computers.
With many thanks
The whole User Instance and AttachDbFileName= approach is flawed - at best - especially when you want to share your database amongst multiple clients!
When running your app in Visual Studio, it will be copying around the .mdf file (from your App_Data directory to the output directory - typically .\bin\debug - where you app runs) 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. YourDatabase)
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=.\\SQLEXPRESS;Database=YourDatabase;Integrated Security=True
and everything else is exactly the same as before...
If it's a local db you should be placing it within the app folder and carry it with the app right?
Put the database in the App_data folder of your app and use that in your connection string
<add name="YourConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\yourfile.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
You need to use a database server and let your users use it via your connection string like this;
Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;
"myServerAddress" should be the ip adress of your server machine.

Cheat application to connect to different SQL Server database

At work I wrote an application that processes data from a SQL Server database and outputs it in file format.
Now I need to use it at home and since app has hard-coded connection string I've got a problem. I got a copy of database at my company, the original database is inaccesable from the outside of the company.
Connection string format looks like this
Data Source=serverName-01;Initial Catalog=dbName_01;Integrated Security=True;Pooling=False
I've tried to cheat app by editing windows hosts file:
serverName-01 127.0.0.1
But it did not work. Is there a way to make it work without going to work and editing source code ?
Lesson learned hard way - never hard-code connection srings :<
To do this you can use an alias defined in SQL Server Configuration manager. Create an alias for your local instance that has the exact same name as the one in the connection string.
Have a look at the following article for how to do this (it's pretty simple):
Create or Delete a Server Alias for Use by a Client (SQL Server Configuration Manager)

Connection string when database (*.mdf) is copied on the web hosting server

I am continuing my previous question, as the reply lead to further doubts/points/concerns. I need help with the connection string on the web host server.
My connection string in the local computer is:
string connectionString = "Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\data.mdf;Integrated
Security=True;User Instance=True";
Now, I simply used the website copy tool with the VS 2010, and the entire website is copied as it is on the web host, with the database being at domainName/httpdocs/Experiment/App_Data/data.mdf
Now, I don't know how the complete connection string on the web hosting server look like. Some of the things which I learned, on the basis of those, I guess it should be:
string connectionString = "Data
Source=IP_Address_Of_WebHostingServer;AttachDbFilename=|DataDirectory|\data.mdf;User
ID=XXXX;Password=XXXX;User Instance=True";
Is it right (or COMPLETE?)? Also, I have no clue what the user id or password is? So on local computer, with the help of Integrated Security=True the windows authentication was being made. How to do it on the remote web host?
In the webhosting console, I see features such as create database, create database users, I can see the username and password aspects there. If those are required then how do I connect that with the database I just copied? It seems like those are the one's where database is created from scratch, while I already have the *.mdf (database) uploaded/copied.
I am stuck at this stage, and have no clue on how to proceed further. I know its something trivial, but out of scope of my knowledge. Please help me in completing the connection string. How do I make the database (data.mdf) file reachable/accessible?
I came across some articles which told to import the database and such (but where/why?), but I don't get it. When the database is in the App_Data folder, then why/how do I do that?
I am confused, please help.I'll highly appreciate step-by-step approach to fix it.Thanks.
EDIT (solution):
The solution given below is perfect. Apart from that this website/blog is worth checking.
-- http://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/asp-net-hosting-options-cs
And if you don't have SQL Management Studio, best way to install is instructed here:
-- http://blogs.msdn.com/b/bethmassi/archive/2011/02/18/step-by-step-installing-sql-server-management-studio-2008-express-after-visual-studio-2010.aspx
Step 1 - Create a DB Script from SQL Management Studio
You will need to firstly script off you database schema and data (not as scary as it sounds - follow the steps here http://blog.sqlauthority.com/2011/05/07/sql-server-2008-2008-r2-create-script-to-copy-database-schema-and-all-the-objects-data-schema-stored-procedure-functions-triggers-tables-views-constraints-and-all-other-database-objects/ ) .
Step 2 - Create your DB at HostGator and Import your DB Script
Create your database at HostGator and import your script file (Follow this guide here http://support.hostgator.com/articles/plesk/plesk-9/how-to-create-or-import-databases-plesk-9 ).
Step 3 - Update your connection string and deploy!
You'll need to update your connection string to be something like this (you will need to add your details).
<add name=”CRMConnectionString” connectionString=”Data Source=Server IP;Initial Catalog=DBName;User ID=UserName;Password=Pwd;” providerName=”System.Data.SqlClient”/>
This connection string was cribbed from this resource here http://asoftwaredeveloper.wordpress.com/2012/01/06/hostgator-web-hosting-and-mssql-db-access/
Then publish your website and upload your files. Its worth noting that you won't need to update your App_Data folder and its contents when you publish because you'll be pointing at the DB on their server not the one in your directory.

C# Connecting to a SQL server with a named instance?

A client of mine has told me the program I made for them won't connect to a SQL server named instance, I have a standard SQL server with no named instance so I'm wondering how I can test this. A named instance connection string look like the one below, could the backslash be were my code fails?
Driver={SQL Native Client};Server=myServerName\theInstanceName;Database=myDataBase;
My code is as follows:
sqlServer=s.Substring(keyword.Length,s.Length-keyword.Length);
FormODBC formODBC=new FormODBC(this);
formODBC.SetSqlServer(sqlServer,dbUsername,dbPassword,database,table);
formODBC.ReadData();
How should I handle the backslash as I suspect this may be the problem?
Thanks
We have SQL servers with named instances.
Examples: myservername\sql2005.
Backslash is fine, in the conection string server name will be "myservername\sql2005", works 100% fine. You can have a "regular instance" on the same server, will be "myservername"
PS just unit test your function making connection string returns "myservername\sql2005".
Also, remember that backslash is an escape character in C# strings. If your instance name is contained in a string variable, make sure you do either:
string server = "myServer\\myInstance";
or
string server = #"myServer\myInstance";
The answer is to ensure your connection string is configurable, and set it to something like:
data source=localhost\msexpress;database=dbname;trusted_connection=true;
There is also the occassional SQL Express edition case where the name is MyServerName\SQLEXPRESS. This can trip up some people because they wouldn't think to specify the server that way.
You can extract your connection string to a config file (note this isn't necessarily secure - that will depend on how their SQL security server is set up and the account your application is running under) - then your client can just add the appropriate connection string for their server to your application's config when deployed.
Notes:
You can create a connection string by renaming a .txt file to a .udl file and running through until you can connect to your/their server.
Your unamed instance can actually be accessed by name - the machine name on which the SQL server is installed
Scott,
At the risk of stating the obvious, have you tried setting up a named instance in your own development environment? I don't actually know the answer to your question but I've never personally run into a solution where testing the scenario that is failing directly didn't help. At a minimum you ought to be able to get better debugging information as to precisely what is failing. Good luck getting this resolved.
Regards,
Chris

Categories