Insert Query into postgresql using Table Adapter C# - c#

I am trying to insert data into a postgresql table in visual studio windows forms table adapter using this query;
INSERT INTO PUBLIC .cashaccount
VALUES (:cashmemo, :cashcredit, :cashdebit)
I am using the ':' because I am aware the '#' operator does not work in postgresql but I am still getting a syntax error. I have googled this issue and I am yet to find a postgresql insert command with variables. Does anyone have an idea on how to make the above statement work?

I believe the syntax error your are getting is due to the space in your table name as pointed below
INSERT INTO PUBLIC .cashaccount
^----Here
VALUES (:cashmemo, :cashcredit, :cashdebit)
Also, read through Npgsql(.NET Postgresql provider) for more information with lots of examples.

Related

How to update a sql database?

Does somebody know how to update a SQL database using a windows form?
I mean, I have a function to make the connection and when I use the select function it works, but I can't modify it.
I'm using this to add a new element "insert into [database name] ([name of the row]) values ([value I want to insert])"
please visit SQL Database Tutorial: Understanding Your Database Structure to understand how actually database store records.
Refer few syntax that may help you
to insert/update records in table
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
for update records in table
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
I had an question exactly about this thread asked by me -->How to take ID numbers from a SQL Server table using Visual C#.
Maybe it can help you it is worth having a look at it.

Oracle Table column name having special( Danish) characters. Unable to execute sql query

I am using DbDataReader to execute query. Please find the code below
Query is:
select BRANCH_NAME, AMOUNT, ACCOUØNT_ID from ACCOUNT
Error is:
ORA-00904: "ACCOU?NT_ID": invalid identifier .
I am trying to connect oracle database and execute the oracle query.
Please help by how to execute the oracle query which columns have special characters.
Quite possibly
select "BRANCH_NAME", "AMOUNT", "ACCOUØNT_ID" from "ACCOUNT";
will work.
See http://docs.oracle.com/cd/E11882_01/server.112/e10592/sql_elements008.htm for more information on Database Object Naming Rules.

Can't insert to sqlite using c#, "id's are not unique" but in the sqlite app it works

My SQL query is
string stringSQL = "Insert into table(clientid, contractorid, driverid)
values (1,1,last_insert_rowid() + 1)"
ExecuteNonQuery(stringSQL)
And the error I get is:
Error: abort due to constraint
violation(clientid, contractorid,
driverid are not unique.
Btw those columns are my primary keys!
Is there an issue using SQLite's functions in c# vs 2010?
Thx I advance
From the last_insert_rowid() documentation:
The last_insert_rowid() function returns the ROWID of the last row insert from the database connection which invoked the function.
Note the "from the database connection" part... which means if there's already data in your table before you open this connection, presumably it's going to start from 0 or 1 again, and end up with a conflict. In other words, I don't think you can use this as a general way of incrementing row IDs.
I'd expect this to work if the table was empty before opening your current database connection, and if that connection is the only thing to have inserted data into that table though... you might want to test that part, just to make sure we understand what's going on.
That's assuming the docs are correct, of course - I've never actually used sqlite myself...

SQL command problems (C# Winforms)

I am successfully connecting to my sql 2008 server hosted on winhost.com. But I am following this tutorial: http://www.codeproject.com/KB/database/sql_in_csharp.aspx which was suggested in an answer from: Connecting to SQL Server Database C#-WinForms and I keep getting the exact same error when I try to:
Insert something into the table.
Retrieve something from the db.
The error is: "Incorrect syntax near the keyword 'table'.".
I don't know what's wrong. The error message is very vague, and everything seems to look fine.
I am using all the examples from the above tutorial, but they all give the same error.
Any suggestions? Does anyone have any other tutorials/articles for me I can have a look at?
Thank you
Where it says INSERT INTO table ... you have to change table to be the actual name of your table as it says in the text just beneath:
Now we will take a look at the values. table is simply the table within the database.
If you chose to call your table table then you can write [table] but it would be better to change the table name to something else.
Where table appears in that tutorial, it's meant to be a 'placeholder' for an actual table name - table by itself is an illegal table name - hence the syntax error. If you need to use this name then [table] would be fine.
TABLE is a reserved word, try surrounding it with brackets, if you have created a table called table.
[table]

Using 'Auto-Sync' feature of DBML with SQLite

I'm having trouble trying to use the 'Auto-Sync' feature of DBML with SQLite. I have a class in my data model that contains a primary key (id). This key is defined as "INTEGER PRIMARY KEY", which SQLite maps to the rowid of the row. To support this, I have set "Auto Generated Value" to 'True' and "Auto-Sync" to 'OnInsert'.
The problem is, when I commit a new entry to the database, the SELECT string used by the LINQ to SQL classes is not supported by SQLite:
SELECT CONVERT(BigInt,SCOPE_IDENTITY()) AS [value]
Instead, SQLite has the last_insert_rowid() function, which I cannot seem to point to.
Does anyone know how to remedy this? Possibly by changing the statement used to select the last row ID or some other trick I'm missing?
EDIT There appears to be some traffic on the provider's website, but no resolutions.
EDIT Since I seem to have confused the question, here's how I've set up my application. Hopefully it helps shed some light on my thought process, and maybe an underlying issue.
Add new "LINQ to SQL Classes" file to my solution
Model my database in the designer, named DataModel
Open a database using a System.Data.SQLite.SQLiteConnection
Initialize the DataModel instance using this connection
You are adding a "Linq-to-SQL" data model to your project, but you're using it against SQLite - that'll never work of course! Linq-to-SQL only ever supports SQL Server (always has, always will), and thus its SQL statements that it generates are SQL Server T-SQL Statements - nothing else.
If you want to use the Entity Framework with SQLite, you need to use "ADO.NET Entity Data Model" (file with the .EDMX extension) as your data model.
Only that will support third-party database drivers like SQLite and others!
Devart implementation of LINQ to SQLite does not contain this problem. The autoincrememnt column is treated correctly.
I ran into the same issue and found the following workaround - use the SQLiteConnection.Changed event to intercept the query and fix it up:
SQLiteConnection.Changed += (obj, eventArgs) => {
var cmd = eventArgs.Command;
if (eventArgs.EventType == SQLiteConnectionEventType.NewDataReader && cmd != null)
{
cmd.CommandText = cmd.CommandText.Replace( #"SELECT CONVERT(Int,SCOPE_IDENTITY()) AS [value]"
, #"; SELECT last_insert_rowid() AS value");
}
};

Categories