SQL Server 2012 : Between issue with parameters - c#

I am having an issue with the query below:
SELECT
tT.RequestID,
TStatus,
TLead,
RequestDate,
tG.Category,
tETM.RAgency,
ECost,
tTT.TType
FROM
tT AS tT
INNER JOIN
tTMain AS tETM ON tT.RequestID = tETM.RequestID
INNER JOIN
tTType AS tTT ON tETM.AMod = tTT.TypeID
INNER JOIN
tGup AS tG ON tT.Category = tG.CategoryID
WHERE
tT.active = 1
AND (ApproxDate BETWEEN '09/30/2016' AND '09/30/2017')
AND tT.Category = 3
I am currently getting an error:
Conversion failed when converting date and/or time from character string.
The way I populate the above query in code:
string theQ = "SELECT " +
"tT.RequestID, " +
"TStatus, " +
"TLead, " +
"RequestDate, " +
"tG.Category, " +
"tETM.RAgency, " +
"ECost, " +
"tTT.TType " +
"FROM " +
"tT AS tT " +
"INNER JOIN " +
"tTMain AS tETM " +
"ON " +
"tT.RequestID = tETM.RequestID " +
"INNER JOIN " +
"tTType AS tTT " +
"ON " +
"tETM.AMod = tTT.TypeID " +
"INNER JOIN " +
"tGup AS tG " +
"ON " +
"tT.Category = tG.CategoryID " +
"WHERE " +
"tT.active = #val1 " +
"AND " +
"(ApproxDate BETWEEN #val2 AND #val3) " +
"AND " +
"tT.Category = #val4"
//Split up the between check. (it looks like this: BETWEEN 'xx/xx/xxxx' AND 'xx/xx/xxxx')
string[] betweenSplit = Regex.Split(Session["between"].ToString(), " AND ");
SqlDataAdapter da = new SqlDataAdapter();
DataSet _ds1 = new DataSet();
command = new SqlCommand(theQ, con);
command.Parameters.AddWithValue(1, "1");
command.Parameters.AddWithValue(2, betweenSplit[0].Replace(" BETWEEN ", ""));
command.Parameters.AddWithValue(3, betweenSplit[1]);
command.Parameters.AddWithValue(4, Session["UCat"].ToString());
da = new SqlDataAdapter(command);
da.Fill(_ds1, dbName);
da.Dispose();
closeAllConnections();
command.Parameters.Clear();
The error occurs on the line:
da.Fill(_ds1, "tripData");
What would I be doing incorrectly because the same query above works just fine when running it in Server Management Studio?

I suggest to avoid implicit conversion of date to/from string.
In your case it could be (for example):
CONVERT (datetime, '09/30/2016',101)

You should pass Datetime parameter to SQL like that
var yourFromDate = DateTime.Now;
SqlParameter fromDate = new SqlParameter("#val2", SqlDbType.DateTime) {Value = yourFromDate};
command.Parameters.Add(fromDate);

Related

Variable in C# SQL query not working

I have a loop in which I grab certain ID's to make a call in a database. There are 2 variables within the query.
The first one works fine but the second one returns nothing. I have tested it a lot and know that the correct value is coming through to the query. Not sure what I am doing wrong here. I replace the variable with a hard coded value that I know is returning and it works fine.
Here is my code:
SqlDataAdapter d8;
d8 = new SqlDataAdapter("SELECT SUM(CAST(AMOUNT AS BIGINT)) AS NEW_AMOUNT
FROM ddb_proc_log_base
WHERE (PROVID = " + docId +
" AND CHART_STATUS = 90
AND YEAR(PLDATE) = 2016
AND CLASS = 2
AND ORD = " + defer + ") OR (ORD = " + defer +
" AND PROVID = " + this.getDocHygDS.Tables[0].Rows[t]["HYG_ID"] +
" AND CHART_STATUS = 90
AND YEAR(PLDATE) = 2016 AND CLASS = 2)", conn3);
cmdBuilder5 = new SqlCommandBuilder(d8);
d8.Fill(this.balances);
#Tyler Nichol You are missing the a single quote where you concatenate string value like
Example ORD = '" + defer + "'
below is an example:
try{
"select * from SomeTable where name='"+name+","
// in your case this may like the following
d8 = new SqlDataAdapter("select SUM(CAST(AMOUNT AS BIGINT)) AS NEW_AMOUNT
FROM ddb_proc_log_base where ( PROVID = "+docId+" AND CHART_STATUS = 90
AND YEAR(PLDATE) = 2016 AND CLASS = 2 AND ORD = '" + defer + "') OR (ORD = '"
+ defer + "' AND PROVID = " + this.getDocHygDS.Tables[0].Rows[t]["HYG_ID"]
+ " AND CHART_STATUS = 90 AND YEAR(PLDATE) = 2016 AND CLASS = 2)", conn3);
}
catch(Exception e)
{
//Throw Null Exception Here
}
Recommended Solution
try{
string UserName="John";
cmd.CommandText = "select * from SomeTable where name=#Name";
cmd.Parameters.AddWithValue("#Name", UserName);
}
catch(Exception e)
{
//Throw Null Exception Here
}
You need to use a CONTAIN in the query :
DataSet getDocHygDS = new DataSet();
string[] hyg_id = getDocHygDS.Tables[0].AsEnumerable().Select(x => x.Field<string>("HYG_ID")).Distinct().ToArray();
string or = "'" + string.Join("' OR '", hyg_id) + "'";
SqlDataAdapter d8;
string query = string.Format("SELECT SUM(CAST(AMOUNT AS BIGINT)) AS NEW_AMOUNT" +
" FROM ddb_proc_log_base" +
" WHERE (PROVID = {0}" +
" AND CHART_STATUS = 90" +
" AND YEAR(PLDATE) = 2016" +
" AND CLASS = 2" +
" AND ORD = {1})" +
" OR" +
" (ORD = {1}" +
" AND CONTAINS(PROVID, {2})" +
" AND CHART_STATUS = 90" +
" AND YEAR(PLDATE) = 2016 AND CLASS = 2)", docId, defer, or);
d8 = new SqlDataAdapter(query, conn3);
cmdBuilder5 = new SqlCommandBuilder(d8);
d8.Fill(this.balances);

Using '#val3' or #val3 in SQL Server query parameters

I am just wondering what is the correct way for sending this old query statement:
"SELECT " +
"* " +
"FROM " +
"tE " +
"WHERE " +
"active = 1 " +
"AND " +
"StartDate = " + Session["between"] + " " +
"AND " +
"UPPER(I1) = '" + Session["StrUser"].ToString().ToUpper() + "'"
To this way (correct way of passing the parameters):
SqlDataAdapter da = new SqlDataAdapter();
DataSet _ds1 = new DataSet();
String blah = "SELECT " +
"* " +
"FROM " +
"tE " +
"WHERE " +
"active = #val1 " +
"AND " +
"StartDate #val2 " +
"AND " +
"UPPER(I1) = '#val3'"
command = new SqlCommand(blah, con);
command.Parameters.AddWithValue("#val1", 1);
command.Parameters.AddWithValue("#val2", Session["between"].ToString());
command.Parameters.AddWithValue("#val3", Session["StrUser"].ToString().ToUpper());
da = new SqlDataAdapter(command);
da.Fill(_ds1, dbName);
da.Dispose();
closeAllConnections();
command.Parameters.Clear();
return _ds1;
The main focus of this question is the '#val3'" part. Is that needed or will the command.Parameters.AddWithValue automatically place that if needed?
Using parameters is a good way of separating SQL code from data. As such, you should understand that the ' characters are part of a data literal. As such, unless you're wanting to compare I1 with the literal string #val3, and instead want to use the parameter, it should not be enclosed in 's.
This is correct:
String blah = "SELECT " +
"* " +
"FROM " +
"tE " +
"WHERE " +
"active = #val1 " +
"AND " +
"StartDate = #val2 " +
"AND " +
"UPPER(I1) = #val3"
will the command.Parameters.AddWithValue automatically place that if needed?
No, it will not - because you're no longer trying to construct a literal value, no 's are required at all. Parameters aren't just a fancy way of doing text replacement - they keep the code and data separate and, importantly, deal with any required conversions behind the scenes.

MySqlDataAdapter Update table that uses Inner Join

I'm tring to add the Insert, Update and Delete commands to a MySqlDataAdapter which has the data added to it with the command:
//iec211.4521studenti
string lv_sObsTable = "`" + mv_sDatabase + "`.`" + mv_sGroup + "observatii`";
string lv_sStudTable = "`" + mv_sDatabase + "`.`" + mv_sGroup + "studenti'";
string lv_sObservatiiCommand = "SELECT o.idObservatie, o.idStudent, s.nume, o.observatie, o.lastUpdate FROM " + lv_sObsTable + " o INNER JOIN " + lv_sStudTable + " s USING (idStudent) ORDER BY s.nume ASC";
mv_dtAdapterObservatii = new MySqlDataAdapter(lv_sObservatiiCommand, mv_sqlConnection);
mv_dtAdapterObservatii.Fill(mv_dsTables, lv_sCurrentTable);
I've serched for an answer and I've found two possible solutions for UPDATE, but both of them give me an error:
string lv_sObsTB = "`" + mv_sDatabase + "`.`" + mv_sGroup + "observatii`";
string lv_sStudTB = "`" + mv_sDatabase + "`.`" + mv_sGroup + "studenti`";
//VERSION 1
string lv_sCommand = "UPDATE o SET s.idStudent=#idStudent, o.observatie=#observatie FROM " + lv_sObsTB + " AS o INNER JOIN " + lv_sStudTB + " s ON o.idStudent=s.idStudent ";
MySqlCommand lv_sqlCommand = new MySqlCommand(lv_sCommand);
lv_sqlCommand.Parameters.Add("#idStudent", MySqlDbType.Int16, 10, "idStudent");
lv_sqlCommand.Parameters.Add("#observatie", MySqlDbType.VarChar, 255, "observatie");
mv_dtAdapterObservatii.UpdateCommand = lv_sqlCommand;
//VERSION 2
string lv_sCommand = "UPDATE " + lv_sObsTB + " SET " + "idStudent=#" + lv_sStudTB + ".idStudent, observatie=#observatie WHERE idObservatie=#idObservatie AND " + lv_sObsTB + ".idStudent IN (SELECT " + lv_sStudTB + ".idStudent FROM " + lv_sStudTB + ")";
MySqlCommand lv_sqlCommand = new MySqlCommand(lv_sCommand);
lv_sqlCommand.Parameters.Add("#" + lv_sStudTB + ".idStudent", MySqlDbType.Int16, 10, "idStudent");
lv_sqlCommand.Parameters.Add("#observatie", MySqlDbType.VarChar, 255, "observatie");
lv_sqlCommand.Parameters.Add("#idObservatie", MySqlDbType.Int16, 10, "idObservatie");
mv_dtAdapterObservatii.UpdateCommand = lv_sqlCommand;
The error message for the first version is : "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 'FROM iec211.4521observatii ASS o INNER JOIN iec211.4521studenti s ON o.id' at line 1"
The error message for the second version is: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 '.4521studenti'.idStudenti, observatie='test' WHERE idObservatie=1 ANDiec211.' at line 1"
I don't know how to use the MySQL commands in combination with MySqlCommand and MySqlDataAdapter.
Until now I've used the following command to obtain data for my table:
string lv_sObservatiiCommand = "SELECT * FROM `" + mv_sDatabase + "`.`" + mv_sGroup + "observatii` ORDER BY idStudent ASC";
mv_dtAdapterObservatii = new MySqlDataAdapter(lv_sObservatiiCommand, mv_sqlConnection);
mv_dtAdapterObservatii.Fill(mv_dsTables, lv_sCurrentTable);
In combination with MySqlCommandBuilder to get the update command and the parameters that needed to be setted:
lv_sqlCommandBuilder = new MySqlCommandBuilder(mv_dtAdapterObservatii);
mv_dtAdapterObservatii.UpdateCommand = lv_sqlCommandBuilder.GetUpdateCommand();
Can someone please explain to me how to correctly add the insert, update and delete commands to a MySqlDataAdapter that contains an INNER JOIN between 2 tables ?
I know that each table must be in a different data adapter, and they are. The "4521studenti" table is inside "mv_dtAdapterStudenti", which is not included in the code above.
Thank you for the help and I hope this won't be considered as a bad post. :)
Okey. I think I've figured it out.
This is the SELECT command for 4521observatii:
string lv_sStudent = "`" + mv_sDatabase + "`.`" + mv_sGroup + "studenti`";
string lv_sObservatii = "`" + mv_sDatabase + "`.`" + mv_sGroup + "observatii`";
string lv_sObservatiiCommand = "SELECT o.idObservatie, o.idStudent, s.nume, o.observatie, o.lastUpdate FROM " + lv_sObservatii + " o INNER JOIN " + lv_sStudent + " s USING (idStudent) ORDER BY s.nume ASC";
// Read data from the server for the current table and add it to the DataSet
mv_dtAdapterObservatii = new MySqlDataAdapter(lv_sObservatiiCommand, mv_sqlConnection);
mv_dtAdapterObservatii.Fill(mv_dsTables, lv_sCurrentTable);
And this is the code for the UPDATE command, inside a different method:
// `iec211`.`4521observatii`
string lv_sObsTB = "`" + mv_sDatabase + "`.`" + mv_sGroup + "observatii`";
// `iec211`.`4521studenti`
string lv_sStudTB = "`" + mv_sDatabase + "`.`" + mv_sGroup + "studenti`";
string lv_sSelectStudenti = "(SELECT idStudent FROM " + lv_sStudTB + " WHERE idStudent=#idStudent)";
string lv_sCommand = "UPDATE " + lv_sObsTB + " SET idStudent="+ lv_sSelectStudenti + ", observatie=#observatie WHERE idObservatie=#idObservatie";
MySqlCommand lv_sqlCommand = new MySqlCommand(lv_sCommand);
lv_sqlCommand.Parameters.Add("#idStudent", MySqlDbType.Int16, 10, "idStudent");
lv_sqlCommand.Parameters.Add("#observatie", MySqlDbType.VarChar, 255, "observatie");
lv_sqlCommand.Parameters.Add("#idObservatie", MySqlDbType.Int16, 10, "idObservatie");
mv_dtAdapterObservatii.UpdateCommand = lv_sqlCommand;
mv_dtAdapterObservatii.UpdateCommand.Connection = mv_sqlConnection;
The idea is that when I use INNER JOIN I bring data from two different tables and put them together. When I used the UPDATE command, the program didn't know which table he was using and gived those errors.
So I thought what if I update the values of 4521observatii and for the column that is used as FOREIGN KEY I assign the value from 4521studenti.
I know it's like "Tell me your name so I can tell you your name.", but it worked for me.
I hope this will help others and you have a better and cleaner solution, pleas tell me :)

SQL Insert Into Syntax Error using C#

I'm transferring information from one Access DB to another using C#. Using the following code generates an error but by putting the query in Access using the same values, I get no error. Can anyone see what is going on?
string sqlInfaction = "INSERT INTO AquiredInfractions" +
"(AgentID, DateID, InfractionID, STime, Durration, Exception) " +
"SELECT " +
"#ID AS AgentID, (SELECT D.DateID FROM DateCodes AS D WHERE D.DateValue = #DT) AS DateID, " +
"I.InfractionID, #ST AS STime, #DR AS Durration, #E AS Exception " +
"FROM " +
"InfractionTypes AS I " +
"INNER JOIN " +
"Groupings AS G " +
"ON I.GroupingID = G.GroupingID " +
"WHERE " +
"G.GroupingTitle = #NAME " +
"AND " +
"I.MinDur <= #DR1 AND I.MaxDur >= #DR2;";
OleDbCommand InfCmd = null;
Then the setup:
string conString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\New.Employee\Documents\UserMan2.accdb";
conn = new OleDbConnection(conString);
InfCmd = new OleDbCommand(sqlInfaction, conn);
InfCmd.Parameters.Add("#ID", OleDbType.Integer);
InfCmd.Parameters.Add("#DT", OleDbType.Date);
InfCmd.Parameters.Add("#ST", OleDbType.SmallInt);
InfCmd.Parameters.Add("#DR", OleDbType.SmallInt);
InfCmd.Parameters.Add("#E", OleDbType.Boolean);
InfCmd.Parameters.Add("#NAME", OleDbType.VarWChar);
InfCmd.Parameters.Add("#DR1", OleDbType.SmallInt);
InfCmd.Parameters.Add("#DR2", OleDbType.SmallInt);
And in the transfer function:
InfCmd.Parameters["#ID"].Value = inf.AgentID;
InfCmd.Parameters["#DT"].Value = inf.date;
InfCmd.Parameters["#ST"].Value = inf.startTime;
InfCmd.Parameters["#DR"].Value = inf.durration;
InfCmd.Parameters["#E"].Value = inf.exception;
InfCmd.Parameters["#NAME"].Value = inf.infract;
InfCmd.Parameters["#DR1"].Value = inf.durration;
InfCmd.Parameters["#DR2"].Value = inf.durration;
InfCmd.ExecuteNonQuery();
In testing, I stepped through the running code, using the values that are put into the above parameters to test the query in Access.
The Value of sqlInfaction via quickwatch:
sqlInfaction "INSERT INTO AquiredInfractions(AgentID, DateID, InfractionID, STime, Durration, Exception)
SELECT #ID AS AgentID, (SELECT D.DateID FROM DateCodes AS D WHERE D.DateValue = #DT) AS DateID,
I.InfractionID, #ST AS STime, #DR AS Durration, #E AS Exception FROM InfractionTypes AS I
INNER JOIN Groupings AS G ON I.GroupingID = G.GroupingID WHERE G.GroupingTitle = #NAME
AND I.MinDur <= #DR1 AND I.MaxDur >= #DR2;" string
The exception gives the following:
Message: Syntax error in INSERT INTO statement.
Error Code: -2147217900
If there is something specific from the exception thrown, let me know but that is the entire message.
After playing around, I found the answer to be missing [] around Exception. The correct query reads:
string sqlInfaction = "INSERT INTO AquiredInfractions" +
"(AgentID, DateID, InfractionID, STime, Durration, [Exception]) " +
"SELECT " +
"#ID AS AgentID, (SELECT D.DateID FROM DateCodes AS D WHERE D.DateValue = #DT) AS DateID, " +
"I.InfractionID, #ST AS STime, #DR AS Durration, #E AS [Exception] " +
"FROM " +
"InfractionTypes AS I " +
"INNER JOIN " +
"Groupings AS G " +
"ON I.GroupingID = G.GroupingID " +
"WHERE " +
"G.GroupingTitle = #NAME " +
"AND " +
"I.MinDur <= #DR1 AND I.MaxDur >= #DR2;";

How can you use parameterized statements with DB2 Text Search?

I've tried this:
select * from ourschema.mytable
where contains(mysearchablefield, #searchTerms) = 1;
Where #searchTerms was set to "search terms"
Unfortunately, it only produced an error:
ERROR [42610] [IBM][DB2/NT] SQL0418N A statement contains a use of a parameter marker that is not valid. SQLSTATE=42610
Is there a way to use parameterized queries for text search with DB2? If not, is there a document which describes the syntax in detail for manual (ugh) escaping of the search terms (quotes, etc)?
Instead of #field you need to use "?". Everything is basically the same.
Okay, here is a live code sample.
sqlStmt = "SELECT COMPLAINT_NUMBER, VIOLATION_NUMBER, COMMON_ADDRESS_KEY, " +
"DEPT_CODE, DEPT_CODE_DESC, DIVISION_CODE, DIVISION_CODE_DESC, " +
"EMPLOYEE_NAME, COMPLAINT_CODE, COMPLAINT_CODE_DESC, COMPLAINT_DATE, " +
"COMMON_ADDRESS_OWNER, RESOLUTION_CODE, 1 AS SORTORDER " +
"FROM QMFILES/NVMASTP " +
"WHERE VCLOSEDATE = 0 AND " +
"DEPT_CODE LIKE #DEPT_CODE1 AND " +
"DIVISION_CODE LIKE #DIVISION_CODE1 AND " +
"COMPLAINT_DATE BETWEEN #FROM_COMPLAINT_DATE1 AND #TO_COMPLAINT_DATE1 " +
statusQry +
"UNION " +
"SELECT COMPLAINT_NUMBER, VIOLATION_NUMBER, COMMON_ADDRESS_KEY, " +
"DEPT_CODE, DEPT_CODE_DESC, DIVISION_CODE, DIVISION_CODE_DESC, " +
"EMPLOYEE_NAME, COMPLAINT_CODE, COMPLAINT_CODE_DESC, COMPLAINT_DATE, " +
"COMMON_ADDRESS_OWNER, RESOLUTION_CODE, 2 AS SORTORDER " +
"FROM QMFILES/NVMASTP " +
"WHERE VCLOSEDATE <> 0 AND " +
"DEPT_CODE LIKE #DEPT_CODE2 AND " +
"DIVISION_CODE LIKE #DIVISION_CODE2 AND " +
"COMPLAINT_DATE BETWEEN #FROM_COMPLAINT_DATE2 AND #TO_COMPLAINT_DATE2 " +
statusQry +
"ORDER BY DEPT_CODE, DIVISION_CODE, COMPLAINT_CODE, SORTORDER";
iDB2Command cmd = new iDB2Command(sqlStmt, conn);
conn.Open();
cmd.DeriveParameters();
conn.Close();
cmd.Parameters["#DEPT_CODE1"].Value = dept;
cmd.Parameters["#DIVISION_CODE1"].Value = serviceArea;
cmd.Parameters["#DEPT_CODE2"].Value = dept;
cmd.Parameters["#DIVISION_CODE2"].Value = serviceArea;
cmd.Parameters["#FROM_COMPLAINT_DATE1"].Value = Convert.ToDecimal(fromDateString);
cmd.Parameters["#TO_COMPLAINT_DATE1"].Value = Convert.ToDecimal(toDateString);
cmd.Parameters["#FROM_COMPLAINT_DATE2"].Value = Convert.ToDecimal(fromDateString);
cmd.Parameters["#TO_COMPLAINT_DATE2"].Value = Convert.ToDecimal(toDateString);
I hope this helps you out more.

Categories

Resources