select COUNT(*) as ColumnCount from information_schema.columns
where table_name = 'table_name'
Monday, December 29, 2008
Monday, December 22, 2008
Visual Studio .NET 2003 Keyboard Shortcuts
http://www.codinghorror.com/blog/files/Visual%20Studio%20.NET%202003%20Keyboard%20Shortcuts.htm
Tuesday, December 16, 2008
More date formats that does not come standard in SQL Server as part of the CONVERT function
1 To make the month name in upper case, simply use the UPPER string function.
SQL Statement | Sample Output |
SELECT SUBSTRING(CONVERT(VARCHAR(10), GETDATE(), 120), 3, 8) AS [YY-MM-DD] SELECT REPLACE(CONVERT(VARCHAR(8), GETDATE(), 11), '/', '-') AS [YY-MM-DD] | 99-01-24 |
SELECT CONVERT(VARCHAR(10), GETDATE(), 120) AS [YYYY-MM-DD] SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 111), '/', '-') AS [YYYY-MM-DD] | 1999-01-24 |
SELECT RIGHT(CONVERT(VARCHAR(8), GETDATE(), 3), 5) AS [MM/YY] SELECT SUBSTRING(CONVERT(VARCHAR(8), GETDATE(), 3), 4, 5) AS [MM/YY] | 08/99 |
SELECT RIGHT(CONVERT(VARCHAR(10), GETDATE(), 103), 7) AS [MM/YYYY] | 12/2005 |
SELECT CONVERT(VARCHAR(5), GETDATE(), 11) AS [YY/MM] | 99/08 |
SELECT CONVERT(VARCHAR(7), GETDATE(), 111) AS [YYYY/MM] | 2005/12 |
SELECT DATENAME(MM, GETDATE()) + RIGHT(CONVERT(VARCHAR(12), GETDATE(), 107), 9) AS [Month DD, YYYY] | July 04, 2006 1 |
SELECT SUBSTRING(CONVERT(VARCHAR(11), GETDATE(), 113), 4, 8) AS [Mon YYYY] | Apr 2006 1 |
SELECT DATENAME(MM, GETDATE()) + ' ' + CAST(YEAR(GETDATE()) AS VARCHAR(4)) AS [Month YYYY] | February 2006 1 |
SELECT CAST(DAY(GETDATE()) AS VARCHAR(2)) + ' ' + DATENAME(MM, GETDATE()) AS [DD Month] | 11 September 1 |
SELECT DATENAME(MM, GETDATE()) + ' ' + CAST(DAY(GETDATE()) AS VARCHAR(2)) AS [Month DD] | September 11 1 |
SELECT CAST(DAY(GETDATE()) AS VARCHAR(2)) + ' ' + DATENAME(MM, GETDATE()) + ' ' + RIGHT(CAST(YEAR(GETDATE()) AS VARCHAR(4)), 2) AS [DD Month YY] | 19 February 72 1 |
SELECT CAST(DAY(GETDATE()) AS VARCHAR(2)) + ' ' + DATENAME(MM, GETDATE()) + ' ' + CAST(YEAR(GETDATE()) AS VARCHAR(4)) AS [DD Month YYYY] | 11 September 2002 1 |
SELECT RIGHT(CONVERT(VARCHAR(8), GETDATE(), 5), 5) AS [MM-YY] SELECT SUBSTRING(CONVERT(VARCHAR(8), GETDATE(), 5), 4, 5) AS [MM-YY] | 12/92 |
SELECT RIGHT(CONVERT(VARCHAR(10), GETDATE(), 105), 7) AS [MM-YYYY] | 05-2006 |
SELECT RIGHT(CONVERT(VARCHAR(7), GETDATE(), 120), 5) AS [YY-MM] SELECT SUBSTRING(CONVERT(VARCHAR(10), GETDATE(), 120), 3, 5) AS [YY-MM] | 92/12 |
SELECT CONVERT(VARCHAR(7), GETDATE(), 120) AS [YYYY-MM] | 2006-05 |
SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 1), '/', '') AS [MMDDYY] | 122506 |
SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 101), '/', '') AS [MMDDYYYY] | 12252006 |
SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 3), '/', '') AS [DDMMYY] | 240702 |
SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 103), '/', '') AS [DDMMYYYY] | 24072002 |
SELECT REPLACE(RIGHT(CONVERT(VARCHAR(9), GETDATE(), 6), 6), ' ', '-') AS [Mon-YY] | Sep-02 1 |
SELECT REPLACE(RIGHT(CONVERT(VARCHAR(11), GETDATE(), 106), 8), ' ', '-') AS [Mon-YYYY] | Sep-2002 1 |
SELECT REPLACE(CONVERT(VARCHAR(9), GETDATE(), 6), ' ', '-') AS [DD-Mon-YY] | 25-Dec-05 1 |
SELECT REPLACE(CONVERT(VARCHAR(11), GETDATE(), 106), ' ', '-') AS [DD-Mon-YYYY] | 25-Dec-2005 1 |
SQL Server Date Formats
SQL Statement | Sample Output |
SELECT CONVERT(VARCHAR(20), GETDATE(), 100) | Jan 1 2005 1:29PM 1 |
SELECT CONVERT(VARCHAR(8), GETDATE(), 1) AS [MM/DD/YY] | 11/23/98 |
SELECT CONVERT(VARCHAR(10), GETDATE(), 101) AS [MM/DD/YYYY] | 11/23/1998 |
SELECT CONVERT(VARCHAR(8), GETDATE(), 2) AS [YY.MM.DD] | 72.01.01 |
SELECT CONVERT(VARCHAR(10), GETDATE(), 102) AS [YYYY.MM.DD] | 1972.01.01 |
SELECT CONVERT(VARCHAR(8), GETDATE(), 3) AS [DD/MM/YY] | 19/02/72 |
SELECT CONVERT(VARCHAR(10), GETDATE(), 103) AS [DD/MM/YYYY] | 19/02/1972 |
SELECT CONVERT(VARCHAR(8), GETDATE(), 4) AS [DD.MM.YY] | 25.12.05 |
SELECT CONVERT(VARCHAR(10), GETDATE(), 104) AS [DD.MM.YYYY] | 25.12.2005 |
SELECT CONVERT(VARCHAR(8), GETDATE(), 5) AS [DD-MM-YY] | 24-01-98 |
SELECT CONVERT(VARCHAR(10), GETDATE(), 105) AS [DD-MM-YYYY] | 24-01-1998 |
SELECT CONVERT(VARCHAR(9), GETDATE(), 6) AS [DD MON YY] | 04 Jul 06 1 |
SELECT CONVERT(VARCHAR(11), GETDATE(), 106) AS [DD MON YYYY] | 04 Jul 2006 1 |
SELECT CONVERT(VARCHAR(10), GETDATE(), 7) AS [Mon DD, YY] | Jan 24, 98 1 |
SELECT CONVERT(VARCHAR(12), GETDATE(), 107) AS [Mon DD, YYYY] | Jan 24, 1998 1 |
SELECT CONVERT(VARCHAR(8), GETDATE(), 108) | 03:24:53 |
SELECT CONVERT(VARCHAR(26), GETDATE(), 109) | Apr 28 2006 12:32:29:253PM 1 |
SELECT CONVERT(VARCHAR(8), GETDATE(), 10) AS [MM-DD-YY] | 01-01-06 |
SELECT CONVERT(VARCHAR(10), GETDATE(), 110) AS [MM-DD-YYYY] | 01-01-2006 |
SELECT CONVERT(VARCHAR(8), GETDATE(), 11) AS [YY/MM/DD] | 98/11/23 |
SELECT CONVERT(VARCHAR(10), GETDATE(), 111) AS [YYYY/MM/DD] | 1998/11/23 |
SELECT CONVERT(VARCHAR(6), GETDATE(), 12) AS [YYMMDD] | 980124 |
SELECT CONVERT(VARCHAR(8), GETDATE(), 112) AS [YYYYMMDD] | 19980124 |
SELECT CONVERT(VARCHAR(24), GETDATE(), 113) | 28 Apr 2006 00:34:55:190 1 |
SELECT CONVERT(VARCHAR(12), GETDATE(), 114) AS [HH:MI:SS:MMM(24H)] | 11:34:23:013 |
SELECT CONVERT(VARCHAR(19), GETDATE(), 120) | 1972-01-01 13:42:24 |
SELECT CONVERT(VARCHAR(23), GETDATE(), 121) | 1972-02-19 06:35:24.489 |
SELECT CONVERT(VARCHAR(23), GETDATE(), 126) | 1998-11-23T11:25:43:250 |
SELECT CONVERT(VARCHAR(26), GETDATE(), 130) | 28 Apr 2006 12:39:32:429AM 1 |
SELECT CONVERT(VARCHAR(25), GETDATE(), 131) | 28/04/2006 12:39:32:429AM |
Sunday, December 14, 2008
Thursday, December 11, 2008
Check for an Integer Value-Without Exception
bool isvalid;
int number;
isvalid = int.TryParse(textBox1.Text,out number);
if (isvalid)
{
MessageBox.Show("Number");
}
if (isvalid == false)
{
MessageBox.Show("Not a number");
}
int number;
isvalid = int.TryParse(textBox1.Text,out number);
if (isvalid)
{
MessageBox.Show("Number");
}
if (isvalid == false)
{
MessageBox.Show("Not a number");
}
Monday, December 8, 2008
How to get configuraion Settings
It is not enough to add the reference in the code. You have to manually add the reference using Add reference. If not ConfigurationManager wont visible.
using System.Collections.Specialized;
using System.Collections;
using System.Configuration;
NameValueCollection appSettings = ConfigurationManager.AppSettings;
string userName = appSettings["UserName"].ToString();
using System.Collections.Specialized;
using System.Collections;
using System.Configuration;
NameValueCollection appSettings = ConfigurationManager.AppSettings;
string userName = appSettings["UserName"].ToString();
RUN a SP
SqlConnection con=new SqlConnection("User ID=" + ConfigurationSettings.AppSettings.Get("UserName").ToString() + ";Password=" + ConfigurationSettings.AppSettings.Get("Password").ToString() + ";Initial Catalog=" + ConfigurationSettings.AppSettings.Get("DBName").ToString() + ";Data Source=" + ConfigurationSettings.AppSettings.Get("ServerName").ToString() + ";Persist Security Info= true;");
con.Open();
SqlCommand cmd=new SqlCommand("SPNAME",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@SPPARAM", PARAM));
cmd.CommandTimeout=0;
SqlTransaction t=con.BeginTransaction();
cmd.Transaction=t;
try
{
cmd.ExecuteNonQuery();
t.Commit();
con.Close();
}
catch(Exception ex)
{
t.Rollback();
con.Close();
string s = ex.Message;
}
con.Open();
SqlCommand cmd=new SqlCommand("SPNAME",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@SPPARAM", PARAM));
cmd.CommandTimeout=0;
SqlTransaction t=con.BeginTransaction();
cmd.Transaction=t;
try
{
cmd.ExecuteNonQuery();
t.Commit();
con.Close();
}
catch(Exception ex)
{
t.Rollback();
con.Close();
string s = ex.Message;
}
Creating a transaction
SqlConnection con=new SqlConnection("User ID=" + ConfigurationSettings.AppSettings.Get("UserName").ToString() + ";Password=" + ConfigurationSettings.AppSettings.Get("Password").ToString() + ";Initial Catalog=" + ConfigurationSettings.AppSettings.Get("DBName").ToString() + ";Data Source=" + ConfigurationSettings.AppSettings.Get("ServerName").ToString() + ";Persist Security Info= true;");
con.Open();
SqlCommand cmd=new SqlCommand(sql,con);
cmd.CommandTimeout=0;
SqlTransaction t=con.BeginTransaction();
cmd.Transaction=t;
try
{
SqlDataAdapter adpt=new SqlDataAdapter(cmd);
adpt.Fill(dsTemp);
t.Commit();
con.Close();
return ds;
}
catch(Exception ex)
{
t.Rollback();
con.Close();
return new DataSet();
}
con.Open();
SqlCommand cmd=new SqlCommand(sql,con);
cmd.CommandTimeout=0;
SqlTransaction t=con.BeginTransaction();
cmd.Transaction=t;
try
{
SqlDataAdapter adpt=new SqlDataAdapter(cmd);
adpt.Fill(dsTemp);
t.Commit();
con.Close();
return ds;
}
catch(Exception ex)
{
t.Rollback();
con.Close();
return new DataSet();
}
Subscribe to:
Posts (Atom)