如何在ASP.NET 3.5中创建和使用类 [英] How to create and use class in asp.net 3.5

查看:116
本文介绍了如何在ASP.NET 3.5中创建和使用类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用ASP.NET3.5版本.我不知道如何在程序中使用单独的类文件.谁能帮我在asp.net 3.5中创建和使用类,并将其用于sql连接.

谢谢您

Hi,

I''m using ASP.NET3.5 version. I dont know how to use seperate class files in program. Can anyone please help me to create and use class in asp.net 3.5 and use it for sql connection.

Thank You

推荐答案

请参阅有关CP的好文章,该文章演示了如何从SQL Server插入和读取数据:
通过C#访问SQL Server的入门指南 [ ^ ]

是一种构造,通过将其他类型,方法和事件的变量组合在一起,可以使您创建自己的自定义类型.
它定义类型的数据和行为.
请参阅有关以下MSDN文章的更多详细信息:
类(C#编程指南) [
Refer a good article on CP which demonstrate how to insert and read data from a SQL Server:
Beginners guide to accessing SQL Server through C#[^]

A class is a construct that enables you to create your own custom types by grouping together variables of other types, methods and events.
It defines the data and behavior of a type.
Refer more details on following MSDN article:
Classes (C# Programming Guide)[^]

This is how you can create a class file and define connection string which can be accessed in you solution all over by creating object of this class file.
public class DBConnection
    {      
        public static string cnnStr;
        public SqlConnection cnTransact;
       
        public string GetConnectionString
        {
            get
            {
                return "ConnectionString"; //Connection strings name in Web.config file
            }
        }

        public DBConnection()
        {
            cnTransact = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        }
    }


Web.config文件:


Web.config file:

<connectionstrings>
    <add name="ConnectionString" connectionstring="Server=SQLExpress;uid=abc;pwd=123;database=DBName" providername="System.Data.SqlClient" />
</connectionstrings>


创建类文件的对象:


Creating object of class file:

DBConnection DB = new DBConnection();


通过此数据库对象,您可以访问解决方案中该类文件中的所有方法和属性. (请注意,DBConnection是类文件名)

打开连接:


By this DB object you can access all the methods and properties in that class file in you solution. (Note that, DBConnection is class file name)

To open connection:

DB.cnTransact.Open();


关闭连接:


To close connection:

DB.cnTransact.Close();



请参阅有关类的更多信息:
C#类 [ ^ ]
在C#中创建简单类 [使用C#和.NET创建类工厂 [ ^ ]



Refer more about classes:
C# Class[^]
Creating a Simple Class in C#[^]
Creating a Class Factory with C# and .NET[^]


这是一个非常大的问题要回答.您需要用谷歌搜索这个问题.浏览YouTube中的视频,ASP.Net3.5的教程,开始学习ASP.Net3.5.如果您尝试在Google中搜索需求,则有很多可用的资源.
对于初学者:
http://www.w3schools.com/aspnet/default.asp [如何包含我自己的课程文件 [如何将C Sharp类文件添加到我的项目RSS
[ ^ ]
如何使用ASP.NET将class.cs文件转换为DLL
[ ^ ]
Creating_Class_File_Asp.Net [
This is a very huge question to answer. You need to google for this question. Go through videos in YouTube, tutorials of ASP.Net3.5, beginning to learn ASP.Net3.5. There are many resources available if you try to put efforts in searching your requirements in google.
For starters:
http://www.w3schools.com/aspnet/default.asp[^]
how to include my own class file[^]
How to add c sharp class file to my project RSS
[^]
how to convert class(.cs) file to DLL using ASP.NET
[^]
Creating_Class_File_Asp.Net[^]


我不知道如何在程序中使用单独的类文件.谁能帮助我在asp.net 3.5中创建和使用类
对于ASP.NET,在App_Code文件夹中添加类.它们将直接得到编译和引用.

您需要做的是:
1.右键单击您的项目,然后选择选项添加ASP.NET文件夹"-> " App_Code",后跟
2.右键单击以再次进行投影,然后选择添加新项"-> myNewClass.cs

一个类将添加到您的Web应用程序中.您可以根据需要使用它.
I dont know how to use seperate class files in program. Can anyone please help me to create and use class in asp.net 3.5
For ASP.NET, add classes in App_Code folder. They will directly get compiled and referenced.

All you need to do is:
1. right click your project and select option to ''Add ASP.NET Folder'' -> ''App_Code'', followed by
2. right click to project again and selecting ''Add new Item'' -> myNewClass.cs

A class will be added in your web application. You can use it as per your need.


这篇关于如何在ASP.NET 3.5中创建和使用类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆