SQL Delete From Table With Constraints.
--Make the System ignore the constraint
ALTER TABLE PK_ConstraintsTableName NOCHECK CONSTRAINT ALL
--Now Delete
DELETE
FROM TableName
WHERE WhereClause
--Put the contraints back
ALTER TABLE PK_ConstraintsTableName CHECK CONSTRAINT ALL
SQL - Using Case Statement To Update A Table:
CREATE TABLE #t1 (ID INT, name VARCHAR(10), val1 INT, val2 INT)
INSERT #t1 (ID , name , val1 , val2 )
SELECT 1, 'aaa01', 10, 100
UNION ALL
SELECT 2, 'bbb01', 20, 200
UNION ALL
SELECT 3, 'aaa02', 30, 300
UNION ALL
SELECT 4, 'bbb02', 40, 400
S
Here is an example of what different join produce in SQL.
SQL Complex Grouping using group substring
SQL Complex Grouping using group sub-string.
The Comma Separated Value (CSV) file contains data that (as the name suggests) are separated by commas. In C# they can easily be split into an array by using Data.Split(','). The problem occurs when data contains a comma.
C# String Wrapper Function
For a project I needed to create a string wrapper function.
The wrapper function had to follow these rules.
1: Each line has a prefix.
2: Each line may not exceed 80 charachters, including the prefix.
C# Copy From Text File To DataTable
private DataTable ReadAsDataTable(string file, bool hasHeader, char seperator)
{
DataTable dt = new DataTable();
StreamReader reader = new StreamReader(file);
string line = string.Empty;
string[] data = null;
int counter = 0;
while (reader.Peek() != -1)
{
line = reader.ReadLine();
data = l
//A simple RegEx validotor.
C# How To Add Or Subtract Days Months Years From Date
private DateTime GetDate(DateTime inputDate, string differenceType, bool add, int difference)
{
DateTime result = inputDate;
if (!add)
{
//Substraction
difference = -1 * difference;
}
switch (differenceType.ToUpper())
{
case "DAYS":
result = new DateTime(inputDate.Year, inputDate.Month, i
c# - How to create images thumbnail control using picturebox
private void AddImageToThumbnail(string[] images)
{
int top = 0;
int ht = 150;
int counter = 0;
PictureBox[] pictures = new PictureBox[images.Length];
if (images.Length > 0)
{
//The panel pnlThumbnail will hold thumbnail pictures.
C# Get All Files In A Directory
public string[] GetAllFilesInDirectory(string path, string pattern = "*.*", SearchOption options = SearchOption.TopDirectoryOnly)
{
if (path.Trim().Length == 0)
return null; //Error handler can go here
if ((pattern.Trim().Length == 0) || (pattern.Substring(pattern.Length - 1) == "."))
return nu
C# Find Most Recent File In Directory
public string GetLastFileInDirectory(string directory, string pattern = "*.*")
{
if (directory.Trim().Length == 0)
return string.Empty; //Error handler can go here
if ((pattern.Trim().Length == 0) || (pattern.Substring(pattern.Length - 1) == "."))
return string.Empty; //Error handler can go her
C# XML To SQL Stored Procedure
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Xml.Serialization;
//using System.Data;
using System.Data.SqlClient;
namespace zSharp2010.XMLToDB
{
public class Employee
{
public int ID { get; set; }
C# Serialization/DeSerialization
Let me first define the Person Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace zSharp2010.Serializer
{
[Serializable()]
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime DOB { get; set;
SQL - Check For Not Null Field
SELECT *
FROM TableName
WHERE NOT (FieldName IS NULL)
C# - VB6 Equivalent of Err.Number
In VB6 you may catch err as:
Private Sub DivideByZero()
On Error GoTo Err_Handler
Dim num As Integer
Dim zero As Integer
num = 100 / zero
Exit_Eop:
Exit Sub
Err_Handler:
MsgBox ("Error Code : " & Err.number & vbCrLf & "Error Message : " & Err.Description)
End Sub
In C# the equivalent woul
C# Using App.config file.
Reading from App.config file:
using System.Configuration;
To read from app.config file you may use the following code:
private string ReadFromAppConfig(string key)
{
return ConfigurationManager.AppSettings[key];
}
To write to app.config file you may use the following code:
private void Write
SQL C# Procedure expects parameter which was not supplied
Don't forget to put:
cmd.CommandType = CommandType.StoredProcedure;
The process cannot access the file because it is being used by another process.
The process cannot access the file 'c:\temp\Query.txt' because it is being used by another process.
Try the following code, however be very careful since you could end up reading the contents of the same file.
booleanExpression ? trueValue : falseValue;
Example: string itemText = count > 1 ? "items" : "item";
C# - VB Equivalent Of IIF
booleanExpression ? trueValue : falseValue;
Example: string itemText = count > 1 ? "items" : "item";
booleanExpression ? trueValue : falseValue;
Example: string itemText = count > 1 ? "items" : "item";
Posted
8th December 2011 by Unknown
Convert Octal To Decimal In C#
public int OctToDecimal(string data)
{
int result = 0;
char[] numbers = data.ToCharArray();
int ASCIIZero = 48;
int ASCIIEight = 56;
try
{
if (!IsNumeric(data))
error = "Invalid Value - This is not a numeric value";
else
{
for (int counter = numbers.Length; counter > 0; counter--)
{
by
Convert Decimal To Octal In C#
public string DecimalToOct(string data)
{
string result = string.Empty;
int rem = 0;
try
{
if (!IsNumeric(data))
error = "Invalid Value - This is not a numeric value";
else
{
int num = int.Parse(data);
while (num > 0)
{
rem = num % 8;
num = num / 8;
result = rem.ToString() + result;
SQL - Copy a row from same table
DECLARE @tblName VarChar(50) @tblName VarChar(50)
DECLARE @columns VarChar(5000) @columns VarChar(5000)
DECLARE @query VarChar(8000) @query VarChar(8000)
DECLARE @id int @id int
--Initialize the table and id.
Convert Decimal To Binary In C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace zSharp2008.zMath
{
class cMath
{
string error = string.Empty;
public string Error
{
get
{
return error;
}
}
public string DecimalToBinary(string data)
{
string result = string.Empty;
in
Convert Binary To Decimal In C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace zSharp2008.zMath
{
class cMath
{
string error = string.Empty;
public string Error
{
get
{
return error;
}
}
public int BinaryToDecimal(string data)
{
int result = 0;
char[] numbers = da
Using Custom Class in C# Dictionary
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace zSharp2010.Dictionary
{
class cDictionary
{
//Create the dictionary.
public bool IsPalindrome(string stringToCheck)
{
char[] rev = stringToCheck.Reverse().ToArray();
return (stringToCheck.Equals(new string(rev), StringComparison.OrdinalIgnoreCase));
}
SQL Merge two rows based on two tables.
Lets say we have two tables
Property Table:
And user table:
We need results in the format:
DECLARE @tblProperties table (PropID int, Property varchar(20), Value varchar(20))
DECLARE @tblUsers table (UserID int, Name varchar(20), PropID Int)
INSERT @tblProperties (PropID, Property, Value)
SEL
Insert multiple rows in a table.
--Creat a table containing some words.
Remove unwanted words from a table.
--USE cursor to remove invalid words from a table.
--Creat a table containing bad words
DECLARE @tblWordsToAvoidList table (sInvalidWords varchar(255))
INSERT @tblWordsToAvoidList (sInvalidWords)
SELECT ( 'aaa')
UNION ALL
SELECT ( 'eee')
--Creat a table containing some words.
The type or namespace name '??????' does not exist in the namespace
If you are getting this error check the following:
1: Right click on References
2: Add the reference to the assembly that is missing (this might be another project that you have).
View comments