An exception of type 'System.Data.SqlClient.SqlException' occurred in
System.Data.dll but was not handled in user code
Additional information: Cannot open database
"aspnet-mywebsite-20150813211505" requested by the login. The login
failed.
Trying to connect on local .mdf database in visual studio 2013 - web forms.
Created database and c# code for connecting to database. Error shows when i try to execute :
using (SqlConnection con = new SqlConnection(#"Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-mywebsite-20150813211505;Integrated Security=SSPI;"))
{
SqlCommand cmd = new SqlCommand("select * from Users;");
cmd.Connection = con;
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Close();
}
Try this:
#"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-mywebsite-20150813211505.mdf;Integrated Security=True"
If this does't work, try to connect to this database using the "Add New Data Source" wizzard and copy the connection string from the auto generated connection string located in the app.config.
Related
When I try to connect to sql server from console application I get the following error for the first time (every time). If I run the application again then this error is not there.
Below is the code I am using
DataTable tblResults = new DataTable();
using (SqlConnection con = new SqlConnection(Common.DatabaseConnection))
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand(sqlQuery, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(tblResults);
con.Close();
}
The issue comes every time I run the application for the firsts time. IF I run the application again then the connection works.
Connection String
data source=sdfasdf.server.com;initial catalog=databasename;persist security info=True;user id=username;password=secret;
First off, i know this has been asked a million times but i just cannot figure it out what is going wrong.
I have made a local database using Microsoft Management Studio and i want to connect to it through C#. Heres my code:
string connStr = "Server=MSSQLSERVER;Database=UrenRegi;IntegratedSecurity=yes;Uid=auth_windows;";
MySqlConnection conn = new MySqlConnection(connStr);
conn.Open();
MySqlCommand cmd = new MySqlCommand("SELECT * From Users", conn);
MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds, "LoadDataBinding");
dataGrid.DataContext = ds;
conn.Close();
The error: Additional information: Unable to connect to any of the specified MySQL hosts.
Im using windows authentication because i know for sure that it is working.
Microsoft SQL Server is not MySQL. Use SqlConnection instead of MySqlConnection (same for all other classes).
I am developing a Class Library in C# and in this Class Library I am trying to access database through ADO.NET code, But I'm getting this error. I don't know what is the reason behind it. So please tell me how can I solve it.
System.Data.SqlClient.SqlException: An attempt to attach an auto-named database for file C:\Users\vivek.nuna\Documents\VisualStudio2005\Projects\SubsystemSyncService\TestClient\bin\Debug\aspnetdb.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
This is the connection string I am using.
<connectionStrings>
<add name="RegistrationConnString"
connectionString="Server=192.168.101.145\SQLEXPRESS;Database=***_HubDB;User Id=sa;Password={C8273EFD-LB2F-4E65-8702-14B61PI08A9}"
providerName="System.Data.SqlClient" />
</connectionStrings>
Note: I can't see a file aspnetdb.mdf in the Debug folder.
This is the code, how I am using ado.net code.
private DataSet GetAddressFieldsAccordingtoAddressId(string strAddressId)
{
try
{
strConnString = ConfigurationManager.ConnectionStrings["RegistrationConnString"].ToString();
SqlConnection connection = new SqlConnection(strConnString);
SqlCommand command = new SqlCommand();
command.CommandType = System.Data.CommandType.StoredProcedure;
command.CommandText = "[dbo].[PL_spPLUIGetAddressFieldsAccordingtoAddressId]";
command.Parameters.Add("#lAddressID", System.Data.SqlDbType.Int).Value = Convert.ToInt32(strAddressId);
command.Connection = connection;
DataSet dsPwd = new DataSet();
SqlDataAdapter dAdapter = new SqlDataAdapter(command);
dAdapter.Fill(dsPwd);
command.Dispose();
dAdapter.Dispose();
return dsPwd;
}
catch (Exception ex)
{
return null;
}
}
I have added reference of this class Library in a C# form.
C# form is calling this method of class library.
If you want a standalone database for your application, you should have a look at
SQL Server Compact : http://www.microsoft.com/sqlserver/2008/en/us/compact.aspx
SQLite : http://sqlite.phxsoftware.com/
For connectionstrings see http://www.connectionstrings.com/access-2003/.
SqlCeConnection sqlConnection1 = new SqlCeConnection();
sqlConnection1.ConnectionString = "Data Source = C:\\Users\\Administrator\\My Documents\\BMS_Data.sdf;Persist Security Info=False";
I can't find the mistake in my code below.When I use SQLDataSource to connect my database,there's no error.However,if I try to write the custom code to connect the database using the same connection string SQLDataSource uses,I encounter this error:
(provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
I suppose there's no mistake in my code,but in the configuration settings of SQL 2008 Express.
Thanks in advance...
SqlConnection sqlcon = new SqlConnection();
sqlcon.ConnectionString = "Data Source=ERHANEMREEROGLU/SQLEXPRESS;Initial Catalog=KET;Integrated Security=True";
sqlcon.Open();
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.Connection=sqlcon;
sqlcmd.CommandText = "SELECT * FROM Login";
sqlcmd.CommandType = CommandType.Text;
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
Shouldn't this read:
sqlcon.ConnectionString = "Data Source=ERHANEMREEROGLU\\SQLEXPRESS;Initial Catalog=KET;Integrated Security=True";
There problem is in the statement in sqlcmd.ExecuteNonQuery();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Login", sqlcmd))
DataTable t = new DataTable();
adapter.Fill(t);
Actualy you are executing a query and you need either SqlDataReader or SqlDataAdapter here.
Try to check if your SQL server allows remote connections, you can set that in the sql server configuration manager
and then enable the remote TCP connection
You should change your line from:
sqlcmd.ExecuteNonQuery();
to:
SqlDataReader dr = sqlcmd.ExecuteReader();
If you are using Windows Authentication on your SQL database you may need to change the Integrated Security to SSPI? Although I'm not overly sure. - edit reading some information about this and SSPI is equivalent to True, so see suggestions below instead.
sqlcon.ConnectionString = "Data Source=ERHANEMREEROGLU/SQLEXPRESS;Initial Catalog=KET;Integrated Security=SSPI";
But as John Blade said you may also need to check in the Configuration Manager that it accepts remote connections.
Also make sure you've added the Windows user to the database. You can do this by using the SQL Mangement Studio Tools.
You are executing ExecuteNonQuery(); which is for Insert/update/delete statements. You may fill a DataReader using ExecuteReader or a dataset/datatable using SQLDataAdapter.
SqlDataReader reader = sqlcmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(String.Format("{0}", reader[0]));
}
You can try:
using (SqlConnection sqlcon = new SqlConnection(
"Data Source=ERHANEMREEROGLU\\SQLEXPRESS;Initial Catalog=KET;Integrated Security=True"))
{
sqlcon.Open();
using (SqlDataAdapter a = new SqlDataAdapter("SELECT * FROM Login", sqlcon ))
{
DataTable t = new DataTable();
a.Fill(t);
}
}
I added an external Data Source to my C# app (I've placed it on the root directory in a folder named 'data') but when I run the program I get an error at con.Open():
An attempt to attach an auto-named database for file C:\Users\alex\documents\visual studio 2010\Projects\Network_Remote_Monitoring\Network_Remote_Monitoring\bin\Debug\data\bd.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
Here is my source code :
con = new System.Data.SqlClient.SqlConnection();
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\data\\bd.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
ds1 = new DataSet();
ds2 = new DataSet();
con.Open();
string sql = "SELECT * From localitati";
da = new System.Data.SqlClient.SqlDataAdapter(sql, con);
da.Fill(ds1, "localitati");
sql = "SELECT * From sucursale";
da = new System.Data.SqlClient.SqlDataAdapter(sql, con);
da.Fill(ds1, "sucursale");
con.Close();
I think you have a same problem as this question: connect to .mdf file in vs2010 , wpf application
I answer it there, but it's just my guess. because it is usual problem.
Does the account that is running your application have access to the data folder?