3 Aralık 2017 Pazar

MS SQL Server Stored Procedure'ler İçerisinde Kelime Aratmak

SELECT DISTINCT
       o.name AS Object_Name,
       o.type_desc
  FROM sys.sql_modules m
       INNER JOIN
       sys.objects o
         ON m.object_id = o.object_id
 WHERE m.definition Like '%abc%';

30 Nisan 2017 Pazar

Hide .js.map files in Visual Studio Code

In your settings (either user or workspace) there is a setting that you can tweak to hide anything you'd like:

{
    "files.exclude": {
        "**/.git": true,
        "**/.DS_Store": true,
        "**/*.js.map": true,
        "**/*.js": {"when": "$(basename).ts"}
    }
}

13 Ekim 2016 Perşembe

Eksik Foreign Key 'leri Nasıl Buluruz?

Referanslar veritabanının kalbidir. Çoğu zaman kod yazarak olması gereken veriyi hatasız bir şekilde veritabanına yazabiliriz. Ancak referansları kod yazarak doğruluğunu kontrol etmek zor bir iştir. Üstelik bir ekiple birlikte çalışıyorsanız, siz hata yapmasanız bile ekipten birileri mutlaka hata yapacaktır. En ufak bir kodsal kaçakta veritabanında hatalı veriler oluşmaya başlayacak ve büyük ihtimalle bu sorun büyüyene kadar kimse tarafından farkedilmeyecektir. Bu yüzden referansları kodsal olarak kontrol etmekten ziyade veritabanına foreign key constraintleri oluşturmalıyız. Veritabanında referans kolonların isimlendirilmesinde genelde kolon isminin sonuna "Id" eklenir. Bu şekilde o kolonun foreign key olduğunu anlarız. Aşağıdaki kod kolon isminin sonunda 'Id' bulunan ancak hiçbir Primary key veya foreign keyin bir parçası olmayan kolonları listeler.


SELECT C.TABLE_SCHEMA,C.TABLE_NAME,C.COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS C
       INNER JOIN INFORMATION_SCHEMA.TABLES T ON C.TABLE_NAME = T.TABLE_NAME AND T.TABLE_TYPE = 'Base Table' AND T.TABLE_SCHEMA = C.TABLE_SCHEMA
       LEFT JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE U ON C.TABLE_NAME = U.TABLE_NAME AND C.COLUMN_NAME = U.COLUMN_NAME AND U.TABLE_SCHEMA = C.TABLE_SCHEMA
WHERE U.COLUMN_NAME IS NULL
       AND C.COLUMN_NAME LIKE '%id'
ORDER BY C.TABLE_SCHEMA, C.TABLE_NAME, C.COLUMN_NAME

7 Mayıs 2015 Perşembe

C# Code for Downloading WeTransfer Files (https://www.wetransfer.com/)

I was doing an asp.net web application project and I needed to download files from WeTransfer. I have searched web but couldn't found any C# code about this topic. Then i found a Phyton code (Link: https://github.com/superalex/py-wetransfer) and convert it to C# by myself. Just dropping the dirty code here as in my project.(with some minor changes :)) You can clear and use the code as you wish.


using Newtonsoft.Json;

using Newtonsoft.Json.Linq;

using System;

using System.Collections.Generic;

using System.Collections.Specialized;

using System.IO;

using System.Net;

using System.Net.Http;

using System.Web;

using System.Web.UI;

 

private const string DOWNLOAD_URL_PARAMS_PREFIX = "downloads/";

private const int CHUNK_SIZE = 1024;

 

protected async void btnDownloadWeTransferFile_Click(object sender, EventArgs e)

        {

            if (string.IsNullOrWhiteSpace(txtFileLink.Text))

            {

                ClientScript.RegisterClientScriptBlock(GetType(), "alertMessage", "alert(\"" + "Link is empty!" + "\");", true);

                return;

            }

 

            HttpClient client = new HttpClient();

            HttpResponseMessage response = await client.GetAsync(txtFileLink.Text);

            response.EnsureSuccessStatusCode();

            string responseUri = response.RequestMessage.RequestUri.ToString();

 

            if (!responseUri.Contains("www.wetransfer.com"))

            {

                ClientScript.RegisterClientScriptBlock(GetType(), "alertMessage", "alert(\"" + "Make sure the file link is a WeTransfer file link!" + "\");", true);

                return;

            }

 

            string fileID = "", recipientID = "", securityHash = "";

 

            if (!responseUri.Contains(DOWNLOAD_URL_PARAMS_PREFIX))

            {

                ClientScript.RegisterClientScriptBlock(GetType(), "alertMessage", "alert(\"" + "File link is in an unknown format!" + "\");", true);

                return;

            }

            else

            {

                string[] splittedLink = responseUri.Split(new string[] { DOWNLOAD_URL_PARAMS_PREFIX }, StringSplitOptions.RemoveEmptyEntries);

                if (splittedLink.Length < 2)

                {

                    ClientScript.RegisterClientScriptBlock(GetType(), "alertMessage", "alert(\"" + "File link is in an unknown format!" + "\");", true);

                    return;

                }

                else

                {

                    string[] parameters = splittedLink[1].Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                    if (parameters.Length < 2 || parameters.Length > 3)

                    {

                        ClientScript.RegisterClientScriptBlock(GetType(), "alertMessage", "alert(\"" + "File link is in an unknown format!" + "\");", true);

                        return;

                    }

                    else

                    {

                        if (parameters.Length == 2)

                        {

                            fileID = parameters[0];

                            securityHash = parameters[1];

                        }

                        else

                        {

                            fileID = parameters[0];

                            recipientID = parameters[1];

                            securityHash = parameters[2];

                        }

                    }

                }

            }

 

            string directDownloadLink = "";

 

            WebClient wc = new WebClient();

            string jsonString = wc.DownloadString(string.Format("https://www.wetransfer.com/api/v1/transfers/{0}/download?recipient_id={1}&security_hash={2}&password=&ie=false", fileID, recipientID, securityHash));

 

            var stringData = (JObject)JsonConvert.DeserializeObject(jsonString);

            byte[] data = null;

            string fileName;

 

            if (stringData.GetValue("direct_link") != null)

            {

                directDownloadLink = stringData["direct_link"].Value<string>();

                directDownloadLink = HttpUtility.UrlDecode(directDownloadLink);

 

                int oIndex = directDownloadLink.IndexOf("filename=");

                string subStr = directDownloadLink.Substring(oIndex + 9);

                fileName = directDownloadLink.Substring(oIndex + 9, subStr.IndexOf(";")).Replace("\"", "");

 

                wc.UseDefaultCredentials = true;

 

                data = wc.DownloadData(directDownloadLink);

            }

            else

            {

                fileName = stringData["fields"]["filename"].Value<string>();

                NameValueCollection myNameValueCollection = new NameValueCollection();

                Dictionary<string, string> dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(stringData.GetValue("fields").ToString());

                foreach (var kvp in dict)

                {

                    myNameValueCollection.Add(kvp.Key.ToString(), kvp.Value.ToString());

                }

                data = wc.UploadValues(stringData["formdata"]["action"].Value<string>(), myNameValueCollection);

            }

 

            string filePath =  "C:\\" + fileName;

            if (File.Exists(filePath))

            {

                string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);

                string fileNameExtension = Path.GetExtension(fileName);

                fileName = fileNameWithoutExtension + DateTime.Now.ToString("yyyyMMddHHmmssfff") + fileNameExtension;

            }

 

            using (FileStream fs = new FileStream(filePath, FileMode.Create))

            {

                fs.Write(data, 0, data.Length);

            }

 

            ClientScript.RegisterClientScriptBlock(GetType(), "alertMessage", "alert(\"" + "File downloaded successfully. File Name:" + fileName + "\");", true);

 

        }