Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
for example : i want to read ms sql server file and by using scripts to detect the data(rows,columns) and then make changes and write to new file. give me all information from beginner to expert level.
Posted
Comments
joshrduncan2012 21-Mar-14 14:28pm    
Great question for google.

Have you done some research on your own yet?
Member 10689154 21-Mar-14 14:53pm    
i found that code. but i does not understand the code.


public string GetTableScript(string TableName, string ConnectionString)
{
string Script = "";

string Sql = "declare @table varchar(100)" + Environment.NewLine +
"set @table = '" + TableName + "' " + Environment.NewLine +
//"-- set table name here" +
"declare @sql table(s varchar(1000), id int identity)" + Environment.NewLine +
" " + Environment.NewLine +
//"-- create statement" +
"insert into @sql(s) values ('create table [' + @table + '] (')" + Environment.NewLine +
" " + Environment.NewLine +
//"-- column list" +
"insert into @sql(s)" + Environment.NewLine +
"select " + Environment.NewLine +
" ' ['+column_name+'] ' + " + Environment.NewLine +
" data_type + coalesce('('+cast(character_maximum_length as varchar)+')','') +
' ' +" + Environment.NewLine +
" case when exists ( " + Environment.NewLine +
" select id from syscolumns" + Environment.NewLine +
" where object_name(id)=@table" + Environment.NewLine +
" and name=column_name" + Environment.NewLine +
" and columnproperty(id,name,'IsIdentity') = 1 " + Environment.NewLine +
" ) then" + Environment.NewLine +
" 'IDENTITY(' + " + Environment.NewLine +
" cast(ident_seed(@table) as varchar) + ',' + " + Environment.NewLine +
" cast(ident_incr(@table) as varchar) + ')'" + Environment.NewLine +
" else ''" + Environment.NewLine +
" end + ' ' +" + Environment.NewLine +
" ( case when IS_NULLABLE = 'No' then 'NOT ' else '' end ) + 'NULL ' + " + Environment.NewLine +
" coalesce('DEFAULT '+COLUMN_DEFAULT,'') + ','" + Environment.NewLine +
" " + Environment.NewLine +
" from information_schema.columns where table_name = @table" + Environment.NewLine +
" order by ordinal_position" + Environment.NewLine +
" " + Environment.NewLine +
//"-- primary key" +
"declare @pkname varchar(100)" + Environment.NewLine +
"select @pkname = constraint_name from information_schema.table_constraints" + Environment.NewLine +
"where table_name = @table and constraint_type='PRIMARY KEY'" + Environment.NewLine +
" " + Environment.NewLine +
"if ( @pkname is not null ) begin" + Environment.NewLine +
" insert into @sql(s) values(' PRIMARY KEY (')" + Environment.NewLine +
" insert into @sql(s)" + Environment.NewLine +
" select ' ['+COLUMN_NAME+'],' from information_schema.key_column_usage" + Environment.NewLine +
" where constraint_name = @pkname" + Environment.NewLine +
" order by ordinal_position" + Environment.NewLine +
//" -- remove trailing comma" +
" update @sql set s=left(s,len(s)-1) where id=@@identity" + Environment.NewLine +
" insert into @sql(s) values (' )')" + Environment.NewLine +
"end" + Environment.NewLine +
"else begin" + Environment.NewLine +
//" -- remove trailing comma" +
" update @sql set s=left(s,len(s)-1) where id=@@identity" + Environment.NewLine +
"end" + Environment.NewLine +
" " + Environment.NewLine +
"-- closing bracket" + Environment.NewLine +
"insert into @sql(s) values( ')' )" + Environment.NewLine +
" " + Environment.NewLine +
//"-- result!" +
"select s from @sql order by id";

DataTable dt = GetTableData(Sql, ConnectionString);
foreach (DataRow row in dt.Rows)
{
Script += row[0].ToString() + Environment.NewLine;
}

return Script;
}
joshrduncan2012 21-Mar-14 15:10pm    
I'm not going to step through that for you. All I can suggest is place that in Visual Studio and go into the debug mode and go step by step to see what's going on. If you don't understand it, I don't understand it.

1 solution

No one can possibly give you "all information from beginner to expert level." If you have a specific question then please ask a specific question.

The best way to learn, in my opinion, is to do. So, look for samples online, get your own db, run the scripts to see what they do, and learn.

And of course come here; but ask specific questions.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900