public void insertdataintosql(string EmpId, string EmpName, string EmpSalary, string IsActive) { SqlConnection conn = new SqlConnection ("Data Source=BBU\\SQLEXPRESS;Initial Catalog=DataBase1;Integrated Security=True"); //SqlConnection conn = new SqlConnection("Data Source=.\\sqlexpress;AttachDbFileName=C:\\Program Files\\Microsoft SQL Server\\MSSQL10.SQLEXPRESS\\MSSQL\\DATA\\Information.mdf;Trusted_Connection=yes"); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "insert into Emp (EmpId,EmpName,EmpSalary,IsActive) values(@EmpId,@EmpName,@EmpSalary,@IsActive)"; cmd.Parameters.Add("@EmpId", SqlDbType.Int).Value = EmpId; cmd.Parameters.Add("@EmpName", SqlDbType.NVarChar).Value = EmpName; cmd.Parameters.Add("@EmpSalary", SqlDbType.NVarChar).Value = EmpSalary; cmd.Parameters.Add("@IsActive", SqlDbType.NVarChar).Value = IsActive; cmd.CommandType = CommandType.Text; conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); }
SET IDENTITY_INSERT dbo.emp ON --> means: please no auto increment, i want to insert the value myself. SET IDENTITY_INSERT dbo.emp OFF --> means: please auto increment and i cannot insert the value myself.
you would set identity insert on only in the case that you would like to provide the identity value in your insert statement. SET IDENTITY_INSERT dbo.emp ON insert into emp (empno, ename,sal,deptno) values (@empno, @ename, @sal, @deptno)
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)