SQL Server connection problem - c#

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?

Related

Read a dbase IV file C#

I am developing an application to recover data from a DBF file.
I did research on the Internet that sent me to this link : enter link description here
I applied this code but nothing helps it doesn't work :/
string constr = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Test\users.dbf;Extended Properties=dBASE IV;User ID=;Password=MyPassword;";
using (OleDbConnection con = new OleDbConnection(constr))
{
var sql = "select * from users.dbf";
OleDbCommand cmd = new OleDbCommand(sql, con);
con.Open();
DataSet ds = new DataSet(); ;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(ds);
}
I pass this code in a Try and Catch and it returns me this error: Unable to start your application. The workgroup information file is missing or opened exclusively by another user.
the error is caused when trying to open the connection. However the file is neither opened nor used by anyone else.
thank you in advance ;)
Try to remove the file name in the connection string. According to the documentation, The "Data Source" property should only contain the path.
string constr = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Test;Extended Properties=dBASE IV;User ID=;Password=MyPassword;";

Unable to connect to any of the specified MySQL hosts. error with C#

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).

cannot open database, on code execution - shows exception

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.

An attempt to attach an auto-named database for file \bin\Debug\aspnetdb.mdf failed

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

Get data from the excel file using c# asp.net

I am trying to load data using following code.
string path = System.IO.Path.GetFullPath(uploadExcelFile.PostedFile.FileName);
string connString = "provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Rizwan shahid\\Desktop\\DataUpload\\Req.xls;Extended Properties=Excel 12.0;";
OleDbConnection oledbConn = new OleDbConnection(connString);
try
{
oledbConn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn);
OleDbDataAdapter oleda = new OleDbDataAdapter();
oleda.SelectCommand = cmd;
DataSet ds = new DataSet();
oleda.Fill(ds, "Table");
return ds.Tables[0];
}
catch
{
return null;
}
finally
{
oledbConn.Close();
}
It was working on 32Bit operating system but when run this code on 64Bit OS it gives the following error
The Microsoft Access database engine could not find the object 'Sheet1$'. Make sure the object exists and that you spell its name and the path name correctly. If 'Sheet1$' is not a local object, check your network connection or contact the server administrator.
I am running VS in Administrator mode and found many solution like replace Sheet1 with file name or place file in C drive but still getting the same error.
you can download latest version here of Jet
http://www.microsoft.com/en-us/download/search.aspx?q=jet
This works (I start out with a 'dummy' path and then apply the real runtime path):
OleStringBuilder =
new OleDbConnectionStringBuilder(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';");
OleStringBuilder.DataSource = MapPath(#"~\App_Data\MyExcelWorksheet.xls");

Categories

Resources