If you want to add a new entry in the database table then this topic is going to help you. Let us consider that we have a table in the database called Student and schema like this
table name: Student
Fields:
{
name varchar(50),
roll varchar(10),
cgpa float,
address varchar(50),
email varchar(50),
birthDate smalldatetime
}
During the creation of table two things must be done:
1. setting a column as an identity element with auto-increment 1
2. setting that column as the primary key.
Lets consider how this can be done. Suppose Admin is table
lets create the table using a sql query:
USE BIZNES
go
create table Admin
(
AdminID smallint IDENTITY,
AdminEmail varchar(50),
AdminPassword varchar(50),
Primary key(AdminID)
)
go
SET IDENTITY_INSERT Admin ON
Another Process:
Suppose you have created table using GUI of SQL server 200x and you want add that constraint.Use the following steps:
a. Right click on table and do to table definition
b. Select the desired column and set
Identity=Yes
Autoincrement=1
Not for replication=Yes
c. Set this column as primary key
where BIZNES is the database name. So if you design a table like this using a query
the created table is perfectly suitable for input to mygeneration tool. So please design
your own table setting identity and primary key and generate business objects like _Admin.cs file or other stored procedures.
We have already learned how to create _Student.cs or Student.cs file. If you are not familiar with this please follow my previous blogs about mygeneration tool. So now a small snippet of code is needed for you to add an entry in the database. That is:
Student std=new Student();
std.ConnectionString=ConfigurationManager.ConnectionStrings["MASUDDB
ConnectionString"].ConnectionString;
std.AddNew();
std.name="Masud";
std.roll="040201";
std.cgpa=4.63;
std.address="Farmgate,Dhaka";
std.email="test@gmail.com";
std.birthDate="25/04/1998";
std.Save();
This is how a new row or entry is created in database. You don't need an insert query for getting an entry. You can handle all this in a fine manner...isn't it?
thanks.
Masud
7.7.9
No comments:
Post a Comment