I have 2 tables which are the customers table and orders table.
Basically my problem is that it always give me an error which is 'unknown field list'.
I have tried in phpmyadmin XAMPP it works the only problem is that when I use it in VS studio c# it gives me an error.
I'm confused the back ticks '`' don't work but the back ticks work on localhost.
Please help me.
This is my sql syntax:
SQL = "SELECT o.`Order ID`, o.Description, o.Amount
FROM tbl_orders AS o
INNER JOIN tbl_customers AS c ON o.`Order ID` = c.`Order ID`
WHERE c.`Customer ID` = '" + cust_id + "'";
cmd.Connection = dbCon;
cmd.CommandText = SQL;
rdr= cmd.ExecuteReader();
dt.Load(rdr);
Have tried deleting and retyping the ` around the column names incase it didnt copy correctly. Also try putting a # before the string so.
SQL = #"<the sql>";
this works on mysql xampp
MySqlConnection sqlConnection = new MySqlConnection(#"Server=localhost;Database=test;");
var sql = #"SELECT o.`Order ID`, o.Description, o.Amount
FROM tbl_orders AS o
INNER JOIN tbl_customers AS c ON o.`Order ID` = c.`Order ID`
WHERE c.`Customer ID` = 1";
sqlConnection.Open();
MySqlCommand sqlCommand = new MySqlCommand(sql,sqlConnection);
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
Columns with spaces in their name should be enclosed in square brackets.
Try
SQL = "SELECT o.[Order ID], o.Description, o.Amount
FROM tbl_orders AS o
INNER JOIN tbl_customers AS c ON o.[Order ID] = c.[Order ID]
WHERE c.[Customer ID] = '" + cust_id + "'";
cmd.Connection = dbCon;
cmd.CommandText = SQL;
rdr= cmd.ExecuteReader();
dt.Load(rdr);
http://tinyurl.com/qcf45vf
What is the use of the square brackets [] in sql statements?
Related
What's wrong?
SqlCommand cmd = new SqlCommand(#"Update Perioada p join Client c on p.ID_Client = c.ID_Client
SET p.Date ='" + dateTimePicker1.Value.ToString("MM/dd/yyyy") + "' WHERE (c.CNP = '" + textBox1.Text + "')", con);
cmd.ExecuteNonQuery();
Assuming you are using SQL server (because you are using system.data.sqlclient), below is the correct syntax for update statement:
string sqlQuery = "Update p SET p.Date =#dt from Perioada p join Client c on p.ID_Client = c.ID_Client WHERE (c.CNP = #cnp)"
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(sqlQuery, con);
command.Parameters.AddWithValue("#dt", dateTimePicker1.Value.ToString("MM/dd/yyyy"));
command.Parameters.AddWithValue("#cnp", textBox1.Text);
try
{
con.Open();
command.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
On a side note, you should use SqlParameter to pass input control's values to the sql server, instead of manually creating sql query with appended values. Your way of creating query is prone to SQL injection attack.
EDIT: Edited answer to depict a way to use parameterised query
I need to query a database for the name and schema name for all the stored procedures.
Here is what I've got so far but I do not know how to get the schema name.
What is the field name for the schema name when querying the sysobjects table?
private DataTable GetProcedures()
{
var table = new DataTable();
string sql = "";
sql = "select name,'' as type ";
sql += " From sysobjects ";
sql += " where type = 'P' ";
sql += " and name not like 'dt_%' ";
sql += " order by name asc";
using (SqlConnection conn = new SqlConnection(cs))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(table);
}
}
}
return table;
}
Use the sys.objects table joined to the sys.schemas table like this:
SELECT schemas.name, objects.name
FROM sys.objects
JOIN sys.schemas ON schemas.schema_id = objects.schema_id
so to find all stored procedures (taking your query as a start), it looks like this:
SELECT schemas.name, objects.name
FROM sys.objects
JOIN sys.schemas ON schemas.schema_id = objects.schema_id
WHERE type = 'P'
AND objects.name NOT LIKE 'dt_%'
As additional information, you should no longer use sysobjects as it has been replaced with sys.objects, same goes for the other sys* tables.
Maybe this can help you :
SELECT name AS object_name
,SCHEMA_NAME(schema_id) AS schema_name
,type_desc
,create_date
,modify_date
FROM sys.objects
WHERE type = 'P'
ORDER BY name asc;
Hello I'm trying to SELECT multiple rows from table and INSERT them into another I thought that it can be done as following:
This part should select multiple rows:
string sqcom = "SELECT text,castka,rocnik FROM zajsluz WHERE akce='"+tentoradek+"' and rocnik='"+klientClass.Rocnik()+"'";
SqlCommand sc = new SqlCommand(sqcom,spojeni);
spojeni.Open();
sc.ExecuteNonQuery();
spojeni.Close();
This is how I try to INSERT selected rows from SqlCommand sc:
string sqlcom2 = "INSERT INTO zajsluz(akce,text,castka,rocnik) values (#akce,#text,#castka,#rocnik)";
SqlCommand sc2 = new SqlCommand(sqlcom2, spojeni);
sc2.Parameters.AddWithValue("#akce", klientClass.Rocnik());
sc2.Parameters.AddWithValue("#text", ); // I dont know how to define this parameter according to what was selected in SqlCommand sc
spojeni.Open();
sc2.ExecuteNonQuery();
spojeni.Close();
Now I'm wondering hwo can I insert into "#text" (sc2) parameter values from SqlCommand "sc" would you please help me solve this out?
Thanks in advance
Edit: ยจ
this is what I tried:
DataSet dt2 = new DataSet();
SqlDataAdapter SDA2 = new SqlDataAdapter("SELECT text,castka FROM zajsluz WHERE akce='" + tentoradek + "' and rocnik='" + klientClass.Rocnik() + "'", spojeni);
SDA2.Fill(dt2);
spojeni.Close();
string sqlcom2 = "INSERT INTO zajsluz(akce,text,castka,rocnik) values (#akce,#text,#castka,#rocnik)";
SqlCommand sc2 = new SqlCommand(sqlcom2, spojeni);
sc2.Parameters.AddWithValue("#akce", zakce.Text);
sc2.Parameters.AddWithValue("#rocnik", klientClass.Rocnik());
sc2.Parameters.AddWithValue("#text", dt2.Tables[0].Columns["text"]);
sc2.Parameters.AddWithValue("#castka", dt2.Tables[0].Columns["castka"]);
spojeni.Open();
sc2.ExecuteNonQuery();
spojeni.Close();
You can directly use insert into & select combination
string sqcom = "INSERT INTO zajsluz(akce,text,castka,rocnik) SELECT rocnik,text,castka,rocnik FROM zajsluz WHERE akce='"+tentoradek+"' and rocnik='" + klientClass.Rocnik() + "'"
SqlCommand sc = new SqlCommand(sqcom,spojeni);
spojeni.Open();
sc.ExecuteNonQuery();
spojeni.Close();
I would try to do this in a single statement if that is possible, i.e. you aren't doing anything to the data in between the two statements.
string sqlcom = "INSERT INTO zajsluz(akce,text,castka,rocnik) SELECT akce,text,castka,rocnik FROM zajsluz WHERE akce='"+tentoradek+"' and rocnik='"+klientClass.Rocnik()+"'";
SqlCommand sc = new SqlCommand(sqcom,spojeni);
spojeni.Open();
sc.ExecuteNonQuery();
spojeni.Close();
Another option would be to use a SQL DataSet/DataTable, which allows you to query and return from SQL an entire table, or a set of rows, that you can then update, delete or insert into. It's described in the following MS article: http://support.microsoft.com/kb/326009/en
This summary answer for your question:
StringBuilder query = new Stringbuilder();
query.AppendLine("INSERT INTO zajsluz(akce,text,castka,rocnik) ");
query.AppendLine("(SELECT #akce, text, castka, #rocnik");
query.AppendLine("FROM zajsluz WHERE akce=#Tentoradek");
query.AppendLine("AND rocnik=#rocnik)");
SqlCommand sc2 = new SqlCommand(sqlcom2, spojeni);
sc2.Parameters.AddWithValue("#Tentoradek", tentoradek);
sc2.Parameters.AddWithValue("#akce", zakce.Text);
sc2.Parameters.AddWithValue("#rocnik", klientClass.Rocnik());
spojeni.Open();
sc2.ExecuteNonQuery();
spojeni.Close();
Suppose I have a table in SQL "abc" and with in it there is a column "number" and this column contains (1,2,3,4) etc.
In my second table "xyz" I have a column "number" and this column contains (1,2,3,4,5,6,7,8,9).
Now I want to compare these two and insert equal data within third table.
So how can I do this?
code :
string str = "SELECT Invoice_Details.PGI_ID, PARTY_BOOKING_DETAILS.PGI_ID AS abc ";
str += "FROM PARTY_BOOKING_MAIN INNER JOIN ";
str += " PARTY_BOOKING_DETAILS ON PARTY_BOOKING_MAIN.PBM_ID = PARTY_BOOKING_DETAILS.PBM_ID CROSS JOIN ";
str += " Invoice_Details where PARTY_BOOKING_MAIN.PM_ID = 1 ";
SqlConnection con = new SqlConnection("data source = .; database = ePartyDatabase01; integrated security = true");
con.Open();
SqlCommand cmd1 = new SqlCommand("update Invoice_Details set [status] = #a", con);
SqlCommand cmd = new SqlCommand(str, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (dr["PGI_ID"].ToString() == dr["abc"].ToString())
{
cmd1.Parameters.AddWithValue("#a", 1);
}
}
dr.Close();
cmd1.ExecuteNonQuery();
con.Close();
You probably want to use some kind of this query:
insert into table3 (number)
select
t1.number
from abc t1
inner join xyz t2
on t1.number = t2.number
I haven't tested this yet, but you might not even need C#, try something like this:
SELECT xyz.number
INTO third_table
FROM (
SELECT t1.number
FROM first_table t1 JOIN second_table t2 ON t1.number = t2.number
) AS xyz
Maybe something like this:
INSERT INTO table3(number)
SELECT
number
FROM
xyz
WHERE NOT EXISTS
(
SELECT
NULL
FROM
abc
WHERE
abc.number=xyz.number
)
I get a error when trying to inner join databases from a MySqL server.
I get this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'lines INNER JOIN snippets ON snippets.id = lines.snippetid_fk WHERE lines.snippe' at line 1
Im Coding in ASP.Net C#.
This is my function that gives the error:
MySqlConnection conn = new MySqlConnection();
string mysql = "Server=***;Database=***;User=***;pwd=***";
conn.ConnectionString = mysql;
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "SELECT * FROM lines " +
"INNER JOIN snippets ON snippets.id = lines.snippetid_fk " +
"WHERE lines.snippetid_fk = 1";
cmd.Connection = conn;
DataTable dt = new DataTable();
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
adapter.Fill(dt);
Repeater_codebank.DataSource = dt;
Repeater_codebank.DataBind();
According to documentation, lines is in the list of MySQL reserved words.
For your query, to work, you must add backticks around the table name:
cmd.CommandText = "SELECT * FROM `lines` " +
"INNER JOIN snippets ON snippets.id = lines.snippetid_fk " +
"WHERE `lines`.snippetid_fk = 1";