sql 2012 express and vs2012 - c#

How can I connect Visual Studio 2012 to SQL Server 2012 express on localhost. My server name :
HACEGAN\SQLEXPRESS
what must I write to my connection string i.e
SqlConnection con = new SqlConnection("Data Source=localhost\\HACEGAN.SQLEXPRESS;Initial Catalog=Register;User ID=sa;Password=123");

Just write your server name to your Data Source part ?
Data Source=HACEGAN\SQLEXPRESS;Initial Catalog=Register;User ID=sa;Password=123
Change your
localhost\\HACEGAN.SQLEXPRESS
to
HACEGAN\SQLEXPRESS
Check out: Visual Studio 2012 and MS Sql Server 2012 - connect with Server Explorer
You can find Server Explorer in Visual Studio 2012 -> View -> Server Explorer

You shouldn't be specifying both a servername & localhost. One or the other.
SqlConnection con = new SqlConnection(#"Data Source=HACEGAN\SQLEXPRESS;Initial Catalog=Register;User ID=sa;Password=123");
Please note the # symbol to prevent escaping issues.

If your sql server is also a default instance, you can use . notation:
Server=.;Database=Register;User Id=sa;Password=123;
If non-default, use .\SQLExpress.

i think you write it directly,
Data Source=HACEGAN\\SQLEXPRESS;Initial Catalog=Register;User ID=xxx;Password=xxx

All other answers in this post are correct, but in your case you should write:
SqlConnection con = new SqlConnection("Data Source=HACEGAN\\SQLEXPRESS;Initial Catalog=Register;User ID=sa;Password=123");

Related

Unable connect to SQL Server from Winforms application after deploy

My application is working in debug mode or from Visual Studio. But after deployment / publish, we are not able to connect to the database from setting file connection string.
BILLING_SYSTEM.Settings setting = new BILLING_SYSTEM.Settings();
We are using
string connectionString = setting.ConnectionString_Web.ToString()
SqlConnection conn = new SqlConnection(connectionString);
Connection String is:
Data Source=.;Initial Catalog=<databaseName>;Integrated Security=True
Its working on local. But after deploy on another machine not able to connect database.
Is there any method in set wizard which resolve my problem?
Please suggest. Thanks in advance.

Connot connect to .dbf file in C#

I'm trying to connect access *.dbf file in ASP.NET select some data and display in html. However I cannot make connection. I suppose, the problem lays in the connection string.
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = #"Provider=Microsoft.Jet.OLEDB.12.0;Data Source=c:\Data;Extended Properties=dBASE 5.0;User ID=Admin;Password=;";
conn.Open();
I get the following error:
ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not
found and no default driver specified
Unfortunately, none none of the solutions posted in older questions solved my problem.
I'm using Visual Studio Community 2015, Windows 10 64-bit.
I guess, that the Provider may be wrong. However I'm not so good in this topic, so I don't know how to do this in a proper way. I'd be very thankful for some step-by-step instruction and for any useful advice.
the *.dbf file is one of the files from the archive here:
DBF File
Jet 4.0 works in 32 bit mode only. You'll need to update your application to run in 32 bit mode. Check this information from connectionstrings.com about running jet in 32 bit mode on a 64 bit machine.
You can see this reference from connectionstrings.com that shows the microsoft dbf connection. It appears to use roman numerials instead of 5.0 as you are using for the dbase version.
I managed to connect with this code, but needed to instal VFPOLEDB driver.
private DataTable myTable()
{
OleDbConnection conn = new OleDbConnection("Provider=VFPOLEDB.1;Data Source=c:\\Data\\;Extended Properties=dBASE IV;User ID=;Password=;");
conn.Open();
string query = "SELECT * FROM d:\\data\\Earthquakes1970";
OleDbCommand cmd = new OleDbCommand(query, conn);
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
conn.Close();
return dt;
}
You should try this. I am running these code with windows 10. and visual studio 2010.
Put your database files in App_Data folder. and use following connection string.
it is working for me.
web.config file
<connectionStrings>
<add name="conn" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|;Extended Properties=dBASE IV;User ID=Admin;Password=;" />
</connectionStrings>

VS 2010 C# Create Setup File with SQL Server database

I'm trying to create a working installer or setup file for an application I created using C# in VS 2010.
I used InstallShield and also the built-in one and they do created the setup files and I installed the app with no problem.
But when I ran the program, this thing pops up:
My database connections:
On the forms:
SqlConnection cn = new SqlConnection(#"SERVER=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;DATABASE=Database;Integrated Security=True;User Instance=True");
App.Config:
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"
What could be wrong? Should my connection string on the form the same as the one in the app.config file?
Visual Studio 2010 Ultimate
SQL Server 2008 Express
It looks like you ran the installer on your development box.
If you use AttachDbFilename in the connection string and you omit the option Database from the connection string the sqlclient will try to create a database with the filename as the name of the database. This fails if the database already exists.
You need to make sure that your SqlConnection uses the connection string from the app.config:
SqlConnection cn = new SqlConnection(
ConfigurationManager.
ConnectionStrings["MyDatabaseNameInConfig"].
ConnectionString);
In your app.config you'll need:
<connectionStrings>
<add name="MyDatabaseNameInConfig"
connectionString="Database=PRODUCTION;Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf; ...."/>
</connectionStrings>
A way to fix this is to add a Database parameter to your connectionstrings for release builds:
connectionString="Database=PRODUCTION;Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf; ...."
You can automate that in Visual Studio with XML Document Transform
Another option is to change your setup to make these changes on install but I'm not sure how easy that is.
A final solution might be to have your application accept a special startup argument to update the connectionstrings. The installer can start your application with that special argument at the end of the setup.

Access DB connected with local directory

Basically I want the connection of my Access DB always available, so if i move the folder project to another computer it has to keep working, without changing the folder path.
I'm working with Windows Forms in Visual Studio 2012.
conn.ConnectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data source= Z:\Tempesta\Area Progetto\Area_Progetto_27_02_2014\Area_Progetto_DATA_MAGAZINE\Data_Magazine\Data_Magazine\DB\DataMG.mdb";
That's the code I have right now for the connection to the DB.
Try this
conn.ConnectionString = #"Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=|DataDirectory|DataMG.mdb"

this is error ORA-12154: TNS:could not resolve the connect identifier specified?

I've this code :
OracleConnection con = new OracleConnection("data source=localhost;user id=fastecit;password=fastecit");
con.Open(); string sql="Select userId from tblusers";
OracleCommand cmd = new OracleCommand(sql, con);
OracleDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{ messageBox.Show(dr[0].Tostring()); }
Same code in both projects,
in project1 "WinForm" Is Working correctly
in project2 "Excel 2007 addins" Following error appears:
ORA-12154: TNS:could not resolve the connect identifier specified
I'm using C#.net 2010 ,office 2007 , windows8, oracle 10g.
When preparing a manual connection to the database, as shown in the picture
Visual Studio, open View menu + Server Explorer.
Right mouse click on Data Connection + Add Connection + Select Oracle Database
server Name : localhost or name of my machine, set username & password and click on Test Connection, test is no succeeds.
ORA-12154: TNS:could not resolve the connect identifier specified?
In case the TNS is not defined you can also try this one:
If you are using C#.net 2010 or other version of VS and oracle 10g express edition or lower version, and you make a connection string like this:
static string constr = #"Data Source=(DESCRIPTION=
(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=yourhostname )(PORT=1521)))
(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=XE)));
User Id=system ;Password=yourpasswrd";
After that you get error message ORA-12154: TNS:could not resolve the connect identifier specified then first you have to do restart your system and run your project.
And if Your windows is 64 bit then you need to install oracle 11g 32 bit and if you installed 11g 64 bit then you need to Install Oracle 11g Oracle Data Access Components (ODAC) with Oracle Developer Tools for Visual Studio version 11.2.0.1.2 or later from OTN and check it in Oracle Universal Installer
Please be sure that the following are checked:
Oracle Data Provider for .NET 2.0
Oracle Providers for ASP.NET
Oracle Developer Tools for Visual Studio
Oracle Instant Client
And then restart your Visual Studio and then run your project ....
NOTE:- SYSTEM RESTART IS necessary TO SOLVE THIS TYPES OF ERROR.......
The database must have a name (example DB1), try this one:
OracleConnection con = new OracleConnection("data source=DB1;user id=fastecit;password=fastecit");
In case the TNS is not defined you can also try this one:
OracleConnection con = new OracleConnection("Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=DB1)));
User Id=fastecit;Password=fastecit");
run the below command in command prompt
tnsping Datasource
This should give a response like below
C:>tnsping *******
TNS Ping Utility for *** Windows: Version *** - Production on *****
Copyright (c) 1997, 2014, Oracle. All rights reserved.
Used parameter files:
c:\oracle*****
Used **** to resolve the alias
Attempting to contact (description=(address_list=(address=(protocol=tcp)(host=)(port=)))(connect_data=(server=)(service_name=)(failover_mode=(type=)(method=)(retries=)(delay=))))**
OK (**** msec)
Add the text 'Datasource=' in beginning and credentials at the end.
the final string should be
Data Source=(description=(address_list=(address=(protocol=tcp)(host=)(port=)))(connect_data=(server=)(service_name=)(failover_mode=(type=)(method=)(retries=)(delay=))));User Id=;Password=;**
Use this as the connection string to connect to oracle db.

Categories