I have a example: I am using C# to solve Oracle database problem
cmd = new OracleCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select DATE_START from EMPLOYEE;";
It works successfull.
BUt when I use GROUP BY, it doesm't work.
cmd = new OracleCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select DATE_START from EMPLOYEE GROUP BY DATE_START;";
It doesn't work
ORA-00911: invalid character.
Thanks so much
Try removing the semicolon(;) from CommandText like
cmd.CommandText = "select DATE_START from EMPLOYEE GROUP BY DATE_START";
//^^ From Here
I am not sure why it should be a problem, but I have seen similar issue before with Oracle + ADO.Net.
Also, if you are not using any aggregate methods then you can use DISTINCT keyword instead of GROUP BY like:
cmd.CommandText = "SELECT DISTINCT DATE_START from EMPLOYEE";
Related
I am new in C# syntax and trying out Insert data into two different table when user hit the Insert button. At the same time, 2nd Insert statement need to grab the 1st Insert statement id as it's foreign key. Primary key id is auto increment.
This sql works in PHP but seems not in C# and I get this throw error message Npgsql.PostgresException: '55000: currval of sequence "student_folio_id_seq" is not yet defined in this session'.
Can any expert in C# Postgres db give me advice where goes wrong in syntax? And good if can specify with examples.
Here is my code:
connection.Open();
NpgsqlCommand cmd = new NpgsqlCommand();
cmd.Connection = connection;
cmd.CommandText = "INSERT INTO student_folio
(f_name,l_name,ic,dob,gender,remark)VALUES(#f_name,
#l_name,#ic,#dob,#gender,#remark) RETURNING id";
cmd.CommandText = "INSERT INTO contact (s_id, address)VALUES((SELECT
currval(pg_get_serial_sequence('student_folio', 'id'))),#address) ";
cmd.CommandType = CommandType.Text;
seems you are trying to re assign your cmd.CommandText with new dml value. Try this.
connection.Open();
NpgsqlCommand cmd = new NpgsqlCommand();
cmd.Connection = connection;
cmd.CommandText = "INSERT INTO student_folio (f_name,l_name,ic,dob,gender,remark)VALUES(#f_name, #l_name,#ic,#dob,#gender,#remark) RETURNING id; " +
"INSERT INTO contact (s_id, address)VALUES((SELECT currval(pg_get_serial_sequence('student_folio', 'id'))),#address);";
cmd.CommandType = CommandType.Text;
I'm trying to all the sql function from the c#.
I've had added the parameters to the function as # fileid and useid. But getting an error. I couldn't figure out the bug in my code..
string query = "exec fn_sh_HitInfo(#fileid,#UserID)";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("#fileid", SqlDbType.BigInt).Value=fid;
cmd.Parameters.Add("#UserID", SqlDbType.BigInt).Value =UserID;
string taglist = string.Empty;
try
{
taglist = DBAction.ExecuteQuery(cmd, FilteringControl.ProjectID).ToString();
}
Don't use parentheses
exec fn_sh_GetTagListHitInfo #fileid,#UserID
Or if this is a scalar UDF you can alternatively use SELECT instead of EXEC (requires schema name in this case)
SELECT dbo.fn_sh_GetTagListHitInfo (#fileid,#UserID )
You don't need the exec part and you don't need to know the correct syntax.
//string query = "exec fn_sh_GetTagListHitInfo(#fileid,#UserID)";
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure; // just mark it as an SP
cmd.CommandText = "fn_sh_GetTagListHitInfo"; // only the name
cmd.Parameters.Add("#fileid", SqlDbType.BigInt).Value=fid;
cmd.Parameters.Add("#UserID", SqlDbType.BigInt).Value = FilteringControl.UserID;
Change the cmd.CommandText value and try.
cmd.CommandText = "select fn_sh_GetTagListHitInfo(#fileid,#UserID)";
This is code segment that I have written in C#. Mobile and Name are columns in my table.
The problem is that there is something wrong with format of my query. Is the syntax correct if we want to connect two queries in C # using OR?
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [Contact Management] WHERE
Mobile='"+Convert.ToInt32(txtSearch.Text)+"' OR Name='"+txtSearch.Text+"'",con);
No, that syntax is not correct. It's vulnerable to sql injection attacks. You need to build it like this:
SqlCommand cmd = new SqlCommand("SELECT * FROM [Contact Management] WHERE
Mobile= #Search OR Name= #Search")
SqlDataAdapter = new SqlDataAdapter(cmd);
cmd.Parameters.Add("#Search", SqlDbType.NVarChar, 50).Value = txtSearch.Text;
You could also write the query this way:
SELECT * FROM [Contact Management] WHERE #Search IN (Mobile, Name)
As usual, never use string concatenation to build sql command. Use parametrized queries
string query = "SELECT * FROM [Contact Management] WHERE Mobile=#mobile OR Name=#name";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("#mobile", Convert.ToInt32(txtSearch.Text));
cmd.Parameters.AddWithValue("#name", txtSearch.Text);
SqlDataAdapter da= new SqlDataAdapter (cmd);
The parametrized query will save your database from Sql Injection Attacks, but also from problems in parsing your input text. What if in the search text you have a single quote? You will get a syntax error with concatenation.
However, let me say that your code will fail before this. If you have a number in your txtSearch, then everything will work, but if you have a string. converting to a number with Convert.ToInt32 will fail. Better to use
SqlCommand cmd = new SqlCommand();
string query;
int numSearch;
if(Int32.TryParse(txtSearch.Text, out numSearch))
{
query = "SELECT * FROM [Contact Management] WHERE Mobile=#p1";
cmd.Parameters.AddWithValue("#p1", numSearch);
}
else
{
query = "SELECT * FROM [Contact Management] WHERE Name=#p1";
cmd.Parameters.AddWithValue("#p1", txtSearch.Text);
}
cmd.CommandText = query;
....
I run this code:
SqlConnection polaczenie;
polaczenie = new SqlConnection(dane_polaczenia);
SqlCommand cmd = new SqlCommand();
cmd.Connection = polaczenie;
polaczenie.Open();
cmd.CommandText = #"SELECT * FROM test.test";
cmd.ExecuteNonQuery();
After executing I got exception Invalid object name 'test.test'.
Database and table values are correct.
How to specify database.table in CommandText correctly?
cmd.CommandText = #"SELECT * FROM test.dbo.test";
or
cmd.CommandText = #"SELECT * FROM test..test";
I want to know how to execute more than one SQL command at once.
At this moment I'm doing it like this:
using (SqlConnection sqlConnection1 = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT nome FROM teste";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
// execute the command
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
listBox1.Items.Add(rdr["name"].ToString());
}
}
}
But how can I do to execute
use [databaseX]
SELECT nome FROM teste
in my c# program?
Separate multiple statements with a semicolon (;).
(BTW, the use statement is generally not needed because it's set in your connection string.)
Use # to declare the string:
cmd.CommandText = #"
use [databaseX]
SELECT nome FROM teste
";
OR actually escape the line break:
cmd.CommandText = "use [databaseX]\nSELECT nome FROM teste";
SQL uses a ; to seperate commands / queries.
SELECT * FROM Table1; SELECT * FROM Table2;
results in to result sets.