OleDbConnection with MsAccess using C# - c#

I'm trying to connect ms access database using c#. While doing that I'm stuck at one place.
Whenever i try to open the connection using
OleDbConnection conn = new OleDbConnection("connection string valid");
conn.Open();
I get error that whichever is the provider, it is not registered onto the local machine !
I have downloaded the msaccessEngine also from well known link http://www.microsoft.com/en-us/download/details.aspx?id=13255.

Related

Connect to a SQL Server database without a set file path

I am attempting to connect to a local SQL Server database in C#.
I am currently using the following connection string:
connectionString = #"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\User\source\repos\majorWork\majorWork\gameStats.mdf;Integrated Security=True";
However, I do not want to use a hardcoded file path, as I wish to be able to use the application across multiple computers, where the file path will be different. How should I go about doing this?
Best way is set this connection in Web.Config file.
<Database>
<ConnectionString name="connection">Server=servername; Initial Catalog=dbname; Persist Security Info=False; User ID=username; Password=password; MultipleActiveResultSets=False; Encrypt=True; TrustServerCertificate=False; Connection Timeout=30;;</ConnectionString>
</Database>
Then add Add System.Configuration as a reference.
in C# you can call this
string constring = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
After that you can create new connection instance by passing this to
SqlConnection con = new SqlConnection(constring)
If u install SQL server express using the default instance, then you can connect using . as your server name anyone can use it with default instance as well.
1.then in visual studio, click on solution explorer
2. Connect database, follow the instruction for SQL server
3. When it gets to server name use . to connect and choose your database name which you have created already in ms SQl, then test your connection
4. After it successful, u can now click on the database name showing under solution explorer,
5.after u click the database name, at the button right corner, there will be a connection string, copy it and use
This will be declared publicly or globally
Sqlconnection con = new sqlconnection("paste the connection string");
And to use
Sqlcommand cmd = new sqlcommand("insert into......",con);
Con.open ();
Cmd.executenonquery();
Con.close();

Local Database fails and crashes in VS2017

I'm trying to set up a local database (Service-based Database) in my C# app. When I intially adds the database to the project, I can see it and edit tables in both VS and SSMS, but when I try to connect to it programmatically, I get this error:
"Cannot open database \"[DataDirectory]DATABASE.MDF\" requested by the login. The login failed.\r\nLogin failed for user 'DESKTOP-MM6AR72\\majbom'."
And after that, the database is gone from both VS and SSMS.
I'm connecting this way:
using(SqlConnection conn = new SqlConnection("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=[DataDirectory]DATABASE.MDF;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"))
{
using(SqlCommand cmd = new SqlCommand(SQL, conn))
{
cmd.Connection.Open();
}
}
I have never tried to use databases this way, so maybe I'm doing something wrong. What I'm trying to accomplish, is a way of storing a lot af data temporarily in my app and make it searchable and editable.
EDIT:
I've followed this guide: https://learn.microsoft.com/en-us/visualstudio/data-tools/create-a-sql-database-by-using-a-designer
Thanks in advance

c# windows, local database(sql server), connection string not working properly

This is my connection string
SqlConnection conn = new SqlConnection(#"Data Source=C:\Users\admin\documents\visual studio 2010\Projects\WFA1\WFA1\database.sdf");
I copied it from the property, I added # in front but didn't change anything. There are no errors during Build and Run. But when I click insert button, the below error shows up.
"A network related or instance specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. "
I tried editing connection string and using datadirectory instead of the entire path, but the same error occurs, I am new to .net so I am unable to figure out what could be the cause. I tried google but I can't tell whether the solutions there are related to my problem.
Thanks in advance!
You're using the wrong type of SQL connection. You should be using the SqlCeConnection class:
SqlCeConnection conn = new SqlCeConnection(#"Data Source=C:\Users\admin\documents\visual studio 2010\Projects\WFA1\WFA1\database.sdf");
Here is a really good reference for SQL CE connection strings.
The data source for SQL Server is a machine name, not a file name.
You should add a reference to the SQL CE assembly, and do this:
SqlCeConnection conn = new SqlCeConnection(#"Data Source=C:\Users\admin\documents\visual studio 2010\Projects\WFA1\WFA1\database.sdf");
For MSSQL Server Compact Edition, you should use the SqlCeConnection class.
So this should work:
SqlCeConnection sqlConnection = new SqlCeConnection(#"Data Source=C:\Users\admin\documents\visual studio 2010\Projects\WFA1\WFA1\database.sdf");
Create you connection string as follow :
add in the namespace using System.Data.SqlServerCe;
SqlCeConnection conn = new SqlCeConnection(#"Data Source=C:\Users\admin\documents\visual
studio 2010\Projects\WFA1\WFA1\database.sdf; Persist Security Info=False;");
if .sdf file inside you project then you can use :
string strConnectionString ="Data Source=" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.
GetExecutingAssembly().GetName().CodeBase) + "\\database.sdf;Persist Security Info=False;";
SqlCeConnectionconn = new SqlCeConnection(strConnectionString );

Getting 12154: TNS:could not resolve the connect identifier specified error with .NET

I've written this simple connection in my code:
OracleConnection con = new OracleConnection("Data Source=ORCL;User
ID=agapus;Password=agap");
con.Open();
Then I run my application pressing F5 and I get the above error. This error occurs no matter if it's just a simple console app or an ASP.NET web site. I've tried both Oracle.DataAccess and the old windows oracle API. I've tried several Oracle services, all with no luck.
The environment where I'm testing this connection is a Windows Server 2008R2. I can easily connect to any database using SQLPlus or TOAD. I've tried explicitly specifying the location of the TNS_NAMES.ORA file, doesn't work either.
The only thing that works is when I specify the full path in the connection string. So if I change the above code into the below one, it'll work:
string conString = "user id=agapus;password=agap;data source=(DESCRIPTION=(ADDRESS=
(PROTOCOL=tcp)(HOST=172.16.0.121)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL)))";
ID=agapus;Password=agap");
OracleConnection con = new OracleConnection(conString);
con.Open();
If you look in your sqlnet.ora file, I believe that you will find that the default domain is set to .world .
That would make the correct name of your connection ORCL.world .
You may also have .world on the end of your connection name in TNSNAMES.ora, and nothing for the default domain.
What do you get when you run tnsping?

Connecting to SQL Server database with Mono C#

I am trying to migrate my service cross-platform with mono, but in attempting to connect to a SQL Server database I get the following timeout error
Timeout expired. The timeout period elapsed prior to the completion of the operation or the server is not responding. at Mono.Data.Tds.Protocol.TdsComm..ctor
at System.Data.SqlClient.SqlConnection.Open()
Databases are fairly new to me, but as far as I can tell from here (Google cache page, mono site is down) accessing SQL Server databases is now possible in Mono. Is that correct?
I attempted to structure my connection string as shown, but still no luck. My simple test code...
using (SqlConnection con = new SqlConnection(cs))
{
con.Open();
SqlDataReader reader = null;
SqlCommand cmd = new SqlCommand("SELECT Parameter FROM Deltas", con);
reader = cmd.ExecuteReader();
reader.Read();
Console.WriteLine(reader["Parameter"].ToString());
con.Close();
}
Am I missing any references or is my format incorrect? How can I connect using Mono C#?
EDIT:
Connection String, Defined globally and init in constuctor:
cs = #"Server=xxx.xxx.xxx.xxx;
Database=myDB;
User ID=user;
Password=passwd;";
cs = #"Data Source=xxx.xxx.xxx.xxx;
Network Library=DBMSSOCN;
Initial Catalog=myDB;
User ID=user;
Password=passwd;";
Top is me trying to conform to the mono example, bottom is what works with the .NET runtime.
It seems you hit a bug:
http://www.mail-archive.com/mono-bugs#lists.ximian.com/msg50686.html
quote:
This only happens when using the .NET 2.0 version of TdsComm - when
compiling with .NET 1.0 (mcs instead of gmcs), the connection also
works.

Categories