ASP.NET 数据库连接

发布于 2024-09-28 22:21:07 字数 589 浏览 0 评论 0原文

我的 ASP.NET 项目的 web.config 文件中有正确的条目。如何确保连接已成功建立?我想从数据库中提取图像,然后将其显示在我的 aspx 页面上。需要注意的一些事项:我使用的是 Visual Studio 2010、SQL Server 2008、.NET 4.0
这是我的 web.config 文件中的相关部分,

<databases>    
    <add key="MyConnection" name="MyName" value="server=servername\SQL2008;uid=myuid;pwd=mypwd;database=databaseName;"/>  
    <add key="DataAccessClass" value="DataAccessSql"/>  
</databases> 

我的项目中还没有任何 app.config 文件。令人惊讶的是,我的 web.config 中没有 部分。我需要明确添加一项吗?

I have the correct entries in the web.config file in my ASP.NET project. How do I make sure that the connection has been established successfully? I want to pull an image form my database and then display it on my aspx page. Some things to note: I am using Visual Studio 2010, SQL server 2008, .NET 4.0
Here is the relevant part from my web.config file

<databases>    
    <add key="MyConnection" name="MyName" value="server=servername\SQL2008;uid=myuid;pwd=mypwd;database=databaseName;"/>  
    <add key="DataAccessClass" value="DataAccessSql"/>  
</databases> 

I do not have any app.config file in my project yet. Surprisingly there is not section in my web.config. Do I need to add one explicitly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挖鼻大婶 2024-10-05 22:21:07

Web.config 文件只是存储配置设置。为了建立与数据库的连接,您需要编写一些代码。

ASP.NET - 数据库连接

创建与 SQL Server 的连接

Web.config file is just a store for configuration settings. In order to establish a connection to the database you need to write some code.

ASP.NET - Database Connection

Creating Connections to SQL Server

智商已欠费 2024-10-05 22:21:07

web.config 文件中输入的连接参数只是参数。没有建立实际的连接。事实上,可以在 web.config 中设置多个连接参数,并可以在运行时进行实际连接。

要实际发生连接,您需要定义

例如,此处 SQLDataSource 设置为连接到 ConnectionStrings.MyNorthwind

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
      <asp:SqlDataSource
          id="SqlDataSource1"
          runat="server"
          DataSourceMode="DataReader"
          ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
          SelectCommand="SELECT LastName FROM Employees">
      </asp:SqlDataSource>

      <asp:ListBox
          id="ListBox1"
          runat="server"
          DataTextField="LastName"
          DataSourceID="SqlDataSource1">
      </asp:ListBox>

    </form>
  </body>
</html>

或在第二个示例中,它们显式创建 SqlConnection。这里的连接字符串将从 web.config 中获取。

private static void ReadOrderData(string connectionString)
{
    string queryString = 
        "SELECT OrderID, CustomerID FROM dbo.Orders;";
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(
            queryString, connection);
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();
        try
        {
            while (reader.Read())
            {
                Console.WriteLine(String.Format("{0}, {1}",
                    reader[0], reader[1]));
            }
        }
        finally
        {
            // Always call Close when done reading.
            reader.Close();
        }
    }
}

这是在 web.config 中找到的连接字符串的示例

<configuration>
  <connectionStrings>
    <add name="ApplicationServices"
         connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
         providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

The connection parameters entered in the web.config file are just parameters. No actual connections are made. In fact, multiple connection parameters can be made in the web.config and the actual connection can be made at runtime.

To actually make a connection happen, you need to define

For instance, here the SQLDataSource is set to connect to ConnectionStrings.MyNorthwind

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
      <asp:SqlDataSource
          id="SqlDataSource1"
          runat="server"
          DataSourceMode="DataReader"
          ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
          SelectCommand="SELECT LastName FROM Employees">
      </asp:SqlDataSource>

      <asp:ListBox
          id="ListBox1"
          runat="server"
          DataTextField="LastName"
          DataSourceID="SqlDataSource1">
      </asp:ListBox>

    </form>
  </body>
</html>

or in this second example, where they explicitely create an SqlConnection. The connectionstring here would be taken from the web.config.

private static void ReadOrderData(string connectionString)
{
    string queryString = 
        "SELECT OrderID, CustomerID FROM dbo.Orders;";
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(
            queryString, connection);
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();
        try
        {
            while (reader.Read())
            {
                Console.WriteLine(String.Format("{0}, {1}",
                    reader[0], reader[1]));
            }
        }
        finally
        {
            // Always call Close when done reading.
            reader.Close();
        }
    }
}

Here is an example of a connection string to be found in your web.config

<configuration>
  <connectionStrings>
    <add name="ApplicationServices"
         connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
         providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文