Showing posts with label sql server. Show all posts
Showing posts with label sql server. Show all posts

Thursday, October 29, 2015

SQL Server: Save changes is not permitted. Resolution

Save changes is not permitted. The changes you have made require the following tables to be dropped and recreated. You have either made changes to a table that can't be re-created or enabled the option Prevent saving changes that require the table to be re-created.

User canceled out of save dialog.

Solution:

  • Go to Tools > Options

  • Go to Designers > Table and Database Designers
  • Uncheck the Prevent saving changes that require the table recreation.

Monday, July 27, 2015

SqlBulkCopy: Insert bulk data to SQL Server database (more than 10 million records)

To insert 10 Million Records. Following is the code to insert bulk data in to SQL Server Database in .net.
using System;
using System.Data;
using System.Data.SqlClient;

namespace BulkCopyInsert
{
    class BlukCopy
    {
        static string DestinationDbConnection = "Data Source=192.168.0.5;Initial Catalog=DatabaseName;Persist Security Info=True;User ID=sa;Password=Password@123";

        static void Main(string[] args)
        {
            DataTable dt = GetDataTable();
            DataRow dr;
            DateTime dtm = DateTime.Now;
            //start dummy data creation
            for (long i = 0; i < 5000000; i++)
            {
                dr = dt.NewRow();

                dr["Name"] = "nitin dhiman";
                dr["Age"] = i;
                dr["ManagerId"] = i;
                dr["CreateDate"] = dtm;

                dt.Rows.Add(dr);
            }
            //End dummy data created
            //Start Bulk Insert
            SqlBulkCopy objBulkCopy = new SqlBulkCopy(DestinationDbConnection);
            objBulkCopy.DestinationTableName = "UserTable";//Table Name, must match with sql table
            objBulkCopy.BatchSize = 10000;
            objBulkCopy.BulkCopyTimeout = 600; //Seconds
            foreach (DataColumn dc in dt.Columns)
            {
                objBulkCopy.ColumnMappings.Add(dc.ColumnName, dc.ColumnName);
            }

            objBulkCopy.WriteToServer(dt);
            //End Bulk Insert
        }

        static DataTable GetDataTable()
        {
            try
            {
                //Columns of table, name must match with sql table columns
                DataTable dt = new DataTable();
                dt.Columns.Add("Name", typeof(string));
                dt.Columns.Add("Department", typeof(string));
                dt.Columns.Add("Age", typeof(int));
                dt.Columns.Add("ManagerId", typeof(int));
                dt.Columns.Add("CreateDate", typeof(DateTime));

                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}


Thursday, June 11, 2015

Search text in SQL Stored Procedure



#Declare an input text as given below.

DECLARE@SEARCH_TEXT VARCHAR(255)
SET@SEARCH_TEXT= 'nitindhiman@gmail.com’

#Query for Search Text in Stored Procedure

SELECT DISTINCT
OBJ.NAME AS OBJECT_NAME,
OBJ.TYPE_DESC
FROM SYS.SQL_MODULES MOD
INNER JOIN SYS.OBJECTS OBJ ON MOD.OBJECT_ID = OBJ.OBJECT_ID
WHERE MOD.DEFINITION LIKE '%'+@SEARCH_TEXT+'%'
ORDER BY TYPE_DESC