Tuesday, November 23, 2010
Tuesday, November 16, 2010
Insert multiple rows from gridview into database
you can use Sql bulk copy for this purpose
private void StartImport()
{
SqlBulkCopy bulkCopy = new SqlBulkCopy("Server=ServerName;Database=test;Trusted_Connection=True;",
SqlBulkCopyOptions.TableLock);
bulkCopy.DestinationTableName = "dest table Name";DataTable dt = CreateDataTableFromFile();
bulkCopy.WriteToServer(dt);
}
private DataTable CreateDataTableFromFile()
{//create a datatable and fill it with the values from the grridview
return the Datatable;
}
Friday, November 12, 2010
Difference Between Sql-server 2000 & 2005
I've been asked this question every time that there's a new version and yet I've never been able to give what I think is a nice, concise, logical answer that satisfies the asker. Probably it's a lack of my ability to easily form words in my mouth and get them out in the proper order, so I decided it might make some sense to do this on paper (metaphorically speaking) and help others out.
Like many of you, I usually get this question from someone
outside of SQL Server. A windows admin, a network guy, etc.,
someone who has little contact with SQL Server. Or maybe it's
someone who's been stuck with admin'ing a SQL Server instance.
In any case, I wanted to try and explain this concisely for the
non-DBAs. As I began this project, however I soon realized that
it's not easy to just give a good general answer. As with
everything else in SQL Server it seems that "it depends" is the
best general answer, so I broke this up into a few areas. This
part will look at the administrative differences and the next
will cover more of the development differences.
The Administrative Differences
Administering a SQL Server instance to me means making sure the
server service runs efficiently and is stable and allows clients
to access the data. The instance should keep data intact and
function according to the rules of the code implemented while
being well maintained.
Or for the non-DBAs, it means that you are the sysadmin and it
just works.
The overall differences are few. Sure we use Management Studio
instead of Enterprise Manager, but that's not really a big deal.
Really many of the changes, like being able to change connections
for a query, are superficial improvements that don't really
present a substantial change. If you think they do, you might be
in the wrong job.
Security is one area that is a very nice improvement. The
separation of the schema from the owner makes administrative
changes easier, but that is a big deal because it greatly
increases the chances you won't keep an old account active
because it's a pain to change owners on objects. There's also
more granularity and ease of administration using the schema as
another level of assigning permissions.
Another big security change is the ability to secure your web
services using certificates instead of requiring authentication
using a name and password. Add to that the capability to encrypt
data, and manage the keys, can make a big difference in the
overall security of your data. You have to carefully ensure your
application and access is properly secured, but just the
marketing value of encryption when you have credit card,
financial, or medical data is huge. SQL Server 2000 had no real
security features for data, allowing an administrator to see all
data. You could purchase a third party add-on, but it was
expensive and required staff training. Not that you don't need to
learn about SQL Server 2005, but it should be a skill that most
DBAs will learn and be able to bring to your organization over time.
High availability is becoming more and more important to all
sizes of businesses. In the past, clustering or log shipping were
your main choices, but both were expensive and required the
Enterprise Edition. This put these features out of the reach of
many companies, or at least, out of many DBAs' budgets. With SQL
Server 2005, you can now implement clustering, log shipping, or
the new Database Mirroring with the Standard edition. With the
ability of Database Mirroring to use commodity hardware, even
disparate hardware between the primary and mirror databases, this
is a very reasonable cost solution for almost any enterprise.
There are also online indexes, online restores, and fast recovery
in the Enterprise Edition that can help ensure that you take less
downtime. Fast recovery especially can be an important feature,
allowing the database to be accessed as the undo operations
start. With a log of open transactions when a database is
restarted, this can really add up to significant amounts of time.
In SQL Server 2000, you had to have a complete, intact database
before anyone could access it. With redo/undo operations
sometimes taking a significant amount of time, this could delay
the time from Windows startup to database availability by minutes.
Data sizes always grow and for most companies, performance is
always an issue on some server. With SQL Server 2000, you were
limited to using 2GB of RAM and 4 CPUs on the Standard Edition.
The number of CPUs hasn't changed, but you can now use as much
RAM as the OS allows. There also is no limit to the database
size, not that the 1,048,516 TB in SQL Server 2000. Since RAM is
usually a limiting factor in the performance of many databases,
upgrading to SQL Server 2005 could be something you can take
advantage of. SQL Server 2005 also has more options and
capabilities on the 64-bit platform than SQL Server 2000.
Why Upgrade?
This is an interesting question and one I've been asked quite a
bit over the last 18 months since SQL Server 2005 has been
released. The short answer is that if SQL Server 2000 meets your
needs, then there's no reason to upgrade. SQL Server 2000 is a
strong, stable platform that has worked well for millions of
installations. If it meets your needs, you are not running up
against the limits of the platform, and you are happy with your
system, then don't upgrade.
However, there is a caveat to this. First the support timeline
for SQL Server 2000 shows mainstream support ending next year, in
April 2008. I can't imagine that Microsoft wouldn't extend that
given the large number of installations of SQL Server 2000, but
with the next version of SQL Server likely to come out next year,
I can see this being the point at which you cannot call for
regular support. The extended support timeline continues through
2013, but that's an expensive option.
The other consideration is that with a new version coming out
next year, you might want to just start making plans to upgrade
to that version even if you're happy with SQL Server 2000. If the
plan is to release a new version every 2-3 years, you'll need to
upgrade at least every 5-6 years to maintain support options.
Be sure that in any case you are sure the application you are
upgrading, if it's a third party, is supported on SQL Server 2005.
Lastly, if you have multiple servers and are considering new
hardware for more than 1 of them, it might make some sense to be
sure to look at buying one large 64-bit server and performing
some consolidations. I might recommend that you wait for the next
version of SQL Server if you are worried about conflicts as I
have heard rumors of switches to help govern the resource usage
in Katmai (SQL Server 2008).
A quick summary of the differences:
Feature SQL Server 2000 SQL Server 2005
Security Owner = Schema, hard to remove old users at times Schema
is separate. Better granularity in easily controlling security.
Logins can be authenticated by certificates.
Encryption No options built in, expensive third party options
with proprietary skills required to implement properly.
Encryption and key management build in.
High Availability Clustering or Log Shipping require Enterprise
Edition. Expensive hardware. Clustering, Database Mirroring or
Log Shipping available in Standard Edition. Database Mirroring
can use cheap hardware.
Scalability Limited to 2GB, 4CPUs in Standard Edition. Limited
64-bit support. 4 CPU, no RAM limit in Standard Edition. More
64-bit options offer chances for consolidation.
Conclusion
These seem to be the major highlights from my perspective as an
administrator. While there are other improvements, such as the
schema changes flowing through replication, I'm not sure that
they represent compelling changes for the non-DBA.
**************************************************************************************************************************************************************************
2005sqlser support try catch which is not possible in 2000
In SQL Server 2000 , a row cannot exceed 8000 bytes in
size ... To solve this problem, Microsoft introduced the
VARCHAR(MAX), NVARCHAR(MAX), and VARBINARY(MAX) data types
in SQL Server 2005. These data types can hold the same
amount of data BLOBs can hold (2 GB)
**************************************************************************************************************************************************************************
--Reference:
http://stackoverflow.com/questions/198478/advantages-of-ms-
sql-server-2008-over-ms-sql-server-2005
SQL SERVER 2000:
1.Query Analyser and Enterprise manager are separate.
2.No XML datatype is used.
3.We can create maximum of 65,535 databases.
4.Nill
5.Nill
6.Nill
7.Nill
8.Nill
9.Nill
10.Nill
11.Nill
12.Nill
13.cant compress the tables and indexes.
14.Datetime datatype is used for both date and time.
15.No varchar(max) or varbinary(max) is available.
16.No table datatype is included.
17.No SSIS is included.
18.CMS is not available.
19.PBM is not available.
20.PIVOT and UNPIVOT functions are not used.
SQL SERVER 2005:
1.Both are combined as SSMS(Sql Server management Studio).
2.XML datatype is introduced.
3.We can create 2(pow(20))-1 databases.
4.Exception Handling
5.Varchar(Max) data type
6.DDL Triggers
7.DataBase Mirroring
8.RowNumber function for paging
9.Table fragmentation
10.Full Text Search
11.Bulk Copy Update
12.Cant encrypt
13.Can Compress tables and indexes.(Introduced in 2005 SP2)
14.Datetime is used for both date and time.
15.Varchar(max) and varbinary(max) is used.
16.No table datatype is included.
17.SSIS is started using.
18.CMS is not available.
19.PBM is not available.
20.PIVOT and UNPIVOT functions are used.
SQL SERVER 2008:
1.Both are combined as SSMS(Sql Server management Studio).
2.XML datatype is used.
3.We can create 2(pow(20))-1 databases.
4.Exception Handling
5.Varchar(Max) data type
6.DDL Triggers
7.DataBase Mirroring
8.RowNumber function for paging
9.Table fragmentation
10.Full Text Search
11.Bulk Copy Update
12.Can encrypt the entire database introduced in 2008.
--check it(http://technet.microsoft.com/en-
us/library/cc278098(SQL.100).aspx)
(http://www.sqlservercentral.com/articles/Administration/imp
lementing_efs/870/)
(http://www.kodyaz.com/articles/sql-server-2005-
database-encryption-step-by-step.aspx)
(http://www.sql-server-
performance.com/articles/dev/encryption_2005_1_p1.aspx)
(http://geekswithblogs.net/chrisfalter/archive/2008/05/08/en
crypt-documents-with-sql-server.aspx)
13.Can compress tables and indexes.
-http://www.mssqltips.com/tip.asp?tip=1582
14.Date and time are seperately used for date and time
datatype,geospatial and timestamp with internal timezone
is used.
15.Varchar(max) and varbinary(max) is used.
16.Table datatype introduced.
17.SSIS avails in this version.
18.Central Management Server(CMS) is Introduced.
-http://msdn.microsoft.com/en-
us/library/bb934126.aspx
-http://www.sqlskills.com/BLOGS/KIMBERLY/post/SQL-
Server-2008-Central-Management-Servers-have-you-seen-
these.aspx
19.Policy based management(PBM) server is Introduced.
-http://www.mssqltips.com/tip.asp?tip=1492
-http://msdn.microsoft.com/en-
us/library/bb510667.aspx
20.PIVOT and UNPIVOT functions are used.
-http://blog.sqlauthority.com/2008/06/07/sql-server-
pivot-and-unpivot-table-examples/
SOURCE:allinterview
Like many of you, I usually get this question from someone
outside of SQL Server. A windows admin, a network guy, etc.,
someone who has little contact with SQL Server. Or maybe it's
someone who's been stuck with admin'ing a SQL Server instance.
In any case, I wanted to try and explain this concisely for the
non-DBAs. As I began this project, however I soon realized that
it's not easy to just give a good general answer. As with
everything else in SQL Server it seems that "it depends" is the
best general answer, so I broke this up into a few areas. This
part will look at the administrative differences and the next
will cover more of the development differences.
The Administrative Differences
Administering a SQL Server instance to me means making sure the
server service runs efficiently and is stable and allows clients
to access the data. The instance should keep data intact and
function according to the rules of the code implemented while
being well maintained.
Or for the non-DBAs, it means that you are the sysadmin and it
just works.
The overall differences are few. Sure we use Management Studio
instead of Enterprise Manager, but that's not really a big deal.
Really many of the changes, like being able to change connections
for a query, are superficial improvements that don't really
present a substantial change. If you think they do, you might be
in the wrong job.
Security is one area that is a very nice improvement. The
separation of the schema from the owner makes administrative
changes easier, but that is a big deal because it greatly
increases the chances you won't keep an old account active
because it's a pain to change owners on objects. There's also
more granularity and ease of administration using the schema as
another level of assigning permissions.
Another big security change is the ability to secure your web
services using certificates instead of requiring authentication
using a name and password. Add to that the capability to encrypt
data, and manage the keys, can make a big difference in the
overall security of your data. You have to carefully ensure your
application and access is properly secured, but just the
marketing value of encryption when you have credit card,
financial, or medical data is huge. SQL Server 2000 had no real
security features for data, allowing an administrator to see all
data. You could purchase a third party add-on, but it was
expensive and required staff training. Not that you don't need to
learn about SQL Server 2005, but it should be a skill that most
DBAs will learn and be able to bring to your organization over time.
High availability is becoming more and more important to all
sizes of businesses. In the past, clustering or log shipping were
your main choices, but both were expensive and required the
Enterprise Edition. This put these features out of the reach of
many companies, or at least, out of many DBAs' budgets. With SQL
Server 2005, you can now implement clustering, log shipping, or
the new Database Mirroring with the Standard edition. With the
ability of Database Mirroring to use commodity hardware, even
disparate hardware between the primary and mirror databases, this
is a very reasonable cost solution for almost any enterprise.
There are also online indexes, online restores, and fast recovery
in the Enterprise Edition that can help ensure that you take less
downtime. Fast recovery especially can be an important feature,
allowing the database to be accessed as the undo operations
start. With a log of open transactions when a database is
restarted, this can really add up to significant amounts of time.
In SQL Server 2000, you had to have a complete, intact database
before anyone could access it. With redo/undo operations
sometimes taking a significant amount of time, this could delay
the time from Windows startup to database availability by minutes.
Data sizes always grow and for most companies, performance is
always an issue on some server. With SQL Server 2000, you were
limited to using 2GB of RAM and 4 CPUs on the Standard Edition.
The number of CPUs hasn't changed, but you can now use as much
RAM as the OS allows. There also is no limit to the database
size, not that the 1,048,516 TB in SQL Server 2000. Since RAM is
usually a limiting factor in the performance of many databases,
upgrading to SQL Server 2005 could be something you can take
advantage of. SQL Server 2005 also has more options and
capabilities on the 64-bit platform than SQL Server 2000.
Why Upgrade?
This is an interesting question and one I've been asked quite a
bit over the last 18 months since SQL Server 2005 has been
released. The short answer is that if SQL Server 2000 meets your
needs, then there's no reason to upgrade. SQL Server 2000 is a
strong, stable platform that has worked well for millions of
installations. If it meets your needs, you are not running up
against the limits of the platform, and you are happy with your
system, then don't upgrade.
However, there is a caveat to this. First the support timeline
for SQL Server 2000 shows mainstream support ending next year, in
April 2008. I can't imagine that Microsoft wouldn't extend that
given the large number of installations of SQL Server 2000, but
with the next version of SQL Server likely to come out next year,
I can see this being the point at which you cannot call for
regular support. The extended support timeline continues through
2013, but that's an expensive option.
The other consideration is that with a new version coming out
next year, you might want to just start making plans to upgrade
to that version even if you're happy with SQL Server 2000. If the
plan is to release a new version every 2-3 years, you'll need to
upgrade at least every 5-6 years to maintain support options.
Be sure that in any case you are sure the application you are
upgrading, if it's a third party, is supported on SQL Server 2005.
Lastly, if you have multiple servers and are considering new
hardware for more than 1 of them, it might make some sense to be
sure to look at buying one large 64-bit server and performing
some consolidations. I might recommend that you wait for the next
version of SQL Server if you are worried about conflicts as I
have heard rumors of switches to help govern the resource usage
in Katmai (SQL Server 2008).
A quick summary of the differences:
Feature SQL Server 2000 SQL Server 2005
Security Owner = Schema, hard to remove old users at times Schema
is separate. Better granularity in easily controlling security.
Logins can be authenticated by certificates.
Encryption No options built in, expensive third party options
with proprietary skills required to implement properly.
Encryption and key management build in.
High Availability Clustering or Log Shipping require Enterprise
Edition. Expensive hardware. Clustering, Database Mirroring or
Log Shipping available in Standard Edition. Database Mirroring
can use cheap hardware.
Scalability Limited to 2GB, 4CPUs in Standard Edition. Limited
64-bit support. 4 CPU, no RAM limit in Standard Edition. More
64-bit options offer chances for consolidation.
Conclusion
These seem to be the major highlights from my perspective as an
administrator. While there are other improvements, such as the
schema changes flowing through replication, I'm not sure that
they represent compelling changes for the non-DBA.
**************************************************************************************************************************************************************************
2005sqlser support try catch which is not possible in 2000
In SQL Server 2000 , a row cannot exceed 8000 bytes in
size ... To solve this problem, Microsoft introduced the
VARCHAR(MAX), NVARCHAR(MAX), and VARBINARY(MAX) data types
in SQL Server 2005. These data types can hold the same
amount of data BLOBs can hold (2 GB)
**************************************************************************************************************************************************************************
--Reference:
http://stackoverflow.com/questions/198478/advantages-of-ms-
sql-server-2008-over-ms-sql-server-2005
SQL SERVER 2000:
1.Query Analyser and Enterprise manager are separate.
2.No XML datatype is used.
3.We can create maximum of 65,535 databases.
4.Nill
5.Nill
6.Nill
7.Nill
8.Nill
9.Nill
10.Nill
11.Nill
12.Nill
13.cant compress the tables and indexes.
14.Datetime datatype is used for both date and time.
15.No varchar(max) or varbinary(max) is available.
16.No table datatype is included.
17.No SSIS is included.
18.CMS is not available.
19.PBM is not available.
20.PIVOT and UNPIVOT functions are not used.
SQL SERVER 2005:
1.Both are combined as SSMS(Sql Server management Studio).
2.XML datatype is introduced.
3.We can create 2(pow(20))-1 databases.
4.Exception Handling
5.Varchar(Max) data type
6.DDL Triggers
7.DataBase Mirroring
8.RowNumber function for paging
9.Table fragmentation
10.Full Text Search
11.Bulk Copy Update
12.Cant encrypt
13.Can Compress tables and indexes.(Introduced in 2005 SP2)
14.Datetime is used for both date and time.
15.Varchar(max) and varbinary(max) is used.
16.No table datatype is included.
17.SSIS is started using.
18.CMS is not available.
19.PBM is not available.
20.PIVOT and UNPIVOT functions are used.
SQL SERVER 2008:
1.Both are combined as SSMS(Sql Server management Studio).
2.XML datatype is used.
3.We can create 2(pow(20))-1 databases.
4.Exception Handling
5.Varchar(Max) data type
6.DDL Triggers
7.DataBase Mirroring
8.RowNumber function for paging
9.Table fragmentation
10.Full Text Search
11.Bulk Copy Update
12.Can encrypt the entire database introduced in 2008.
--check it(http://technet.microsoft.com/en-
us/library/cc278098(SQL.100).aspx)
(http://www.sqlservercentral.com/articles/Administration/imp
lementing_efs/870/)
(http://www.kodyaz.com/articles/sql-server-2005-
database-encryption-step-by-step.aspx)
(http://www.sql-server-
performance.com/articles/dev/encryption_2005_1_p1.aspx)
(http://geekswithblogs.net/chrisfalter/archive/2008/05/08/en
crypt-documents-with-sql-server.aspx)
13.Can compress tables and indexes.
-http://www.mssqltips.com/tip.asp?tip=1582
14.Date and time are seperately used for date and time
datatype,geospatial and timestamp with internal timezone
is used.
15.Varchar(max) and varbinary(max) is used.
16.Table datatype introduced.
17.SSIS avails in this version.
18.Central Management Server(CMS) is Introduced.
-http://msdn.microsoft.com/en-
us/library/bb934126.aspx
-http://www.sqlskills.com/BLOGS/KIMBERLY/post/SQL-
Server-2008-Central-Management-Servers-have-you-seen-
these.aspx
19.Policy based management(PBM) server is Introduced.
-http://www.mssqltips.com/tip.asp?tip=1492
-http://msdn.microsoft.com/en-
us/library/bb510667.aspx
20.PIVOT and UNPIVOT functions are used.
-http://blog.sqlauthority.com/2008/06/07/sql-server-
pivot-and-unpivot-table-examples/
SOURCE:allinterview
Wednesday, November 3, 2010
How to display a link without underline and display underline when mouseover on the link using CSS?
Write following css class.
a
{
text-decoration:none;
}
a:hover
{
text-decoration:underline;
}
The first class will force all anchor tag (link)to not display any docoration (underline) and second class will force all anchor tag (link) to display text decoration as underline when mouse over it (ie. display underline when mouse over).
Wednesday, October 27, 2010
Friday, October 22, 2010
Partial Classes
Partial types is a new feature in programming that is introduced in Visual Studio.Net 2005. This feature helps you to divide your types such as class, interface or any type definition across multiple files. At present, when you write any class you need to have the class in a single file, i.e., you need to start the class and end the class in the same file. In VS.Net 2005, you have the option of separating your code for the class across different files. You may split a single class across different files based on functionality, so that you have different developers work on different methods.
There is no modification done in the CLR for partial classes. The implementation of partial classes is done by the compiler for VB.Net and C# in Visual Studio .Net itself. For the CLR, whether you implement the code using partial classes or the class being in a single file, is the same. The compiler, when compiling the code in VS.Net 2005, looks for the partial classes and combines them so that the end result will look as if the code were written in a single class. After compilation the IL produced will look the same for both the implementation.
In Visual Studio .Net 2005, if you try to write some code for an event of a control, in the code-behind file now you will not see the codes that were automatically generated by VS.Net. Instead you will see the code-behind file for an .aspx page which will be a partial class for that page. Since, a class file can be slit across different files, the automatic code generated are placed in a different file which will have a partial class for that .aspx page. Thus, you will see very little code in the code-behind file.
Partial classes are declared using the "partial" keyword. Let us look at the sample code that is given below in C#, which implements a class as partial classes in different files.
Example
//FirstFile.cs
using System;
interface IPartialClass
{
void PrintAString(string s);
void PrintAString();
}
partial class MyPartialClass
{
private string s = "Variable from a separate Partial Class.";
}
class MainClass
{
static void Main(string[] args)
{
MyPartialClass PClass = new MyPartialClass();
PClass.PrintAString("String from a Partial Class.");
PClass.PrintAString();
Console.ReadLine();
}
}
//SecondFile.cs
using System;
public partial class MyPartialClass : IPartialClass
{
public void PrintAString(string str)
{
Console.WriteLine(str);
}
}
//ThirdFile.cs
using System;
partial class MyPartialClass
{
public void PrintAString()
{
Console.WriteLine(s);
}
}
The code given above has three different files, FirstFile.cs, SecondFile.cs, and ThirdFile.cs. The first file has an interface IPartialClass declared in it. This interface is implemented in two other files, SecondFile.cs and ThirdFile.cs. You may note that the method PrintAString(string str) is implemented in the SecondFile.cs and the method PrintAString() is implemented in the ThirdFile.cs. You may note that the string variable 's' is declared in the FirstFile.cs. The IL produced after compiling this code in VS.Net 2005 will look the same as if compiled in earlier versions of VS.Net 2005 and the CLR finds no difference in the IL produced.
If you are not implementing all of the methods in the interface in the partial classes, you will get a compilation error stating that you have not implemented all of the methods. This is the same type of error that you get if you have a single class in a single file.
The PrintAString method in the above example is an overloaded method. A unique overloaded method is implemented in the SecondFile.cs and the ThirdFile.cs respectively. If the signature for the overloaded method in different files does not differ, you will get an error stating that the signature are the same, which is again the same type of error that you get when you have a single class instead of partial classes.
The intelligence in Visual Studio .Net 2005 recognizes the partial class and the implementation of the interfaces across different files. You may note that the private string variable declared in the FirstFile, has the scope in the ThirdFile.cs in which that string variable is actually used.
This type of splitting the class across different files, help in increasing the productivity, as a single big class running into thousands of lines of code can be split into different files. Each of these file can have a different functionality to be developed, which can be given to different developers.
There is no modification done in the CLR for partial classes. The implementation of partial classes is done by the compiler for VB.Net and C# in Visual Studio .Net itself. For the CLR, whether you implement the code using partial classes or the class being in a single file, is the same. The compiler, when compiling the code in VS.Net 2005, looks for the partial classes and combines them so that the end result will look as if the code were written in a single class. After compilation the IL produced will look the same for both the implementation.
In Visual Studio .Net 2005, if you try to write some code for an event of a control, in the code-behind file now you will not see the codes that were automatically generated by VS.Net. Instead you will see the code-behind file for an .aspx page which will be a partial class for that page. Since, a class file can be slit across different files, the automatic code generated are placed in a different file which will have a partial class for that .aspx page. Thus, you will see very little code in the code-behind file.
Partial classes are declared using the "partial" keyword. Let us look at the sample code that is given below in C#, which implements a class as partial classes in different files.
Example
//FirstFile.cs
using System;
interface IPartialClass
{
void PrintAString(string s);
void PrintAString();
}
partial class MyPartialClass
{
private string s = "Variable from a separate Partial Class.";
}
class MainClass
{
static void Main(string[] args)
{
MyPartialClass PClass = new MyPartialClass();
PClass.PrintAString("String from a Partial Class.");
PClass.PrintAString();
Console.ReadLine();
}
}
//SecondFile.cs
using System;
public partial class MyPartialClass : IPartialClass
{
public void PrintAString(string str)
{
Console.WriteLine(str);
}
}
//ThirdFile.cs
using System;
partial class MyPartialClass
{
public void PrintAString()
{
Console.WriteLine(s);
}
}
The code given above has three different files, FirstFile.cs, SecondFile.cs, and ThirdFile.cs. The first file has an interface IPartialClass declared in it. This interface is implemented in two other files, SecondFile.cs and ThirdFile.cs. You may note that the method PrintAString(string str) is implemented in the SecondFile.cs and the method PrintAString() is implemented in the ThirdFile.cs. You may note that the string variable 's' is declared in the FirstFile.cs. The IL produced after compiling this code in VS.Net 2005 will look the same as if compiled in earlier versions of VS.Net 2005 and the CLR finds no difference in the IL produced.
If you are not implementing all of the methods in the interface in the partial classes, you will get a compilation error stating that you have not implemented all of the methods. This is the same type of error that you get if you have a single class in a single file.
The PrintAString method in the above example is an overloaded method. A unique overloaded method is implemented in the SecondFile.cs and the ThirdFile.cs respectively. If the signature for the overloaded method in different files does not differ, you will get an error stating that the signature are the same, which is again the same type of error that you get when you have a single class instead of partial classes.
The intelligence in Visual Studio .Net 2005 recognizes the partial class and the implementation of the interfaces across different files. You may note that the private string variable declared in the FirstFile, has the scope in the ThirdFile.cs in which that string variable is actually used.
This type of splitting the class across different files, help in increasing the productivity, as a single big class running into thousands of lines of code can be split into different files. Each of these file can have a different functionality to be developed, which can be given to different developers.
Subscribe to:
Comments (Atom)