I have a table Users with 3 columns Id, Name, Address.
I am using usersTableAdapter.Insert() for adding a new record, but it requires all 3 parameters including Id which is Identity column and auto-incremented and cannot be added manually.
Here is the code:
this.usersTableAdapter.Insert("Haroon", "Pakistan");
This is not working.
Is there any way to use the above command without providing an Id value?
Yes; you've set your tableadapter up incorrectly..
Examine this screenshot of a tableadapter conencted to a sql server table that has an autoincrement ID column. You can see the table design from SSMS in the to right (blue border). You can see highlighted that the table adapter was created with select * from autoinctable and the wizard has ONLY chosen to reference the Text column for INSERT (also highlighted)
In code, the Insert command only asks for the Text column:
This TA was created with "refresh the dataset" turned on; you can see in the commands (like insert) that the database calculated value is retrieved (select ... scope_identity)
Related
I created a project in ASP.NET MVC with a separate database project which I run every time there is a table change. My only problem is that if I add one column for example, it will drop the entire database and recreate it and delete all data in the table.
Does anyone know of a pre-deployment script or a method I can use to add / remove / rename tables or column and at the same time preserve the integrity of my data? i.e keep my data while modifying my database
You can rename columns using SQL Server functions, but doing this risks breaking scripts used by other functions or stored procedures in your database. I don't endorse this practice, so I'm not posting about it below. Adding or removing columns is fair game.
You can add columns to a table by using the following query:
ALTER TABLE [YourTable]
ADD [ColumnName] [Datatype];
And you can drop columns using this query:
ALTER TABLE [YourTable]
DROP COLUMN [ColumnName];
These SQL commands will preserve the other columns in your table. If you want to change a column name I encourage you to set up a View in your SQL Server client and give the column an alias.
This can be accomplished using:
CREATE VIEW [ViewName]
AS
SELECT [ColumnName] AS [ColumnAlias]
FROM [TableName]
GO
You'd be able to perform SELECTS on the view in just the same way you can query SELECT on a normal table, except you can query the [ColumnAlias] instead of the [ColumnName]. You cannot perform INSERT or DELETE queries on a view, however
I am currently trying to insert a DataRow into an Oracle SQL Database. The entry gets inserted into the Database but always gets a new ID. I am providing an ID in the Insert Command but it always uses an auto increment and doesn't let me insert my ID.
What can I do?
There are 2 possible solutions to your problem.
Set column as NOT to be "Identity" column. This will take care of
your issue without any hassle.
If you must keep that column as "Identity" column, set "Generated"
property to "By Default on Null". This will ensure that when you are
NOT passing any value for ID, the system generates next sequence
number automatically.
For solution 2:
DDL Statement: ID NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY INCREMENT BY 1
Table Creation in SQL Developer:
I have a SQL table with 10 columns, and two of the columns are project and employee.
What I am attempting to do is I have a drop down list of values for
the employee field in this table,
And I need to do an INSERT statement into the same table, inserting
all of the values that exist in the table, and just change the value
of the employee field on inserting.
I only want to insert a single record, so I am fearing that if I
execute an insert statement with on a WHERE project equals what the
user selected, that I will get thousands of rows inserted.
I just want to insert one single row from the table where the project
field is equal to what the user selected in a drop down, and insert
one single row copying all the data of a single row of that project,
and
Just replace the employee field with what the user selected in a
drop down.
You can INSERT using SELECT TOP 1 to get a row from the existing table with a WHERE clause that includes the required Project, like so:
DECLARE #EmployeeIdToAdd int = 12345
DECLARE #ProjectId int = 10
INSERT INTO [ProjectEmployees] (Col1, Col2,..., Project, Employee)
SELECT TOP 1 Col1, Col2,..., Project, #EmployeeIdToAdd
FROM ProjectEmployees
WHERE Project = #ProjectId
You would just need to set the parameters accordingly.
I have created table in MS SQL 2008 with one identity column(Start Value - 1 and Increment is also 1) and 4 other columns. I am accessing this DB from C# ASP.NET. Used to push data only for the non identity column. Identity column will auto increment itself.
As of now i am manually querying the column value with the remaining for columns. But I am facing problem if all the other four column values are equal i am not getting the exact value which i am looking for
Now my query is, Is there any why in C# where I can get the value of the newly created identity column whenever new record is created.
Thanks.
You can use
SCOPE_IDENTITY()
Which will returns the primary key value of the recently inserted row
The answer to your question actually lies in SQL Server. You can run:
SELECT ##identity
after your insert to get the last inserted row identity.
http://technet.microsoft.com/en-us/library/aa933167(v=sql.80).aspx
EDIT BASED ON COMMENTS:
Consider using SCOPE_IDENTITY() as referenced here:
http://technet.microsoft.com/en-us/library/aa259185(v=sql.80).aspx
In SQL terms you can output the records back if you wish it. But how you might apply this to C# is up to you. Example:
INSERT INTO TABLE_A (SOMETHING, SOMETHINGELSE, RANDOMVAL3)
OUTPUT inserted.A_ID, inserted.SOMETHING, inserted.SOMETHINGELSE, inserted.RANDOMVAL3
SELECT 'ASD','DOSD', 123
But unless you're using merge, you can't use OUTPUT to print out any values from joining tables from an INSERT. But that's another matter entirely, I think.
Also, it's hardly good practice to bounce this data between the application and the DB all the time, so I'd look to alternatives if possible.
I have added a strongly-typed Database (MySQl) to a C# (.net 3.5) project. The database (DB.Electrophysiology_Data) has several relational tables but basically I want to add a new 'filesRow' to the 'files' table. I use something like the following code.
DB.Electrophysiology_Data.filesRow new_file = ds.files.NewfilesRow();
...set the values for the new_file (eg, filename, creationdate etc)
ds.files.AddfilesRow(new_file);
...eventually I update the Database via a DataAdaptor
file_adaptor.Update(ds.files)
The problem is that the AutoIncrement column (FileID) is set to -1. After closing my applicaiton and restarting, I see the new filesRow is added to the Database ok and now has an appropriate FileID value (about 5000, since I have about this many rows already in the files DataTable).
Is there a way of knowing the FileID value set by the database using the Strong-typed Database objects (DataSet, DataTable etc).
Thanks for any help,
pete
In order for adaptor to retrieve an auto-generated ID from the database just after you inserted a new record, you need to perform SELECT.
In My SQL You can retrieve the most recent AUTO_INCREMENT value with the LAST_INSERT_ID() SQL function
If the Configure Query Wizard in stong-typed datasets cannot generate a query for MySQL in proper way, you can edit INSERT command manually and add SELECT clause here
Usually if you the command looks like
insert my_table (field1, field2, ...) values (#field1, #field2, ...)
select id, field1, field2 ... from my_table where id = LAST_INSERT_ID()
As you see, we need to select not only ID but other fields too in order to get last values from the table (that may be different from values in the dataset if any defaults exist).