Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

Friday, 22 May 2020

Using SQL to Query CDS Data - Login failed: TDS protocol endpoint is disabled for this organization

Hi Everyone,

I was exploring on preview feature on how can we connect SQL to Dynamics Data and while connecting to CDS received below error.

"Login failed: TDS protocol endpoint is disabled for this organization."

Follow below steps to solve it.

1) Download the OrgDBOrgSettingsTool i.e. CRM2016-Tools-KB4046795-ENU-amd64 from here and run it.
2) Change CRM URL's and Connections in the Configuration File.
Line 17  - Discovery Service URL
Line 21  - Version like Online or OnPrem.
Line 25  - Username
Line 31 - CRM Service URL

Add below runtime tag after ApplicationSettings.

<runtime>
<AppContextSwitchOverrides value="Switch.System.Net.DontEnableSchUseStrongCrypto=false"></AppContextSwitchOverrides>
</runtime>

3) Run the below command by replacing Orgname.

Microsoft.Crm.SE.OrgDBOrgSettingsTool Update /u <Orgname> EnableTDSEndpoint true


Hope this helps.

--
Happy 365'ing
Gopinath

Saturday, 20 August 2016

Autogenerate GUID in SQL Table

Hi,
Today I was working on SQL Table creation and I had a requirement to generate a UniqueIdentifier automatically whenever a record is inserted into the table.

We can achieve this requirement by having Column datatype as 'UniqueIdentifier' and Default Value or Binding of the column property should be set to 'newid()'
Hope this helps.
--
Happy Coding

Gopinath

Wednesday, 11 November 2015

How to get the records count per table from SQL Server Database?

Hi,

I have started working on Data Migration work. So, started knowing more on SQL Server, Tables, Database Sizes and etc..

Today, I found the way to know the Records count per table and size of each table in the Database.
Here are the steps to follow for getting the details.

1) Logon to SQL Server Management Studio
2) Right-click on the database which you want to know the details.
3) Select Reports -> Standard Reports -> Disk Usage by Table.

4) A report will be loaded in a new tab. This might take a couple of seconds depending on the size of the database.
5) You can export the report to Excel, PDF and Word and perform your analysis on it.
Note : This procedure can be applied for any database. As CRM Database is my favorite, I have taken that as a sample.

Hope this helps.

--
Happy Querying.

Gopinath 

How to check the database size in SQL Server?

Hi,
 
Today, I got know the size of the database. But unfortunately I could not get the correct database size on the file system until I take the back up.
 
Select the database, right click and click on properties.
 
 
In the above image, the size shown 460.81 MB is sum of Data and Log files. It doesn't mean that real size of the database would be the same.
 
I have taken the back up of the database and on the file system the size mentioned was 262 MB.
 
Space Available 90.03 MB is the space that is still available to fill up. Once this space is utilized then SQL Server will allocates new Data space and Log space as per the growth rate set.
 
Use the following query to see the size of Log files and data files individually.
 
SELECT DB_NAME(database_id) AS DatabaseName,
Name AS Logical_Name,
Physical_Name, (size*8)/1024 SizeMB
FROM sys.master_files
WHERE DB_NAME(database_id) = 'DB_NAME'

Output
351 + 109 = 460 MB.
 
Hope this helps.
 
--
Cheers

Gopinath 

Thursday, 3 May 2012

C# to SQL


Connecting  SQL server  from C#
To connect to a SQL server database, you first need to set up a SQL Connection object. You then need something called a connection string to tell C# where the database is.
Specify the connection string
<connectionStrings>
    <add name="OsmEmpConString" connectionString="Data Source=.\sqlexpress;Initial Catalog=EmpolyeeDB; User ID= Sqluser ; Password= 123456789;"
    providerName="System.Data.SqlClient" />
  </connectionStrings>
DataSource = Your database source
Initial Catalog = Database name
User ID = User id of the database
Password = Password to the database
Use that connection in .cs file
string strDbCon = WebConfigurationManager.ConnectionStrings["OsmEmpConString"].ToString();
    SqlConnection _cnEmpDB = new SqlConnection(strDbCon);
       _ cnEmpDB.Open();
_ cnEmpDB is the instance of SqlConnection.
The namespace required for doing this is using System.Data.SqlClient;

C# code

Below are the methods in which you can do all types of operations from C# code.

private SqlConnection _cnEdu;

        // Open Connection
        public void OpenConnection()
        {
            AppSettingsReader objAppReader = new AppSettingsReader();
            string strDbCon = objAppReader.GetValue("DbconEdu", typeof(string)).ToString();
            _cnEdu = new SqlConnection(strDbCon);
            _cnEdu.Open();
        }

        // Close Connection
        public void CloseConnection()
        {
            if (_cnEdu == null)
            {
                return;
            }
            _cnEdu.Close();
            _cnEdu.Dispose();
        }

        // Method to check the Database connection. If the connection is closed, Opens it.
        private void CheckDbConnection()
        {
            if (_cnEdu == null)
            {
                //checks for connection
                OpenConnection();
            }
            else if (_cnEdu.State == ConnectionState.Closed)
            {
                OpenConnection();
            }
        }

        // Method which takes SQL query as parameter and returns a DataTable.
        public DataTable ExecuteDataTable(string strQuery)
        {
            if (strQuery == null && strQuery.Length == 0)//checks for the query
            {
                return null;
            }
            CheckDbConnection();
            DataTable dtReturn = new DataTable();//creates table
            using (SqlDataAdapter daExecute = new SqlDataAdapter(strQuery, _cnEdu))
            {
                daExecute.Fill(dtReturn);
            }
            return dtReturn;
        }

        // Method which takes SQL query as parameter and return true if the record exists in the database.
        public bool RecordExists(string strQuery)
        {
            CheckDbConnection();
            using (SqlCommand cmdEdu = new SqlCommand())
            {
                cmdEdu.Connection = _cnEdu;
                cmdEdu.CommandText = strQuery;
                cmdEdu.CommandType = CommandType.Text;
                Object objColValue = cmdEdu.ExecuteScalar();
                bool blnExists = true;
                if (objColValue == null)
                {
                    blnExists = false;
                }
                return blnExists;
            }
        }

        // Method which takes SQL insert and update query and executes in the database.
        public void ExecQuery(string strQuery)
        {
            CheckDbConnection();
            using (SqlCommand cmdEdu = new SqlCommand())
            {
                cmdEdu.Connection = _cnEdu;
                cmdEdu.CommandText = strQuery;
                cmdEdu.CommandType = CommandType.Text;
                int intColValue = cmdEdu.ExecuteNonQuery();
            }
        }

        // Method which takes a input parameter and executes the specified storedprocedure.
        public void ExecuteStoredProcuedure(string strEmpID)
        {
            CheckDbConnection();
            SqlCommand cmd = new SqlCommand("updateEmployee", _cnEdu);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@pi_emp_id", SqlDbType.VarChar, 20).Value = strEmpID;
            cmd.Parameters.Add("@pi_name",SqlDbType.VarChar, 50).Value = "Name";
            cmd.Parameters.Add("@po_address", SqlDbType.VarChar, 500).Value = "Address of the Employee";
            cmd.Parameters.Add("@po_email", SqlDbType.VarChar, 50).Value = "name@name.com";
            cmd.ExecuteNonQuery();
        }