如何从SQL检索和显示数据到Aspx Webform [英] How to retrieve and display data from SQL to Aspx webform

查看:87
本文介绍了如何从SQL检索和显示数据到Aspx Webform的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是.net的新手,我正尝试创建一个简单的网站.

i'm a newbie in.net and im try to create a simple site.

我有一个多行文本框,具有固定的宽度和高度.

I have this textbox that is multiline and has fix width and height.

<asp:TextBox ID="TextBox1" runat="server" Height="168px" TextMode="MultiLine" 
                            Width="303px"></asp:TextBox>

我还创建了一个web.config来连接到我的sql数据库:

i have also created a web.config to connect to my sql database:

<connectionStrings>
<add name="TDBSConnectionString" connectionString="Data Source=local;Initial Catalog=IBSI;Persist Security Info=True;User ID=sa;Password=1"
  providerName="System.Data.SqlClient" />

如何从数据库中检索数据并将内容显示在上面的文本框中.我使用多行,因为数据不止一个.

How can i retrieve a data from my database and display the content into the textbox above. i use multiline because the data is more than one.

推荐答案

已查看此msdn教程:

have look to this msdn tutorial : Retrieving Data Using the DataReader

示例:

        SqlDataReader rdr = null;
        SqlConnection con = null;
        SqlCommand cmd = null;

        try
        {
            // Open connection to the database
            string ConnectionString = "server=xeon;uid=sa;"+
                "pwd=manager; database=northwind";
            con = new SqlConnection(ConnectionString);
            con.Open();

            // Set up a command with the given query and associate
            // this with the current connection.
            string CommandText = "SELECT FirstName, LastName" +
                                 "  FROM Employees" +
                                 " WHERE (LastName LIKE @Find)";
            cmd = new SqlCommand(CommandText);
            cmd.Connection = con;

            // Add LastName to the above defined paramter @Find
            cmd.Parameters.Add(
                new SqlParameter(
                "@Find", // The name of the parameter to map
                System.Data.SqlDbType.NVarChar, // SqlDbType values
                20, // The width of the parameter
                "LastName"));  // The name of the source column

            // Fill the parameter with the value retrieved
            // from the text field
            cmd.Parameters["@Find"].Value = txtFind.Text;

            // Execute the query
            rdr = cmd.ExecuteReader();

            // Fill the list box with the values retrieved
            lbFound.Items.Clear();
            while(rdr.Read())
            {
                lbFound.Items.Add(rdr["FirstName"].ToString() +
                " " + rdr["LastName"].ToString());
            }
        }
        catch(Exception ex)
        {
            // Print error message
            MessageBox.Show(ex.Message);
        }
        finally
        {
            // Close data reader object and database connection
            if (rdr != null)
                rdr.Close();

            if (con.State == ConnectionState.Open)
                con.Close();
        }

这篇关于如何从SQL检索和显示数据到Aspx Webform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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