public static DataTable ToDataTable<T>(this IList<T> data)
{
PropertyDescriptorCollection props =
TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
for(int i = 0 ; i < props.Count ; i++)
{
PropertyDescriptor prop = props[i];
table.Columns.Add(prop.Name, prop.PropertyType);
}
object[] values = new object[props.Count];
foreach (T item in data)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = props[i].GetValue(item);
}
table.Rows.Add(values);
}
return table; }
This blog is useful to all my friends who are working on the .Net Technology and wants to enhance their skills as well their problem solving ability.
Tuesday, October 2, 2012
Convert Generic List to DataTable
Monday, October 1, 2012
Thursday, August 30, 2012
Compare Two database structure By Query only
This will give you the list of Columns along with Table Name, Column Name and Data Type that is in master but not exists or different from test.
SELECT
T.[name] AS [table_name], AC.[name] AS [column_name],
TY.[name] AS system_data_typeFROM [master].sys.[tables] AS T
INNER JOIN [master].sys.[all_columns] AC ON T.[object_id] = AC.[object_id]
INNER JOIN [master].sys.[types] TY ON AC.[system_type_id] = TY.[system_type_id]
EXCEPT
SELECT
T.[name] AS [table_name], AC.[name] AS [column_name],
TY.[name] AS system_data_typeFROM test.sys.[tables] AS T
INNER JOIN test.sys.[all_columns] AC ON T.[object_id] = AC.[object_id]
INNER JOIN test.sys.[types] TY ON AC.[system_type_id] = TY.[system_type_id]
http://dbcomparer.com/Download/Default.aspx
Friday, June 22, 2012
Know the sql database table wise size
SET NOCOUNT ON
DBCC UPDATEUSAGE(0)
-- DB size.
EXEC sp_spaceused
-- Table row counts and sizes.
CREATE TABLE #t
(
[name] NVARCHAR(128),
[rows] int,
reserved VARCHAR(18),
data VARCHAR(18),
index_size VARCHAR(18),
unused VARCHAR(18)
)
INSERT #t EXEC sp_msForEachTable 'EXEC sp_spaceused ''?'''
SELECT *
FROM #t order by rows desc
-- # of rows.
SELECT SUM(CAST([rows] AS int)) AS [rows]
FROM #t
DROP TABLE #t
Know the SQL database size in mb kb and GB
SELECT DB_NAME(database_id) AS DatabaseName,
Name AS Logical_Name,
Physical_Name, (size*8)/1024 SizeMB,
(size*8) / 1024 AS '(MB)',
((size*8) / 1024) / 1024 AS '(GB)'
FROM sys.master_files
WHERE DB_NAME(database_id) = 'Chromalox'
Tuesday, June 12, 2012
Insert/Update Sql table data using PowerShell Script
## open database connection
$conn = New-Object System.Data.SqlClient.SqlConnection("Data Source=servername; Initial Catalog=databasename; Integrated Security=SSPI")
$conn.Open()
$cmd = $conn.CreateCommand()
$cmd.CommandText ="INSERT tblTemp VALUES ('Server1', 'C:', 456)"
$cmd.ExecuteNonQuery()
$conn.Close()
Save the above code as .ps1 in your machine and then use below code in powershell.exe window to execute your script ....
powershell -ExecutionPolicy ByPass -File D:\Projects\PowerShell_ImportProject\TestScript_InsertSQL.ps1
And you are done .. same way you can use update statement or so .... you can use for loop as per your requirement to insert data into sql server....
Tuesday, May 1, 2012
Detect if JavaScript is enabled in ASPX
if (Session["JSChecked"] == null)
//JSChecked -indicates if it tried to run the javascript version
{
// prevent infinite loop
Session["JSChecked"] = "Checked";
string path = Request.Url + "?JScript=1";
Page.ClientScript.RegisterStartupScript(this.GetType(), "redirect",
"window.location.href='" + path + "';", true);
}
if (Request.QueryString["JScript"] == null)
Response.Write("JavaScript is not enabled.");
else
Response.Write("JavaScript is enabled.");
Subscribe to:
Posts (Atom)