I have been working on a c# project that connects to a access database, but a certain sequence of events causes it to crash with a AccessViolationException
The issue comes after calling a database connection using oledb in a separate form than the savefiledialog, and than calling savefiledialog1.ShowDialog()
Note: This also applies to the open file dialog.
It might be a bug in Access Database Engine 2010. Use 2007 instead.
connect.microsoft.com: oledb-operations-cause-accessviolationexception-during-savefiledialog
Codeproject: OpenFileDialog + OleDbConnection = AccessViolationException
Be sure you are using System.Data.OleDb from System.data.dll
Then try something like this:
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
// Declare Command
OleDbCommand command = new OleDbCommand(YourSQL);
// Set the Connection to the new OleDbConnection.
command.Connection = connection;
// Open the connection and execute the command.
try
{
connection.Open();
command.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
// The connection is automatically closed when the
// code exits the using block.
}
I had a similar issue, too, and this helped me:
I added "OLE DB Services=-1" in my connectionstring, now the problem is solved.
See: http://www.codeproject.com/Questions/106826/OpenFileDialog-plus-OleDbConnection-equals-AccessV.aspx SOLUTION 8
Related
I have an asp.net C# application (.net 4.0) connected to SQL Server 2012 using ADO.Net and am encountering an error which says:
[InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.]
I very well know what a DataReader is but, my problem is getting this error in below conditions:
I have not at all used any DataReader in my application, I have only
used DataAdapters everywhere. The code works fine while running in
local environment and there is no errors.
Application works fine even after deployment in IIS7 when used by a
single user.
The error only occurs when multiple users starts using the website hosted in IIS7.
Kindly help, I am also doubting for any problems with my hosting in IIS7
After a lot of trial and error, finally I found out that it's a problem with SqlConnections. What I used to do was open a connection at the instantiation of my DAL layer object. So, whenever two methods from the same DAL object called together, it used to throw the error.
I solved it by opening and closing a connection in every call to the database. Now it all works fine. This also allows max number of users to use the application at a time. Below is my code sample from DAL:-
DataTable dt = new DataTable();
try
{
using (SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ToString()))
{
if (sqlcon.State == ConnectionState.Closed)
sqlcon.Open();
SqlCommand sqlCommand = new SqlCommand
{
Connection = sqlcon,
CommandType = CommandType.StoredProcedure,
CommandText = "MyStoredProc"
};
sqlCommand.Parameters.AddWithValue("#Parameter1", Parameter1);
using (SqlDataAdapter adapter = new SqlDataAdapter(sqlCommand))
{
adapter.Fill(dt);
}
}
return dt;
}
catch (Exception exp)
{
LogHelper.LogError(string.Concat("Exception Details: ", ExceptionFormatter.WriteExceptionDetail(exp)));
throw exp;
}
finally
{
dt.Dispose();
}
Please post a better way of doing it if you know any, thank you.
I'm new to C# and connecting to databases.
The problem is: whenever I try to connect to DB by running this code:
string connparams = "server=127.0.0.1;uid=root;pwd=12345;database=test;";
try
{
MySqlConnection connection = new MySqlConnection(connparams);
}
catch (System.ArgumentException me)
{
Console.WriteLine(me.ToString());
}
connection.Open();
connection.Close();
I get the window "The application is in break mode"
When I take a look at the events window I can see an exception with description "No source code". When I try to see the implementations of functions in MySQLConnection I can see none of them, only declarations. So what should I do/redo/reinstall in order to solve this? Using VS2017, .NET Framework 4.6.1, Connector/NET (MySQL)
I have a very strange case of SQL connection timeout from an application written in C# .NET.
The SqlCommand.ExecuteNonQuery() is being used to sequentially execute several scripts in SQL Server, one after another. Each script contains a command to create just one table (no data update/insert/delete operations at all). For some reason, at one of the scripts, the SqlCommand.ExecuteNonQuery throws a timeout exception.
When I execute creation of all these tables in SQL Server Management Studio, they get executed just fine and almost instantaneously.
Does anyone has an idea what could be causing timeout when the tables are created from the application?
All sql scripts look similar like following:
SQL:
create table dbo.Test
(
Code varchar(10) not null
, Name varchar(50) not null
, other columns...
primary key
, unique key
, foreign key
)
The scripts are shipped from C# using this code:
try
{
using (SqlConnection conSQL = new SqlConnection ("[connection string]"))
{
using (SqlCommand cmdSQL = new SqlCommand(sSQL, conSQL))
{
cmdSQL.CommandTimeout = iTimeOut;
conSQL.Open();
cmdSQL.ExecuteNonQuery(); // this is where it jumps to catch part and
// throws out timeout exception
conSQL.Close();
}
}
}
catch(Exception ex)
{
throw (ex);
}
This is happening on the Test server, meaning nothing else is happening on the server while the application is executing these scripts.
You can override the default time out setting for sql transactions by updating the machine.config, which can be found here:
%windir%\Microsoft.NET\Framework\[version]\config\machine.config
64-bit
%windir%\Microsoft.NET\Framework64\[version]\config\machine.config
At the end of the machine.config add or update the following line:
<system.transactions>
<machineSettings maxTimeout="01:00:00" /> --> set this to desired value.
</system.transactions>
</configuration>
If the above doesn't work, you can specify the Timeout setting for SqlCommand through code as well:
using (SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
SqlCommand command = new SqlCommand(queryString, connection);
// Setting command timeout in seconds:
command.CommandTimeout = 3600;
try {
command.ExecuteNonQuery();
}
catch (SqlException e) {
Console.WriteLine(e);
}
}
more information here
Just stop it from running and run again your problem will be solved.... It is generally occur first time only when their are lot of files to load maybe it is bug in visual studio.
New:
After stop try to refresh open Web page (in browser where error displays ) instead of relaunch it again...
I am developing a WPF application.
I created a local SQLite database (using System.Data.SQLite):
SQLiteConnection conn = new SQLiteConnection("Data Source=test.db");
And need to fill it with a MySQL database located in a server. I already got to download the .sql dump file from the server, the problem is that how can i import this file to my local database?
I already searched on the web and found this question the problem is that this mention android...
Any information is welcome.
UPDATE 1:
Opening the .sql file and executing a SQLite command does not import the .sql, here is my code:
string lines = "";
try
{
using (StreamReader sr = new StreamReader(databaseFile))
{
lines += sr.ReadToEnd();
}
} catch (Exception)
{
}
SQLiteConnection conn = new SQLiteConnection("Data Source=test.db");
conn.Open();
var command = conn.CreateCommand();
command.CommandText = lines;
command.ExecuteNonQuery();
conn.Close();
The application crashes with the next error:
Additional information: SQL logic error or missing database
near "AUTO_INCREMENT": syntax error
They are different SQL dialects so that solution is going to give you quite a bit of a headache.
If this is a one time activity I would use a (free) tool like:
http://www.sql-workbench.net/
that has a Data Pump tool that allows you to move data across different databases.
Ok, so I've been looking everywhere on how to connect a microsoft access database to a C# command line application. I have experience in visual studio when I was doing visual basic so I connected the database to the project via the "add new data source" menu. Now I'm here I have no clue how to declare the connection and open it. I know the basics of SQL code so that's not really a problem right now, it's just the connecting part. Looking around, I've found this:
using System.Data.SqlClient;
string connectionString = null;
connectionString = ;
SqlConnection cnn ;
cnn = new SqlConnection(connectionString);
try
{
cnn.Open();
Console.WriteLine("Connection Open!");
cnn.Close();
}
catch (Exception ex)
{
Console.WriteLine("Error: Connection Cannot be Opened!");
}
But I have no clue what this does. If this is wrong can someone please correct me and explain it. Please keep it as simple as possible.
You simply use OleDb* instead of Sql*. ie:
string connectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\MyFolder\Northwind.accdb";
using( OleDbConnection cnn = new OleDbConnection(connectionString))
{
try
{
cnn.Open();
Console.WriteLine("Connection Open!");
cnn.Close();
}
catch (Exception ex)
{
Console.WriteLine("Error: Connection Cannot be Opened!");
}
}
PS: I wouldn't recommend you using access as if it is a database. It is not a database. It is the wolf disguised like a sheep. If you don't care about your data then OK.
Setting the connectionString = null won't work!
See: http://www.connectionstrings.com/ for help on connection strings. This is really a great site.
And if you want a connection to Access, you need an OleDbConnection, not a SqlConnection. The latter is used for connections to SQL-Server.
You can find an example here: Getting values back from OleDbDataReader reading from Access database.