Microsoft Access OleDb Connection Error - c#

I am trying to access an Access 2003 database remotely from my ASP.NET application. My code is:
DataSet dsImportedData = new DataSet();
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
conn.ConnectionString = #"Provider=MS Remote;Remote Provider=Microsoft.Jet.OLEDB.4.0;Remote Server=http://myIp;Data source=C:\myDatabase.mdb;";
try
{
System.Data.OleDb.OleDbCommand command = conn.CreateCommand();
command.CommandText = "SELECT * FROM myTable";
conn.Open();
System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(command);
adapter.Fill(dsImportedData);
}
catch (Exception ex)
{
}
finally
{
conn.Close();
}
However, I am always getting an exception stating: {"[Microsoft][ODBC Microsoft Access Driver] Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'."}
My command is basic, I have no idea what could be wrong with it. Did anyone confront with the same issue? Thanks!

Try this ....
String command = "SELECT * FROM myTable";
conn.Open();
System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(command, conn);
adapter.Fill(dsImportedData);

Apparently the error can be caused by the specified table not existing. Just a thought...
Another thought would be to remove the remoting complexity and try to get to the most simple working code, possibly with a new access database just to start to narrow down what is causing the problem.

If you set the command type to Stores procedure it works for me.
command.CommandType = System.Data.CommandType.StoredProcedure;

Related

"ORA-00942 table or view does not exist" exception in c# code

I am trying to access Oracle synonyms from my c# code. But it is throwing exception ORA-00942: table or view does not exist. I tried out all the possible ways that I could find but failed to achieve so. Can anyone suggest me how I can access synonyms?
Here's what I want to do:
string retrieveData="select * from syn_empdetails"; //syn_empdetails is the synonym
using (OracleConnection sqlcon = new OracleConnection())
{
sqlcon.ConnectionString = connectionString;
try
{
sqlcon.Open();
sqlcmd = sqlcon.CreateCommand();
sqlcmd.CommandText = retrieveData;
OracleDataReader dr = sqlcmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
Console.WriteLine("{0}\t{1}\t{2}\t{3}",dr.GetString(0),dr.GetString(1),dr.GetString(2),dr.GetString(3));
}
}
dr.Close();
sqlcon.Close();
Please suggest how I can access this synonym.
Note: This query runs fine in sql developer. All the connections and schema are also fine.
Is this due to privilege issue or something else and how can I solve it?

Couldn't delete all rows of a table from Microsoft Access via OleDB in C#

I had a method in which I intend for it to delete all data from the table. However, even as it is called, the deletion didn't happen at all.
Here below is the method.
Let's assume that the data is already loaded in the table.
Table "CartListClone" has 5 columns (excluding the ID). The table as you can see in the connection string derives from Access.
public void deleteEverything() {
OleDbConnection connect =
new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=POSDB.accdb;
Persist Security Info = False");
connect.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connect;
command.CommandText = "DELETE * FROM CartListClone";
}
As of now, I feel that the problem is rooted at the method.
Is there something that I did wrong here?
Much appreciated for any help.
UPDATE: Following sstan's suggestion, here below is the rewritten method.
public void deleteEverything() {
OleDbConnection connect =
new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=POSDB.accdb;
Persist Security Info = False");
connect.Open();
OleDbCommand command = new OleDbCommand("DELETE * FROM CartListClone");
command.Connection = connect;
command.ExecuteNonQuery();
}
You're never executing the command object. You're missing this line at the end:
command.ExecuteNonQuery();

how to delete data in Sql Server 2012 using c#?

i want to delete data in my database and using this code but its now working
private static void DeletePreviousRecord()
{
string connectionString = "Data Source=ABDULLAH\\ABDULLAHZAFAR;Initial Catalog=FoodHunt;Integrated Security=True";
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand("Delete From RestaurantsMenu", con))
{
try
{
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
var result = cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{ }
}
}
}
i tried this but this is not working, how can i do that, any suggestion?
Setting the CommandType to StoredProcedure when you clearly use a sql text directly cannot do any good at your code.
Remove that line because the default is CommandType.Text (and this is correct for your command)
But as stated in the comment above.
If you catch the exception, at least write in some log or display at
video what the error message is
If you don't add a WHERE clause at your sql statement, you delete
everything in the table (Probably you are lucky that this code has
not worked)
Looking at your comment below, if you want to delete every record (and reset the Identity column if any) a faster approach is
using (SqlCommand cmd = new SqlCommand("TRUNCATE TABLE RestaurantsMenu", con))
For a quick reading about the difference between TRUNCATE and DELETE look at this article

Inserting data into sql server fails with no reason

I'm quite used to using c# with SQL server. I have no idea why a simple statement would fail to insert data. My code is as follows:
query = "INSERT INTO MCDPhoneNumber ([MCDID],[PhoneNumber])" +
"VALUES("+maxid+", '"+tel+"')";
SqlConnection conn = new SqlConnection("Data Source=source; ...");
SqlCommand newCommand = new SqlCommand(query, conn);
int success= myCommand.ExecuteNonQuery();
if (success!= 1)
{
MessageBox.Show("It didn't insert anything:" + query);
}
First of all let me tell that I know that I should use parameters for data and I initially did, but when it failed I tried a simple query and it still fails. For addition I can tell that I have a similar insert just before that one in another table and it works. What's funnier is that when I copy paste query to SQL Server Management Studio it works. It also doesn't report any error in process.
====================== Edit ===============================
If you wish to use old command object (i.e. myCommand) then use following code instead of creating a new command(newCommand)
myCommand.CommandText = query;
myCommand.CommandType = System.Data.CommandType.Text;
And then execute it
you are binding query with newCommand and executing myCommand.
====================== Edit ===============================
SqlCommand newCommand = new SqlCommand(query, conn);
here you have defined newCommand for SQLCOMMAND object
int success= myCommand.ExecuteNonQuery();
and you are accessing it as myCommand
And moreover i think you are not opening connection
First of all, you define your command as newCommand but you executing your myCommand.
You should always use parameterized queries for your sql queries. This kind of string concatenations are open for SQL Injection attacks.
query = "INSERT INTO MCDPhoneNumber (MCDID, PhoneNumber) VALUES(#maxid, #tel)";
using(SqlConnection conn = new SqlConnection("Data Source=source; Initial Catalog=base; Integrated Security = true"))
{
SqlCommand newCommand = new SqlCommand(query, conn);
conn.Open();
newCommand.Parameters.AddWithValue("#maxid", maxid);
newCommand.Parameters.AddWithValue("#tel", tel);
int success= newCommand.ExecuteNonQuery();
if (success != 1)
{
MessageBox.Show("It didn't insert shit:" + query);
}
}
And please be more polite about your error messages :)

How to call a Oracle stored procedure (PL/SQL) from .NET?

I am new to Oracle. There is a requirement in our firm for a project using Oracle and .Net. So I was trying to run a demo application. I am using Oracle 10g XE as DB and VS2010.
I wrote a procedure with a simple select query, which got compiled (got this procedure format by googling).
I ran the stored procedure from the SQL command prompt from XE Dashboard itself. This was the result:
Now I wrote code in C# to call that stored procedure:
string sqlCon = "Data Source=xe;Persist Security Info=True;User ID=sa;Password=password;Unicode=True;Provider=OraOLEDB.Oracle;";
OleDbConnection Con = new OleDbConnection(sqlCon);
OleDbCommand cmd = new OleDbCommand();
DataSet ds = null;
OleDbDataAdapter adapter;
try
{
Con.Open();
////Stored procedure calling. It is already in sample db.
cmd.CommandText = "TESTPROC";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = Con;
ds = new DataSet();
adapter = new OleDbDataAdapter(cmd);
adapter.Fill(ds, "Users");
return ds.Tables[0];
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
This code block didn't throw any exceptions. It got executed but what I received was an empty Dataset. But when I tried to fetch data using the query directly I got the result.
So, is the way I am trying to access the stored procedure correct? or Is there any mistake in my stored procedure? Can anyone point out the error or the best way to do this?
Thanks in advance.
Suggestion: try declaring "result" as an "out" parameter:
https://forums.oracle.com/forums/thread.jspa?messageID=4566433
Here's one other link that might help:
http://support.microsoft.com/kb/309361
There is another link that might help:
http://www.akadia.com/services/ora_return_result_set.html

Categories