[摘录]asp.net项目连接数据库
2009-10-04 15:48
204 查看
Unless you have a specific reason to do so, never ask for all columns (using the * wildcard)
when you only need a part of them. This generates more traffic and stress on the database server than necessary
and slows down performance. Moreover, even if you do need to ask for all columns in the table, it’s safer
to mention them explicitly to protect your application in case the number or order of columns change in future.
// Create the connection object
SqlConnection connection = new SqlConnection();
// Set the connection string
connection.ConnectionString = "Server=(local)/SqlExpress; " +
"User ID=balloonshop; Password=ecommerce;" +
"Database=BalloonShop";
// Open the connection
connection.Open();
The code is fairly straightforward: you first create a SqlConnection object, then set its
ConnectionString property, and finally open the connection. A connection needs to be opened
before it is used for any operations.
Understanding the connection string is important—if your program has problems connecting
to the database, these problems likely can be solved by fixing the connection string (assuming
that SQL Server is properly configured and that you actually have access to it).
The connection string contains the three important elements. The first is the name of the
SQL Server instance you’re connecting to. For SQL Server 2008 Express Edition, the default
instance name is (local)/SqlExpress. You’ll want to change this if your SQL Server instance
has another name. You can use your computer name instead of (local). Of course, if you
connect to a remote SQL Server instance, you’ll need to specify the complete network path
instead of (local).
After specifying the server, you need to supply security information needed to log in to the
server. You can log in to SQL Server either by using SQL Server Authentication (in which case
you need to supply a SQL Server username and password as shown in the code snippet) or
by using Windows Authentication (also named Windows Integrated Security). With Windows
Integrated Security, you don’t have to supply a username and password, because SQL Server
uses the Windows login information of the currently logged-in user.
To log in using Windows Authentication, you’ll need to supply Integrated Security=True
(or Integrated Security=SSPI) instead of User ID=username; Password=password. The final part
of the connection string specifies the database you’ll be working with.
ASP.NET, since version 2.0, brings in the notions of themes and skins. Skins are like CSS
files in that they contain various properties, but they do this on a control-type basis, they allow
setting properties that aren’t accessible through CSS (such as an image’s src property), and
they are applied on the server side. Skin definitions are saved in files with the .skin extension
(these files can store one or more skin definitions) and look much like the definition of an ASP.NET
control. A typical skin definition looks like this:
<asp:Image runat="server" SkinID="BalloonShopLogo" src="/Images/BalloonShop.png"/>
A theme is a collection of CSS files, skins, and images. You can add more themes to a web
site and allow for easily changing the look of your site by switching the active theme at design
time or even at runtime.
In the exercise, you’ll create a new theme called BalloonShopDefault, and you’ll add a CSS
file to it, which will be then used to display the list of departments.
when you only need a part of them. This generates more traffic and stress on the database server than necessary
and slows down performance. Moreover, even if you do need to ask for all columns in the table, it’s safer
to mention them explicitly to protect your application in case the number or order of columns change in future.
// Create the connection object
SqlConnection connection = new SqlConnection();
// Set the connection string
connection.ConnectionString = "Server=(local)/SqlExpress; " +
"User ID=balloonshop; Password=ecommerce;" +
"Database=BalloonShop";
// Open the connection
connection.Open();
The code is fairly straightforward: you first create a SqlConnection object, then set its
ConnectionString property, and finally open the connection. A connection needs to be opened
before it is used for any operations.
Understanding the connection string is important—if your program has problems connecting
to the database, these problems likely can be solved by fixing the connection string (assuming
that SQL Server is properly configured and that you actually have access to it).
The connection string contains the three important elements. The first is the name of the
SQL Server instance you’re connecting to. For SQL Server 2008 Express Edition, the default
instance name is (local)/SqlExpress. You’ll want to change this if your SQL Server instance
has another name. You can use your computer name instead of (local). Of course, if you
connect to a remote SQL Server instance, you’ll need to specify the complete network path
instead of (local).
After specifying the server, you need to supply security information needed to log in to the
server. You can log in to SQL Server either by using SQL Server Authentication (in which case
you need to supply a SQL Server username and password as shown in the code snippet) or
by using Windows Authentication (also named Windows Integrated Security). With Windows
Integrated Security, you don’t have to supply a username and password, because SQL Server
uses the Windows login information of the currently logged-in user.
To log in using Windows Authentication, you’ll need to supply Integrated Security=True
(or Integrated Security=SSPI) instead of User ID=username; Password=password. The final part
of the connection string specifies the database you’ll be working with.
ASP.NET, since version 2.0, brings in the notions of themes and skins. Skins are like CSS
files in that they contain various properties, but they do this on a control-type basis, they allow
setting properties that aren’t accessible through CSS (such as an image’s src property), and
they are applied on the server side. Skin definitions are saved in files with the .skin extension
(these files can store one or more skin definitions) and look much like the definition of an ASP.NET
control. A typical skin definition looks like this:
<asp:Image runat="server" SkinID="BalloonShopLogo" src="/Images/BalloonShop.png"/>
A theme is a collection of CSS files, skins, and images. You can add more themes to a web
site and allow for easily changing the look of your site by switching the active theme at design
time or even at runtime.
In the exercise, you’ll create a new theme called BalloonShopDefault, and you’ll add a CSS
file to it, which will be then used to display the list of departments.
相关文章推荐
- 在ASP.NET项目中的web.config文件里配置数据库连接并在程序代码中获取连接字符串
- 在ASP.NET项目中的web.config文件里配置数据库连接并在程序代码中获取连接字符串
- ASP.NET项目中的web.config文件里配置数据库连接并在程序代码中获取连接字符串
- asp.net mvc 3.0详细笔记__09__创建数据库,应用EF连接模型类和数据表
- 加密数据库连接字符串(asp.net 2.0)
- asp.net连接数据库
- asp.net连接数据库 增加,修改,删除,查询代码
- ASP.net 连接interbase数据库
- ASP.NET中使用web.config配置数据库连接
- Asp.net连接数据库例程(C#):
- ASP.NET数据库编程之Access连接失败
- asp.net连接数据库sql2005
- Asp.Net连接Oracle数据库的通用类orahelper.cs并且利用它对数据库查询、汇总、更新等操作演示
- WebPart-----asp.net ajax CTP 拖拽控件使用时的数据库连接问题
- VS2010+Silverlight4中asp.net配置无法连接数据库的问题
- Asp.Net连接Oracle数据库的通用操作类并且利用它对数据库查询、汇总、更新等操作演示
- Asp.Net配置Web.config连接数据库SqlServer
- ASP.NET在IIS上部署使用Oracle数据库无法连接数据库解决方法
- asp.net入门---数据库连接和登录用户
- asp.net简单3层数据库连接读取操作