C# SQL loop in insert statement from string values
I have an insert statement which creates a new row into a table:
string SQLQuery = "INSERT INTO TABLEABC VALUES('" + Rtext + "', '" + TDate + "')";
But I want to create multiple lines for 20 repeats worth of data for 8 week (56 days) intervals.
For example:
Rtext TDate
John 23/11/2018
I would want it to create:
Rtext TDate
John 23/11/2018
John 18/01/2019
John 15/03/2019
John 10/05/2019
John 05/07/2019
John 30/08/2019
John 25/10/2019
John 20/12/2019
John 14/02/2020
John 10/04/2020
John 05/06/2020
John 31/07/2020
John 25/09/2020
John 20/11/2020
John 15/01/2021
John 12/03/2021
John 07/05/2021
John 02/07/2021
John 27/08/2021
John 22/10/2021
John 17/12/2021
c# sql
|
show 1 more comment
I have an insert statement which creates a new row into a table:
string SQLQuery = "INSERT INTO TABLEABC VALUES('" + Rtext + "', '" + TDate + "')";
But I want to create multiple lines for 20 repeats worth of data for 8 week (56 days) intervals.
For example:
Rtext TDate
John 23/11/2018
I would want it to create:
Rtext TDate
John 23/11/2018
John 18/01/2019
John 15/03/2019
John 10/05/2019
John 05/07/2019
John 30/08/2019
John 25/10/2019
John 20/12/2019
John 14/02/2020
John 10/04/2020
John 05/06/2020
John 31/07/2020
John 25/09/2020
John 20/11/2020
John 15/01/2021
John 12/03/2021
John 07/05/2021
John 02/07/2021
John 27/08/2021
John 22/10/2021
John 17/12/2021
c# sql
Recursive cte? Add 56 days for each iteration.
– jarlh
Nov 23 '18 at 8:50
1
What's wrong with just using DateTime.AddDays(56) in a loop from 1 to 20?
– Dylan Nicholson
Nov 23 '18 at 8:51
UseTDate.AddDays(56)and assign parameters for every execution. Don't forget to useParameters.Clear().
– Tetsuya Yamamoto
Nov 23 '18 at 8:51
@jarlh liek this?DECLARE @i int = 0 WHILE @i < 300 BEGIN SET @i = @i + 1 /* your code*/ END
– Matt
Nov 23 '18 at 8:54
Why are you not parametrising your query either? This is wide open to SQL injection. Personally, I would handle the additional rows in SQL Server as well; either by use of an rCTE (as @jarlh mentioned) or a (virtual) tally table.
– Larnu
Nov 23 '18 at 8:54
|
show 1 more comment
I have an insert statement which creates a new row into a table:
string SQLQuery = "INSERT INTO TABLEABC VALUES('" + Rtext + "', '" + TDate + "')";
But I want to create multiple lines for 20 repeats worth of data for 8 week (56 days) intervals.
For example:
Rtext TDate
John 23/11/2018
I would want it to create:
Rtext TDate
John 23/11/2018
John 18/01/2019
John 15/03/2019
John 10/05/2019
John 05/07/2019
John 30/08/2019
John 25/10/2019
John 20/12/2019
John 14/02/2020
John 10/04/2020
John 05/06/2020
John 31/07/2020
John 25/09/2020
John 20/11/2020
John 15/01/2021
John 12/03/2021
John 07/05/2021
John 02/07/2021
John 27/08/2021
John 22/10/2021
John 17/12/2021
c# sql
I have an insert statement which creates a new row into a table:
string SQLQuery = "INSERT INTO TABLEABC VALUES('" + Rtext + "', '" + TDate + "')";
But I want to create multiple lines for 20 repeats worth of data for 8 week (56 days) intervals.
For example:
Rtext TDate
John 23/11/2018
I would want it to create:
Rtext TDate
John 23/11/2018
John 18/01/2019
John 15/03/2019
John 10/05/2019
John 05/07/2019
John 30/08/2019
John 25/10/2019
John 20/12/2019
John 14/02/2020
John 10/04/2020
John 05/06/2020
John 31/07/2020
John 25/09/2020
John 20/11/2020
John 15/01/2021
John 12/03/2021
John 07/05/2021
John 02/07/2021
John 27/08/2021
John 22/10/2021
John 17/12/2021
c# sql
c# sql
asked Nov 23 '18 at 8:49
MattMatt
10.8k216698
10.8k216698
Recursive cte? Add 56 days for each iteration.
– jarlh
Nov 23 '18 at 8:50
1
What's wrong with just using DateTime.AddDays(56) in a loop from 1 to 20?
– Dylan Nicholson
Nov 23 '18 at 8:51
UseTDate.AddDays(56)and assign parameters for every execution. Don't forget to useParameters.Clear().
– Tetsuya Yamamoto
Nov 23 '18 at 8:51
@jarlh liek this?DECLARE @i int = 0 WHILE @i < 300 BEGIN SET @i = @i + 1 /* your code*/ END
– Matt
Nov 23 '18 at 8:54
Why are you not parametrising your query either? This is wide open to SQL injection. Personally, I would handle the additional rows in SQL Server as well; either by use of an rCTE (as @jarlh mentioned) or a (virtual) tally table.
– Larnu
Nov 23 '18 at 8:54
|
show 1 more comment
Recursive cte? Add 56 days for each iteration.
– jarlh
Nov 23 '18 at 8:50
1
What's wrong with just using DateTime.AddDays(56) in a loop from 1 to 20?
– Dylan Nicholson
Nov 23 '18 at 8:51
UseTDate.AddDays(56)and assign parameters for every execution. Don't forget to useParameters.Clear().
– Tetsuya Yamamoto
Nov 23 '18 at 8:51
@jarlh liek this?DECLARE @i int = 0 WHILE @i < 300 BEGIN SET @i = @i + 1 /* your code*/ END
– Matt
Nov 23 '18 at 8:54
Why are you not parametrising your query either? This is wide open to SQL injection. Personally, I would handle the additional rows in SQL Server as well; either by use of an rCTE (as @jarlh mentioned) or a (virtual) tally table.
– Larnu
Nov 23 '18 at 8:54
Recursive cte? Add 56 days for each iteration.
– jarlh
Nov 23 '18 at 8:50
Recursive cte? Add 56 days for each iteration.
– jarlh
Nov 23 '18 at 8:50
1
1
What's wrong with just using DateTime.AddDays(56) in a loop from 1 to 20?
– Dylan Nicholson
Nov 23 '18 at 8:51
What's wrong with just using DateTime.AddDays(56) in a loop from 1 to 20?
– Dylan Nicholson
Nov 23 '18 at 8:51
Use
TDate.AddDays(56) and assign parameters for every execution. Don't forget to use Parameters.Clear().– Tetsuya Yamamoto
Nov 23 '18 at 8:51
Use
TDate.AddDays(56) and assign parameters for every execution. Don't forget to use Parameters.Clear().– Tetsuya Yamamoto
Nov 23 '18 at 8:51
@jarlh liek this?
DECLARE @i int = 0 WHILE @i < 300 BEGIN SET @i = @i + 1 /* your code*/ END– Matt
Nov 23 '18 at 8:54
@jarlh liek this?
DECLARE @i int = 0 WHILE @i < 300 BEGIN SET @i = @i + 1 /* your code*/ END– Matt
Nov 23 '18 at 8:54
Why are you not parametrising your query either? This is wide open to SQL injection. Personally, I would handle the additional rows in SQL Server as well; either by use of an rCTE (as @jarlh mentioned) or a (virtual) tally table.
– Larnu
Nov 23 '18 at 8:54
Why are you not parametrising your query either? This is wide open to SQL injection. Personally, I would handle the additional rows in SQL Server as well; either by use of an rCTE (as @jarlh mentioned) or a (virtual) tally table.
– Larnu
Nov 23 '18 at 8:54
|
show 1 more comment
4 Answers
4
active
oldest
votes
You can try this.
DECLARE @i INT=0, @LastDate AS DATE=GETDATE()
WHILE (@i < 20)
BEGIN
INSERT INTO TABLEABC VALUES('" + Rtext + "', '" + @LastDate + "')
SET @LastDate = @LastDate.AddDays(56)
SET @i=@i+1;
END
3
This won't work as is, you have to consider the fact that it is inside c# code. Double-check your quotation marks ;) Also this won't prevent SQL-injection. Plus, does SQL really support.AddDays(56)?
– Rafalon
Nov 23 '18 at 8:59
Looping is not great inside SQL Server, and this doesn't address to injection issue at all.
– Larnu
Nov 23 '18 at 9:00
1
What doesAddDays(56)do inSET @LastDate = @LastDate.AddDays(56)? is this a new function in SQL Server?
– Sami
Nov 23 '18 at 10:20
@Sami Initial value of '@LastDate is today's date. '@LastDate.AddDays(56) means we are adding 56days to the '@LastDate.
– Thilina Nakkawita
Nov 23 '18 at 10:59
@ThilinaNakkawita, that's great, if the code you've got in your answer was in a language that supported that syntax. What you've got here is a weird hybrid of C# and SQL, which will run in neither
– Rob
Nov 23 '18 at 11:26
add a comment |
Firstly, you should be parametrising your query. My C# is very rusty (poor), so you'll have to excuse me if this is wrong, however, if I recall (and my Google-fu worked), then you'll want to do something more like:
string SQLQuery = "INSERT INTO TABLEABC VALUES(@RText, @TDate)";
SQLCommand Command = new SQLCommand(SQLQuery, YourConnection);
Command.Parameters.Add("@RText",SQLDbType.varchar);
Command.Parameters["@RText"].Value = RText; //Assumed variable name
Command.Parameters.Add("@TDate",SQLDbType.date); //Assumed datatype
Command.Parameters["@TDate"].Value = TDate; //Assumed variable name
This doesn't solve the repeating items though, so the first line could be replaced with:
string SQLQuery = "INSERT INTO TableABC SELECT @RText, DATEADD(WEEK, 8*I, @TDate) FROM (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18),(19)) V(I);";
Alternativey, you could use a rCTE instead of a virtual Tally table. In SQL this would look like:
WITH rCTE AS(
SELECT @RText AS RText,
@TDate AS TDate
0 AS I
UNION ALL
SELECT RText,
DATEADD(WEEK, (I+1)*8,TDate) AS TDate,
I+1
FROM rCTE
WHERE I+1 <20
)
INSERT INTO TABLEABC
SELECT RText,
TDate
FROM rCTE;
If you're going to have a large amount of values, a scalable Tally Table is the way to go:
WITH N AS(
SELECT N
FROM (VALUES(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL))N(N)),
Tally AS(
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS I
FROM N N1 -10
CROSS JOIN N N2 --100
CROSS JOIN N N2 --1000
CROSS JOIN N N2 --10000 --you get the idea
)
INSERT INTO TABLEABC
SELECT TOP 500
@RText,
DATEADD(WEEK, (I-1)*8, @TDate)
FROM Tally;
A rCTE, in the sense above, is an RBAR method, so it's speed will get slower the more rows you need. A tally table would be far faster, isn't RBAR, and doesn't need the MAXRECURSION option.
1
While this may be correct, would you do the same if we wanted to insert 500 values instead of 20?
– Rafalon
Nov 23 '18 at 9:09
@Rafalon The OP specifically states 20 in their post. For 500 I would use a scalable tally table,.
– Larnu
Nov 23 '18 at 9:11
Don't take offense, my question was only here to make you think further and improve your (already good) answer :)
– Rafalon
Nov 23 '18 at 9:12
Added a Scalable Virtual Tally Table for you @Rafalon
– Larnu
Nov 23 '18 at 9:16
Can you put this into a full answer? I dont know which parts need using
– Matt
Nov 23 '18 at 10:53
|
show 1 more comment
Looking into the syntax you are using, seems like you are trying it in C#. So use below code.
you need to use stringbuilder and append insert string or concate in SQLQuery. Use below code.
Datetime myDate = Convert.toDatetime("23/11/2018")
string SQLQuery = "INSERT INTO TABLEABC VALUES('" + Rtext + "', '" + TDate + "');";
SQLQuery += "INSERT INTO TABLEABC VALUES('" + John + "', '" + myDate + "');";
for (int i = 1; i <=19; i++)
{
SQLQuery +=" INSERT INTO TABLEABC VALUES('" + John + "', myDate.AddDays(56) );";
}
Injection is still a problem here.
– Larnu
Nov 23 '18 at 9:00
what are you using for injecting in database? is it linq or entity-relational mapping?
– Agustus Codes
Nov 23 '18 at 9:03
How aboutRtext = "1','19000101'); DROP TABLE TABLEABC;--"?
– Larnu
Nov 23 '18 at 9:04
Is Rtext name of column ? what is '19000101'?
– Agustus Codes
Nov 23 '18 at 9:05
@AgustusCodes the very fact that you do not know whatRtextis when you have it in your answer kind of invalidates your answer
– Rafalon
Nov 23 '18 at 9:07
|
show 6 more comments
I preferred using SQL parameters and a for loop to execute the insert command for every iteration, but you should clear parameters before next execution begins. Here is an example by assumed that you're converting the date string to DateTime first:
string SQLQuery = "INSERT INTO TABLEABC (RText, TDate) VALUES (@RText, @TDate)";
// edit: TDate is a string, convert it to DateTime first
DateTime date;
if (DateTime.TryParseExact(TDate, "dd/MM/yyyy", CultureInfo.InvariantCulture,
DateTimeStyles.None, out date)
{
using (var con = new SqlConnection(connectionString))
{
con.Open();
using (var cmd = new SqlCommand(SQLQuery, con))
{
for (var i = 0; i < 20; i++)
{
cmd.Parameters.AddWithValue("@RText", Rtext);
// add for next iterations
if (i > 0)
{
date = date.AddDays(56);
}
cmd.Parameters.AddWithValue("@TDate", date);
cmd.ExecuteNonQuery();
cmd.Parameters.Clear(); // clear existing parameters for next iteration
}
}
}
}
else
{
// handle invalid dates
}
Note: If the data type is exactly known, use Parameters.Add() instead of Parameters.AddWithValue().
Getting an error:'string' does not contain a definition for 'AddDays' and no accessible extension method 'AddDays' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
– Matt
Nov 23 '18 at 9:24
Because you have date stored as string inTDatemaybe?
– Rafalon
Nov 23 '18 at 9:25
Yesstring TDate= TDateTB.Text;
– Matt
Nov 23 '18 at 9:26
AddDaysis used forDateTime. If you want to convert a string to DateTime, usevar date = DateTime.ParseExact(TDate, "dd/MM/yyyy", CultureInfo.InvariantCulture)and usedateinsideAdd()/AddWithValue().
– Tetsuya Yamamoto
Nov 23 '18 at 9:26
hmm gettingThe name 'CultureInfo' does not exist in the current contextnow
– Matt
Nov 23 '18 at 9:34
|
show 10 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443301%2fc-sharp-sql-loop-in-insert-statement-from-string-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can try this.
DECLARE @i INT=0, @LastDate AS DATE=GETDATE()
WHILE (@i < 20)
BEGIN
INSERT INTO TABLEABC VALUES('" + Rtext + "', '" + @LastDate + "')
SET @LastDate = @LastDate.AddDays(56)
SET @i=@i+1;
END
3
This won't work as is, you have to consider the fact that it is inside c# code. Double-check your quotation marks ;) Also this won't prevent SQL-injection. Plus, does SQL really support.AddDays(56)?
– Rafalon
Nov 23 '18 at 8:59
Looping is not great inside SQL Server, and this doesn't address to injection issue at all.
– Larnu
Nov 23 '18 at 9:00
1
What doesAddDays(56)do inSET @LastDate = @LastDate.AddDays(56)? is this a new function in SQL Server?
– Sami
Nov 23 '18 at 10:20
@Sami Initial value of '@LastDate is today's date. '@LastDate.AddDays(56) means we are adding 56days to the '@LastDate.
– Thilina Nakkawita
Nov 23 '18 at 10:59
@ThilinaNakkawita, that's great, if the code you've got in your answer was in a language that supported that syntax. What you've got here is a weird hybrid of C# and SQL, which will run in neither
– Rob
Nov 23 '18 at 11:26
add a comment |
You can try this.
DECLARE @i INT=0, @LastDate AS DATE=GETDATE()
WHILE (@i < 20)
BEGIN
INSERT INTO TABLEABC VALUES('" + Rtext + "', '" + @LastDate + "')
SET @LastDate = @LastDate.AddDays(56)
SET @i=@i+1;
END
3
This won't work as is, you have to consider the fact that it is inside c# code. Double-check your quotation marks ;) Also this won't prevent SQL-injection. Plus, does SQL really support.AddDays(56)?
– Rafalon
Nov 23 '18 at 8:59
Looping is not great inside SQL Server, and this doesn't address to injection issue at all.
– Larnu
Nov 23 '18 at 9:00
1
What doesAddDays(56)do inSET @LastDate = @LastDate.AddDays(56)? is this a new function in SQL Server?
– Sami
Nov 23 '18 at 10:20
@Sami Initial value of '@LastDate is today's date. '@LastDate.AddDays(56) means we are adding 56days to the '@LastDate.
– Thilina Nakkawita
Nov 23 '18 at 10:59
@ThilinaNakkawita, that's great, if the code you've got in your answer was in a language that supported that syntax. What you've got here is a weird hybrid of C# and SQL, which will run in neither
– Rob
Nov 23 '18 at 11:26
add a comment |
You can try this.
DECLARE @i INT=0, @LastDate AS DATE=GETDATE()
WHILE (@i < 20)
BEGIN
INSERT INTO TABLEABC VALUES('" + Rtext + "', '" + @LastDate + "')
SET @LastDate = @LastDate.AddDays(56)
SET @i=@i+1;
END
You can try this.
DECLARE @i INT=0, @LastDate AS DATE=GETDATE()
WHILE (@i < 20)
BEGIN
INSERT INTO TABLEABC VALUES('" + Rtext + "', '" + @LastDate + "')
SET @LastDate = @LastDate.AddDays(56)
SET @i=@i+1;
END
answered Nov 23 '18 at 8:57
Thilina NakkawitaThilina Nakkawita
8671027
8671027
3
This won't work as is, you have to consider the fact that it is inside c# code. Double-check your quotation marks ;) Also this won't prevent SQL-injection. Plus, does SQL really support.AddDays(56)?
– Rafalon
Nov 23 '18 at 8:59
Looping is not great inside SQL Server, and this doesn't address to injection issue at all.
– Larnu
Nov 23 '18 at 9:00
1
What doesAddDays(56)do inSET @LastDate = @LastDate.AddDays(56)? is this a new function in SQL Server?
– Sami
Nov 23 '18 at 10:20
@Sami Initial value of '@LastDate is today's date. '@LastDate.AddDays(56) means we are adding 56days to the '@LastDate.
– Thilina Nakkawita
Nov 23 '18 at 10:59
@ThilinaNakkawita, that's great, if the code you've got in your answer was in a language that supported that syntax. What you've got here is a weird hybrid of C# and SQL, which will run in neither
– Rob
Nov 23 '18 at 11:26
add a comment |
3
This won't work as is, you have to consider the fact that it is inside c# code. Double-check your quotation marks ;) Also this won't prevent SQL-injection. Plus, does SQL really support.AddDays(56)?
– Rafalon
Nov 23 '18 at 8:59
Looping is not great inside SQL Server, and this doesn't address to injection issue at all.
– Larnu
Nov 23 '18 at 9:00
1
What doesAddDays(56)do inSET @LastDate = @LastDate.AddDays(56)? is this a new function in SQL Server?
– Sami
Nov 23 '18 at 10:20
@Sami Initial value of '@LastDate is today's date. '@LastDate.AddDays(56) means we are adding 56days to the '@LastDate.
– Thilina Nakkawita
Nov 23 '18 at 10:59
@ThilinaNakkawita, that's great, if the code you've got in your answer was in a language that supported that syntax. What you've got here is a weird hybrid of C# and SQL, which will run in neither
– Rob
Nov 23 '18 at 11:26
3
3
This won't work as is, you have to consider the fact that it is inside c# code. Double-check your quotation marks ;) Also this won't prevent SQL-injection. Plus, does SQL really support
.AddDays(56)?– Rafalon
Nov 23 '18 at 8:59
This won't work as is, you have to consider the fact that it is inside c# code. Double-check your quotation marks ;) Also this won't prevent SQL-injection. Plus, does SQL really support
.AddDays(56)?– Rafalon
Nov 23 '18 at 8:59
Looping is not great inside SQL Server, and this doesn't address to injection issue at all.
– Larnu
Nov 23 '18 at 9:00
Looping is not great inside SQL Server, and this doesn't address to injection issue at all.
– Larnu
Nov 23 '18 at 9:00
1
1
What does
AddDays(56) do in SET @LastDate = @LastDate.AddDays(56)? is this a new function in SQL Server?– Sami
Nov 23 '18 at 10:20
What does
AddDays(56) do in SET @LastDate = @LastDate.AddDays(56)? is this a new function in SQL Server?– Sami
Nov 23 '18 at 10:20
@Sami Initial value of '@LastDate is today's date. '@LastDate.AddDays(56) means we are adding 56days to the '@LastDate.
– Thilina Nakkawita
Nov 23 '18 at 10:59
@Sami Initial value of '@LastDate is today's date. '@LastDate.AddDays(56) means we are adding 56days to the '@LastDate.
– Thilina Nakkawita
Nov 23 '18 at 10:59
@ThilinaNakkawita, that's great, if the code you've got in your answer was in a language that supported that syntax. What you've got here is a weird hybrid of C# and SQL, which will run in neither
– Rob
Nov 23 '18 at 11:26
@ThilinaNakkawita, that's great, if the code you've got in your answer was in a language that supported that syntax. What you've got here is a weird hybrid of C# and SQL, which will run in neither
– Rob
Nov 23 '18 at 11:26
add a comment |
Firstly, you should be parametrising your query. My C# is very rusty (poor), so you'll have to excuse me if this is wrong, however, if I recall (and my Google-fu worked), then you'll want to do something more like:
string SQLQuery = "INSERT INTO TABLEABC VALUES(@RText, @TDate)";
SQLCommand Command = new SQLCommand(SQLQuery, YourConnection);
Command.Parameters.Add("@RText",SQLDbType.varchar);
Command.Parameters["@RText"].Value = RText; //Assumed variable name
Command.Parameters.Add("@TDate",SQLDbType.date); //Assumed datatype
Command.Parameters["@TDate"].Value = TDate; //Assumed variable name
This doesn't solve the repeating items though, so the first line could be replaced with:
string SQLQuery = "INSERT INTO TableABC SELECT @RText, DATEADD(WEEK, 8*I, @TDate) FROM (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18),(19)) V(I);";
Alternativey, you could use a rCTE instead of a virtual Tally table. In SQL this would look like:
WITH rCTE AS(
SELECT @RText AS RText,
@TDate AS TDate
0 AS I
UNION ALL
SELECT RText,
DATEADD(WEEK, (I+1)*8,TDate) AS TDate,
I+1
FROM rCTE
WHERE I+1 <20
)
INSERT INTO TABLEABC
SELECT RText,
TDate
FROM rCTE;
If you're going to have a large amount of values, a scalable Tally Table is the way to go:
WITH N AS(
SELECT N
FROM (VALUES(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL))N(N)),
Tally AS(
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS I
FROM N N1 -10
CROSS JOIN N N2 --100
CROSS JOIN N N2 --1000
CROSS JOIN N N2 --10000 --you get the idea
)
INSERT INTO TABLEABC
SELECT TOP 500
@RText,
DATEADD(WEEK, (I-1)*8, @TDate)
FROM Tally;
A rCTE, in the sense above, is an RBAR method, so it's speed will get slower the more rows you need. A tally table would be far faster, isn't RBAR, and doesn't need the MAXRECURSION option.
1
While this may be correct, would you do the same if we wanted to insert 500 values instead of 20?
– Rafalon
Nov 23 '18 at 9:09
@Rafalon The OP specifically states 20 in their post. For 500 I would use a scalable tally table,.
– Larnu
Nov 23 '18 at 9:11
Don't take offense, my question was only here to make you think further and improve your (already good) answer :)
– Rafalon
Nov 23 '18 at 9:12
Added a Scalable Virtual Tally Table for you @Rafalon
– Larnu
Nov 23 '18 at 9:16
Can you put this into a full answer? I dont know which parts need using
– Matt
Nov 23 '18 at 10:53
|
show 1 more comment
Firstly, you should be parametrising your query. My C# is very rusty (poor), so you'll have to excuse me if this is wrong, however, if I recall (and my Google-fu worked), then you'll want to do something more like:
string SQLQuery = "INSERT INTO TABLEABC VALUES(@RText, @TDate)";
SQLCommand Command = new SQLCommand(SQLQuery, YourConnection);
Command.Parameters.Add("@RText",SQLDbType.varchar);
Command.Parameters["@RText"].Value = RText; //Assumed variable name
Command.Parameters.Add("@TDate",SQLDbType.date); //Assumed datatype
Command.Parameters["@TDate"].Value = TDate; //Assumed variable name
This doesn't solve the repeating items though, so the first line could be replaced with:
string SQLQuery = "INSERT INTO TableABC SELECT @RText, DATEADD(WEEK, 8*I, @TDate) FROM (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18),(19)) V(I);";
Alternativey, you could use a rCTE instead of a virtual Tally table. In SQL this would look like:
WITH rCTE AS(
SELECT @RText AS RText,
@TDate AS TDate
0 AS I
UNION ALL
SELECT RText,
DATEADD(WEEK, (I+1)*8,TDate) AS TDate,
I+1
FROM rCTE
WHERE I+1 <20
)
INSERT INTO TABLEABC
SELECT RText,
TDate
FROM rCTE;
If you're going to have a large amount of values, a scalable Tally Table is the way to go:
WITH N AS(
SELECT N
FROM (VALUES(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL))N(N)),
Tally AS(
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS I
FROM N N1 -10
CROSS JOIN N N2 --100
CROSS JOIN N N2 --1000
CROSS JOIN N N2 --10000 --you get the idea
)
INSERT INTO TABLEABC
SELECT TOP 500
@RText,
DATEADD(WEEK, (I-1)*8, @TDate)
FROM Tally;
A rCTE, in the sense above, is an RBAR method, so it's speed will get slower the more rows you need. A tally table would be far faster, isn't RBAR, and doesn't need the MAXRECURSION option.
1
While this may be correct, would you do the same if we wanted to insert 500 values instead of 20?
– Rafalon
Nov 23 '18 at 9:09
@Rafalon The OP specifically states 20 in their post. For 500 I would use a scalable tally table,.
– Larnu
Nov 23 '18 at 9:11
Don't take offense, my question was only here to make you think further and improve your (already good) answer :)
– Rafalon
Nov 23 '18 at 9:12
Added a Scalable Virtual Tally Table for you @Rafalon
– Larnu
Nov 23 '18 at 9:16
Can you put this into a full answer? I dont know which parts need using
– Matt
Nov 23 '18 at 10:53
|
show 1 more comment
Firstly, you should be parametrising your query. My C# is very rusty (poor), so you'll have to excuse me if this is wrong, however, if I recall (and my Google-fu worked), then you'll want to do something more like:
string SQLQuery = "INSERT INTO TABLEABC VALUES(@RText, @TDate)";
SQLCommand Command = new SQLCommand(SQLQuery, YourConnection);
Command.Parameters.Add("@RText",SQLDbType.varchar);
Command.Parameters["@RText"].Value = RText; //Assumed variable name
Command.Parameters.Add("@TDate",SQLDbType.date); //Assumed datatype
Command.Parameters["@TDate"].Value = TDate; //Assumed variable name
This doesn't solve the repeating items though, so the first line could be replaced with:
string SQLQuery = "INSERT INTO TableABC SELECT @RText, DATEADD(WEEK, 8*I, @TDate) FROM (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18),(19)) V(I);";
Alternativey, you could use a rCTE instead of a virtual Tally table. In SQL this would look like:
WITH rCTE AS(
SELECT @RText AS RText,
@TDate AS TDate
0 AS I
UNION ALL
SELECT RText,
DATEADD(WEEK, (I+1)*8,TDate) AS TDate,
I+1
FROM rCTE
WHERE I+1 <20
)
INSERT INTO TABLEABC
SELECT RText,
TDate
FROM rCTE;
If you're going to have a large amount of values, a scalable Tally Table is the way to go:
WITH N AS(
SELECT N
FROM (VALUES(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL))N(N)),
Tally AS(
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS I
FROM N N1 -10
CROSS JOIN N N2 --100
CROSS JOIN N N2 --1000
CROSS JOIN N N2 --10000 --you get the idea
)
INSERT INTO TABLEABC
SELECT TOP 500
@RText,
DATEADD(WEEK, (I-1)*8, @TDate)
FROM Tally;
A rCTE, in the sense above, is an RBAR method, so it's speed will get slower the more rows you need. A tally table would be far faster, isn't RBAR, and doesn't need the MAXRECURSION option.
Firstly, you should be parametrising your query. My C# is very rusty (poor), so you'll have to excuse me if this is wrong, however, if I recall (and my Google-fu worked), then you'll want to do something more like:
string SQLQuery = "INSERT INTO TABLEABC VALUES(@RText, @TDate)";
SQLCommand Command = new SQLCommand(SQLQuery, YourConnection);
Command.Parameters.Add("@RText",SQLDbType.varchar);
Command.Parameters["@RText"].Value = RText; //Assumed variable name
Command.Parameters.Add("@TDate",SQLDbType.date); //Assumed datatype
Command.Parameters["@TDate"].Value = TDate; //Assumed variable name
This doesn't solve the repeating items though, so the first line could be replaced with:
string SQLQuery = "INSERT INTO TableABC SELECT @RText, DATEADD(WEEK, 8*I, @TDate) FROM (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18),(19)) V(I);";
Alternativey, you could use a rCTE instead of a virtual Tally table. In SQL this would look like:
WITH rCTE AS(
SELECT @RText AS RText,
@TDate AS TDate
0 AS I
UNION ALL
SELECT RText,
DATEADD(WEEK, (I+1)*8,TDate) AS TDate,
I+1
FROM rCTE
WHERE I+1 <20
)
INSERT INTO TABLEABC
SELECT RText,
TDate
FROM rCTE;
If you're going to have a large amount of values, a scalable Tally Table is the way to go:
WITH N AS(
SELECT N
FROM (VALUES(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL))N(N)),
Tally AS(
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS I
FROM N N1 -10
CROSS JOIN N N2 --100
CROSS JOIN N N2 --1000
CROSS JOIN N N2 --10000 --you get the idea
)
INSERT INTO TABLEABC
SELECT TOP 500
@RText,
DATEADD(WEEK, (I-1)*8, @TDate)
FROM Tally;
A rCTE, in the sense above, is an RBAR method, so it's speed will get slower the more rows you need. A tally table would be far faster, isn't RBAR, and doesn't need the MAXRECURSION option.
edited Nov 23 '18 at 9:15
answered Nov 23 '18 at 9:07
LarnuLarnu
16.1k41630
16.1k41630
1
While this may be correct, would you do the same if we wanted to insert 500 values instead of 20?
– Rafalon
Nov 23 '18 at 9:09
@Rafalon The OP specifically states 20 in their post. For 500 I would use a scalable tally table,.
– Larnu
Nov 23 '18 at 9:11
Don't take offense, my question was only here to make you think further and improve your (already good) answer :)
– Rafalon
Nov 23 '18 at 9:12
Added a Scalable Virtual Tally Table for you @Rafalon
– Larnu
Nov 23 '18 at 9:16
Can you put this into a full answer? I dont know which parts need using
– Matt
Nov 23 '18 at 10:53
|
show 1 more comment
1
While this may be correct, would you do the same if we wanted to insert 500 values instead of 20?
– Rafalon
Nov 23 '18 at 9:09
@Rafalon The OP specifically states 20 in their post. For 500 I would use a scalable tally table,.
– Larnu
Nov 23 '18 at 9:11
Don't take offense, my question was only here to make you think further and improve your (already good) answer :)
– Rafalon
Nov 23 '18 at 9:12
Added a Scalable Virtual Tally Table for you @Rafalon
– Larnu
Nov 23 '18 at 9:16
Can you put this into a full answer? I dont know which parts need using
– Matt
Nov 23 '18 at 10:53
1
1
While this may be correct, would you do the same if we wanted to insert 500 values instead of 20?
– Rafalon
Nov 23 '18 at 9:09
While this may be correct, would you do the same if we wanted to insert 500 values instead of 20?
– Rafalon
Nov 23 '18 at 9:09
@Rafalon The OP specifically states 20 in their post. For 500 I would use a scalable tally table,.
– Larnu
Nov 23 '18 at 9:11
@Rafalon The OP specifically states 20 in their post. For 500 I would use a scalable tally table,.
– Larnu
Nov 23 '18 at 9:11
Don't take offense, my question was only here to make you think further and improve your (already good) answer :)
– Rafalon
Nov 23 '18 at 9:12
Don't take offense, my question was only here to make you think further and improve your (already good) answer :)
– Rafalon
Nov 23 '18 at 9:12
Added a Scalable Virtual Tally Table for you @Rafalon
– Larnu
Nov 23 '18 at 9:16
Added a Scalable Virtual Tally Table for you @Rafalon
– Larnu
Nov 23 '18 at 9:16
Can you put this into a full answer? I dont know which parts need using
– Matt
Nov 23 '18 at 10:53
Can you put this into a full answer? I dont know which parts need using
– Matt
Nov 23 '18 at 10:53
|
show 1 more comment
Looking into the syntax you are using, seems like you are trying it in C#. So use below code.
you need to use stringbuilder and append insert string or concate in SQLQuery. Use below code.
Datetime myDate = Convert.toDatetime("23/11/2018")
string SQLQuery = "INSERT INTO TABLEABC VALUES('" + Rtext + "', '" + TDate + "');";
SQLQuery += "INSERT INTO TABLEABC VALUES('" + John + "', '" + myDate + "');";
for (int i = 1; i <=19; i++)
{
SQLQuery +=" INSERT INTO TABLEABC VALUES('" + John + "', myDate.AddDays(56) );";
}
Injection is still a problem here.
– Larnu
Nov 23 '18 at 9:00
what are you using for injecting in database? is it linq or entity-relational mapping?
– Agustus Codes
Nov 23 '18 at 9:03
How aboutRtext = "1','19000101'); DROP TABLE TABLEABC;--"?
– Larnu
Nov 23 '18 at 9:04
Is Rtext name of column ? what is '19000101'?
– Agustus Codes
Nov 23 '18 at 9:05
@AgustusCodes the very fact that you do not know whatRtextis when you have it in your answer kind of invalidates your answer
– Rafalon
Nov 23 '18 at 9:07
|
show 6 more comments
Looking into the syntax you are using, seems like you are trying it in C#. So use below code.
you need to use stringbuilder and append insert string or concate in SQLQuery. Use below code.
Datetime myDate = Convert.toDatetime("23/11/2018")
string SQLQuery = "INSERT INTO TABLEABC VALUES('" + Rtext + "', '" + TDate + "');";
SQLQuery += "INSERT INTO TABLEABC VALUES('" + John + "', '" + myDate + "');";
for (int i = 1; i <=19; i++)
{
SQLQuery +=" INSERT INTO TABLEABC VALUES('" + John + "', myDate.AddDays(56) );";
}
Injection is still a problem here.
– Larnu
Nov 23 '18 at 9:00
what are you using for injecting in database? is it linq or entity-relational mapping?
– Agustus Codes
Nov 23 '18 at 9:03
How aboutRtext = "1','19000101'); DROP TABLE TABLEABC;--"?
– Larnu
Nov 23 '18 at 9:04
Is Rtext name of column ? what is '19000101'?
– Agustus Codes
Nov 23 '18 at 9:05
@AgustusCodes the very fact that you do not know whatRtextis when you have it in your answer kind of invalidates your answer
– Rafalon
Nov 23 '18 at 9:07
|
show 6 more comments
Looking into the syntax you are using, seems like you are trying it in C#. So use below code.
you need to use stringbuilder and append insert string or concate in SQLQuery. Use below code.
Datetime myDate = Convert.toDatetime("23/11/2018")
string SQLQuery = "INSERT INTO TABLEABC VALUES('" + Rtext + "', '" + TDate + "');";
SQLQuery += "INSERT INTO TABLEABC VALUES('" + John + "', '" + myDate + "');";
for (int i = 1; i <=19; i++)
{
SQLQuery +=" INSERT INTO TABLEABC VALUES('" + John + "', myDate.AddDays(56) );";
}
Looking into the syntax you are using, seems like you are trying it in C#. So use below code.
you need to use stringbuilder and append insert string or concate in SQLQuery. Use below code.
Datetime myDate = Convert.toDatetime("23/11/2018")
string SQLQuery = "INSERT INTO TABLEABC VALUES('" + Rtext + "', '" + TDate + "');";
SQLQuery += "INSERT INTO TABLEABC VALUES('" + John + "', '" + myDate + "');";
for (int i = 1; i <=19; i++)
{
SQLQuery +=" INSERT INTO TABLEABC VALUES('" + John + "', myDate.AddDays(56) );";
}
edited Nov 23 '18 at 9:11
answered Nov 23 '18 at 8:58
Agustus CodesAgustus Codes
3019
3019
Injection is still a problem here.
– Larnu
Nov 23 '18 at 9:00
what are you using for injecting in database? is it linq or entity-relational mapping?
– Agustus Codes
Nov 23 '18 at 9:03
How aboutRtext = "1','19000101'); DROP TABLE TABLEABC;--"?
– Larnu
Nov 23 '18 at 9:04
Is Rtext name of column ? what is '19000101'?
– Agustus Codes
Nov 23 '18 at 9:05
@AgustusCodes the very fact that you do not know whatRtextis when you have it in your answer kind of invalidates your answer
– Rafalon
Nov 23 '18 at 9:07
|
show 6 more comments
Injection is still a problem here.
– Larnu
Nov 23 '18 at 9:00
what are you using for injecting in database? is it linq or entity-relational mapping?
– Agustus Codes
Nov 23 '18 at 9:03
How aboutRtext = "1','19000101'); DROP TABLE TABLEABC;--"?
– Larnu
Nov 23 '18 at 9:04
Is Rtext name of column ? what is '19000101'?
– Agustus Codes
Nov 23 '18 at 9:05
@AgustusCodes the very fact that you do not know whatRtextis when you have it in your answer kind of invalidates your answer
– Rafalon
Nov 23 '18 at 9:07
Injection is still a problem here.
– Larnu
Nov 23 '18 at 9:00
Injection is still a problem here.
– Larnu
Nov 23 '18 at 9:00
what are you using for injecting in database? is it linq or entity-relational mapping?
– Agustus Codes
Nov 23 '18 at 9:03
what are you using for injecting in database? is it linq or entity-relational mapping?
– Agustus Codes
Nov 23 '18 at 9:03
How about
Rtext = "1','19000101'); DROP TABLE TABLEABC;--"?– Larnu
Nov 23 '18 at 9:04
How about
Rtext = "1','19000101'); DROP TABLE TABLEABC;--"?– Larnu
Nov 23 '18 at 9:04
Is Rtext name of column ? what is '19000101'?
– Agustus Codes
Nov 23 '18 at 9:05
Is Rtext name of column ? what is '19000101'?
– Agustus Codes
Nov 23 '18 at 9:05
@AgustusCodes the very fact that you do not know what
Rtext is when you have it in your answer kind of invalidates your answer– Rafalon
Nov 23 '18 at 9:07
@AgustusCodes the very fact that you do not know what
Rtext is when you have it in your answer kind of invalidates your answer– Rafalon
Nov 23 '18 at 9:07
|
show 6 more comments
I preferred using SQL parameters and a for loop to execute the insert command for every iteration, but you should clear parameters before next execution begins. Here is an example by assumed that you're converting the date string to DateTime first:
string SQLQuery = "INSERT INTO TABLEABC (RText, TDate) VALUES (@RText, @TDate)";
// edit: TDate is a string, convert it to DateTime first
DateTime date;
if (DateTime.TryParseExact(TDate, "dd/MM/yyyy", CultureInfo.InvariantCulture,
DateTimeStyles.None, out date)
{
using (var con = new SqlConnection(connectionString))
{
con.Open();
using (var cmd = new SqlCommand(SQLQuery, con))
{
for (var i = 0; i < 20; i++)
{
cmd.Parameters.AddWithValue("@RText", Rtext);
// add for next iterations
if (i > 0)
{
date = date.AddDays(56);
}
cmd.Parameters.AddWithValue("@TDate", date);
cmd.ExecuteNonQuery();
cmd.Parameters.Clear(); // clear existing parameters for next iteration
}
}
}
}
else
{
// handle invalid dates
}
Note: If the data type is exactly known, use Parameters.Add() instead of Parameters.AddWithValue().
Getting an error:'string' does not contain a definition for 'AddDays' and no accessible extension method 'AddDays' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
– Matt
Nov 23 '18 at 9:24
Because you have date stored as string inTDatemaybe?
– Rafalon
Nov 23 '18 at 9:25
Yesstring TDate= TDateTB.Text;
– Matt
Nov 23 '18 at 9:26
AddDaysis used forDateTime. If you want to convert a string to DateTime, usevar date = DateTime.ParseExact(TDate, "dd/MM/yyyy", CultureInfo.InvariantCulture)and usedateinsideAdd()/AddWithValue().
– Tetsuya Yamamoto
Nov 23 '18 at 9:26
hmm gettingThe name 'CultureInfo' does not exist in the current contextnow
– Matt
Nov 23 '18 at 9:34
|
show 10 more comments
I preferred using SQL parameters and a for loop to execute the insert command for every iteration, but you should clear parameters before next execution begins. Here is an example by assumed that you're converting the date string to DateTime first:
string SQLQuery = "INSERT INTO TABLEABC (RText, TDate) VALUES (@RText, @TDate)";
// edit: TDate is a string, convert it to DateTime first
DateTime date;
if (DateTime.TryParseExact(TDate, "dd/MM/yyyy", CultureInfo.InvariantCulture,
DateTimeStyles.None, out date)
{
using (var con = new SqlConnection(connectionString))
{
con.Open();
using (var cmd = new SqlCommand(SQLQuery, con))
{
for (var i = 0; i < 20; i++)
{
cmd.Parameters.AddWithValue("@RText", Rtext);
// add for next iterations
if (i > 0)
{
date = date.AddDays(56);
}
cmd.Parameters.AddWithValue("@TDate", date);
cmd.ExecuteNonQuery();
cmd.Parameters.Clear(); // clear existing parameters for next iteration
}
}
}
}
else
{
// handle invalid dates
}
Note: If the data type is exactly known, use Parameters.Add() instead of Parameters.AddWithValue().
Getting an error:'string' does not contain a definition for 'AddDays' and no accessible extension method 'AddDays' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
– Matt
Nov 23 '18 at 9:24
Because you have date stored as string inTDatemaybe?
– Rafalon
Nov 23 '18 at 9:25
Yesstring TDate= TDateTB.Text;
– Matt
Nov 23 '18 at 9:26
AddDaysis used forDateTime. If you want to convert a string to DateTime, usevar date = DateTime.ParseExact(TDate, "dd/MM/yyyy", CultureInfo.InvariantCulture)and usedateinsideAdd()/AddWithValue().
– Tetsuya Yamamoto
Nov 23 '18 at 9:26
hmm gettingThe name 'CultureInfo' does not exist in the current contextnow
– Matt
Nov 23 '18 at 9:34
|
show 10 more comments
I preferred using SQL parameters and a for loop to execute the insert command for every iteration, but you should clear parameters before next execution begins. Here is an example by assumed that you're converting the date string to DateTime first:
string SQLQuery = "INSERT INTO TABLEABC (RText, TDate) VALUES (@RText, @TDate)";
// edit: TDate is a string, convert it to DateTime first
DateTime date;
if (DateTime.TryParseExact(TDate, "dd/MM/yyyy", CultureInfo.InvariantCulture,
DateTimeStyles.None, out date)
{
using (var con = new SqlConnection(connectionString))
{
con.Open();
using (var cmd = new SqlCommand(SQLQuery, con))
{
for (var i = 0; i < 20; i++)
{
cmd.Parameters.AddWithValue("@RText", Rtext);
// add for next iterations
if (i > 0)
{
date = date.AddDays(56);
}
cmd.Parameters.AddWithValue("@TDate", date);
cmd.ExecuteNonQuery();
cmd.Parameters.Clear(); // clear existing parameters for next iteration
}
}
}
}
else
{
// handle invalid dates
}
Note: If the data type is exactly known, use Parameters.Add() instead of Parameters.AddWithValue().
I preferred using SQL parameters and a for loop to execute the insert command for every iteration, but you should clear parameters before next execution begins. Here is an example by assumed that you're converting the date string to DateTime first:
string SQLQuery = "INSERT INTO TABLEABC (RText, TDate) VALUES (@RText, @TDate)";
// edit: TDate is a string, convert it to DateTime first
DateTime date;
if (DateTime.TryParseExact(TDate, "dd/MM/yyyy", CultureInfo.InvariantCulture,
DateTimeStyles.None, out date)
{
using (var con = new SqlConnection(connectionString))
{
con.Open();
using (var cmd = new SqlCommand(SQLQuery, con))
{
for (var i = 0; i < 20; i++)
{
cmd.Parameters.AddWithValue("@RText", Rtext);
// add for next iterations
if (i > 0)
{
date = date.AddDays(56);
}
cmd.Parameters.AddWithValue("@TDate", date);
cmd.ExecuteNonQuery();
cmd.Parameters.Clear(); // clear existing parameters for next iteration
}
}
}
}
else
{
// handle invalid dates
}
Note: If the data type is exactly known, use Parameters.Add() instead of Parameters.AddWithValue().
edited Nov 23 '18 at 10:11
answered Nov 23 '18 at 9:08
Tetsuya YamamotoTetsuya Yamamoto
14.8k42040
14.8k42040
Getting an error:'string' does not contain a definition for 'AddDays' and no accessible extension method 'AddDays' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
– Matt
Nov 23 '18 at 9:24
Because you have date stored as string inTDatemaybe?
– Rafalon
Nov 23 '18 at 9:25
Yesstring TDate= TDateTB.Text;
– Matt
Nov 23 '18 at 9:26
AddDaysis used forDateTime. If you want to convert a string to DateTime, usevar date = DateTime.ParseExact(TDate, "dd/MM/yyyy", CultureInfo.InvariantCulture)and usedateinsideAdd()/AddWithValue().
– Tetsuya Yamamoto
Nov 23 '18 at 9:26
hmm gettingThe name 'CultureInfo' does not exist in the current contextnow
– Matt
Nov 23 '18 at 9:34
|
show 10 more comments
Getting an error:'string' does not contain a definition for 'AddDays' and no accessible extension method 'AddDays' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
– Matt
Nov 23 '18 at 9:24
Because you have date stored as string inTDatemaybe?
– Rafalon
Nov 23 '18 at 9:25
Yesstring TDate= TDateTB.Text;
– Matt
Nov 23 '18 at 9:26
AddDaysis used forDateTime. If you want to convert a string to DateTime, usevar date = DateTime.ParseExact(TDate, "dd/MM/yyyy", CultureInfo.InvariantCulture)and usedateinsideAdd()/AddWithValue().
– Tetsuya Yamamoto
Nov 23 '18 at 9:26
hmm gettingThe name 'CultureInfo' does not exist in the current contextnow
– Matt
Nov 23 '18 at 9:34
Getting an error:
'string' does not contain a definition for 'AddDays' and no accessible extension method 'AddDays' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)– Matt
Nov 23 '18 at 9:24
Getting an error:
'string' does not contain a definition for 'AddDays' and no accessible extension method 'AddDays' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)– Matt
Nov 23 '18 at 9:24
Because you have date stored as string in
TDate maybe?– Rafalon
Nov 23 '18 at 9:25
Because you have date stored as string in
TDate maybe?– Rafalon
Nov 23 '18 at 9:25
Yes
string TDate= TDateTB.Text;– Matt
Nov 23 '18 at 9:26
Yes
string TDate= TDateTB.Text;– Matt
Nov 23 '18 at 9:26
AddDays is used for DateTime. If you want to convert a string to DateTime, use var date = DateTime.ParseExact(TDate, "dd/MM/yyyy", CultureInfo.InvariantCulture) and use date inside Add()/AddWithValue().– Tetsuya Yamamoto
Nov 23 '18 at 9:26
AddDays is used for DateTime. If you want to convert a string to DateTime, use var date = DateTime.ParseExact(TDate, "dd/MM/yyyy", CultureInfo.InvariantCulture) and use date inside Add()/AddWithValue().– Tetsuya Yamamoto
Nov 23 '18 at 9:26
hmm getting
The name 'CultureInfo' does not exist in the current context now– Matt
Nov 23 '18 at 9:34
hmm getting
The name 'CultureInfo' does not exist in the current context now– Matt
Nov 23 '18 at 9:34
|
show 10 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443301%2fc-sharp-sql-loop-in-insert-statement-from-string-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Recursive cte? Add 56 days for each iteration.
– jarlh
Nov 23 '18 at 8:50
1
What's wrong with just using DateTime.AddDays(56) in a loop from 1 to 20?
– Dylan Nicholson
Nov 23 '18 at 8:51
Use
TDate.AddDays(56)and assign parameters for every execution. Don't forget to useParameters.Clear().– Tetsuya Yamamoto
Nov 23 '18 at 8:51
@jarlh liek this?
DECLARE @i int = 0 WHILE @i < 300 BEGIN SET @i = @i + 1 /* your code*/ END– Matt
Nov 23 '18 at 8:54
Why are you not parametrising your query either? This is wide open to SQL injection. Personally, I would handle the additional rows in SQL Server as well; either by use of an rCTE (as @jarlh mentioned) or a (virtual) tally table.
– Larnu
Nov 23 '18 at 8:54