I have a few tables that need a new row created on the fly. I was wondering if there is a quick and dirty way to do this without assigning values to parameters etc.
For instance table1 UserId, followed by a bunch of fields
I have a few tables that need a new row created on the fly. I was wondering if there is a quick and dirty way to do this without assigning values to parameters etc.
There is no alternative to the
SQL INSERT command, but you aren't forced to assign a value to all the fields.
If you don't set a value, SQL CE uses:
the next incremental identity value, if the field has an identity property;
the default value, if the column has a default value;
And on the fly? I guess you want to autoexecute statement than try using if else to select statements which have nulls insert data! Simple is to use INSERT INTO statement that has been told above!
Secondly the information you have provided is not enough. What you actually need to do is missing here. The only quick way is the actuall way!
Please "Marks As Answer" if any answer helped you out!
~~! FIREWALL !~~
wavemaster
Participant
1351 Points
1162 Posts
quick and dirty table row create?
Jan 26, 2013 12:51 AM|LINK
I have a few tables that need a new row created on the fly. I was wondering if there is a quick and dirty way to do this without assigning values to parameters etc.
For instance table1 UserId, followed by a bunch of fields
Something along the lines of:
SQL INSERT Table1 CurrentUser, NULLs
Result would be: 78, NULL, NULL, NULL, NULL etc
GmGregori
Contributor
5574 Points
749 Posts
Re: quick and dirty table row create?
Jan 26, 2013 08:26 AM|LINK
There is no alternative to the SQL INSERT command, but you aren't forced to assign a value to all the fields.
If you don't set a value, SQL CE uses:
So your command could be:
saeed_saedva...
Member
408 Points
74 Posts
Re: quick and dirty table row create?
Jan 26, 2013 08:44 AM|LINK
suppose that you have a table named TestTable with 2 columns Id and name. you can insert into it like below:
@{ Layout = "~/_SiteLayout.cshtml"; var db = Database.Open("StarterSite"); if(IsPost) { var query = db.Execute("Insert into TestTable (Name) VALUES (@0)","John"); } } <form method="post"> <input type="submit" value="Create Table"> </form>Saeed Saedvand
Afzaal.Ahmad...
Contributor
2759 Points
1060 Posts
Re: quick and dirty table row create?
Jan 26, 2013 12:03 PM|LINK
Create a row? Just use INSERT INTO statement.
And on the fly? I guess you want to autoexecute statement than try using if else to select statements which have nulls insert data! Simple is to use INSERT INTO statement that has been told above!
Secondly the information you have provided is not enough. What you actually need to do is missing here. The only quick way is the actuall way!
~~! FIREWALL !~~
wavemaster
Participant
1351 Points
1162 Posts
Re: quick and dirty table row create?
Jan 26, 2013 01:15 PM|LINK
I see.
If my table has the following columns : userid, col1, col2, col3 and I need to set userid but do not care about the others?
db.Execute("INSERT INTO Table1 (userid, col1, col2, col3) VALUES (@0, @1)", currentuser)
I believe that doesn't work.
userid is the key, but it is not an identity field
GmGregori
Contributor
5574 Points
749 Posts
Re: quick and dirty table row create?
Jan 26, 2013 05:22 PM|LINK
Try
db.Execute("INSERT INTO Table1 (userid) VALUES (@0)", currentuser);If col1, col2 and col3 are nullable fields it works.
Afzaal.Ahmad...
Contributor
2759 Points
1060 Posts
Re: quick and dirty table row create?
Jan 26, 2013 06:48 PM|LINK
For this case, you don't need to execute other cols.
Do as GmGregori has said!
Just execute the col as
db.Execute("INSERT INTO TABLE (UserId) VALUES (@0)", value);This will create the row, leaving all other cols null! If you check it out in Database Workspace.
Execute this code as you will see that only the column you gave the value is executed! Others are left and NULL is present there. Which has no value!
~~! FIREWALL !~~