简体   繁体   English

如何检索SQL Server表的列值并将其存储在label.C#ASP.Net中的文本

[英]How to retrieve column value of sql server table and store it in label.Text of c# ASP.Net

My question is 我的问题是

Suppose I have a Column "fname" whose value is 'Nikhil' in table "profile". 假设我在“配置文件”表中有一列“ fname”,其值为“ Nikhil”。

How to retrieve column value of sql server table and store it in label.Text of c# ASP.Net. 如何检索SQL Server表的列值并将其存储在C#ASP.Net的label.Text中。

I mean what should be the code if I want label text to be "fname" value that is "Nikhil" 我的意思是,如果我希望标签文本为“ Nikhil”的“ fname”值,应该是什么代码

Connection is already done properly because I am able to display table data in Gridview. 连接已经正确完成,因为我能够在Gridview中显示表数据。

label1.Text = ?; label1.Text =?; // I want fname here //我想在这里使用fname

Regards, 问候,

Nikhil 尼基尔

Go to MSDN to learn it http://msdn.microsoft.com/en-us/bb188199 . 转到MSDN进行学习http://msdn.microsoft.com/en-us/bb188199

Here's a sample on how to connect to a database. 这是有关如何连接到数据库的示例。

    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();

            // Call Read before accessing data.
            while (reader.Read())
            {
                Console.WriteLine(String.Format("{0}, {1}",
                    reader[0], reader[1]));
            }

            // Call Close when done reading.
            reader.Close();
        }
    }

There are many resources out there, try to search first before posting question. 那里有很多资源,请在发布问题之前先搜索。

Firstly, I established a connection 首先,我建立了联系

SqlConnection con = new SqlConnection("data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true");
SqlCommand cmd = new SqlCommand();

and then, 接着,

cmd.CommandText = "select fname from table where qid=1";
cmd.Connection = con;
string fname = ((string)cmd.ExecuteScalar());

or 要么

Label1.text = ((string)cmd.ExecuteScalar());

First Create A connection Class in App_code folder and dont forget to set database path 首先在App_code文件夹中创建一个连接类,不要忘记设置数据库路径

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// Summary description for Connection
/// </summary>
public class Connection

{
    SqlConnection con = new SqlConnection();
    SqlDataAdapter ad;
    SqlCommand cmd;
    SqlDataReader rd;
    public Connection()
    {
        // Set Your Database Path Here from C:\user onwords
        con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\SIROI.COM\Documents\Visual Studio 2008\WebSites\WebSite14\App_Data\ASPNETDB.MDF;Integrated Security=True;User Instance=True";

    }

    public DataSet filldata(DataSet ds, string query, string tbname)
    {
        try
        {
            con.Open();
            ad = new SqlDataAdapter(query, con);
            ad.Fill(ds, tbname);

        }
        catch (SqlException ex)
        {
        }
        finally
        {
            con.Close();
        }
        return ds;
    }
    public bool ExecuteQuery(string query)
    {
        bool flag = false;
        try
        {
            con.Open();
            cmd = new SqlCommand(query, con);
            int a = cmd.ExecuteNonQuery();
            if (a > 0)
            {
                flag = true;
            }          
        }
        catch(Exception ex)
        {

        }
        finally
        {
            con.Close();
        }
        return flag;
    }
    public SqlDataReader ExecuteReader(string query)
    {
        try
        {
            con.Open();
            cmd = new SqlCommand(query, con);
            rd = cmd.ExecuteReader();
        }
        catch (Exception ex)
        {

        }

        return rd;
    }
}

Now create data source by calling connection 现在通过调用连接创建数据源

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ASPNETDBConnectionString1 %>" 
    SelectCommand="Your * SQL Query">
    <SelectParameters>
        <asp:QueryStringParameter Name="Your param" QueryStringField="Your Field" 
            Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

Now at last Create a Label and set field name to retrive in Bind Function 现在,最后创建一个Label并设置要在Bind Function中检索的字段名称

<asp:Label ID="Label6" runat="server" Text='<%# Bind("your Field") %>'></asp:Label>

Regards http://www.siroi.com Dont Forget to like us on Facebook http://www.facebook.com/siroi.india 问候http://www.siroi.com不要忘记像我们在Facebook http://www.facebook.com/siroi.india

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检索SQL Server 2005表的列值并将其存储在label.C#ASP.Net中的文本 - How to retrieve column value of sql server 2005 table and store it in label.Text of c# ASP.Net linq到实体检索数据库表行值并显示为label.Text C#.net - linq to entities retrieve database table row value and display as label.Text C# .net 如何在ASP.net C#中将HTML文本编辑器内容存储和检索到SQL Server DB - how to store and retrieve the HTML text Editor contents to SQL Server DB in ASP.net C# 如何将通用处理程序绑定到ASP.NET中的Label.Text - How to bind generic handler to Label.Text in ASP.NET 如何通过C#ASP.net从SQL Server中的文本框存储日期 - How to store date from text box in SQL Server through C# ASP.net 使用 C# 检查 ASP.NET 中 SQL Server 表中的列值是否为空 - Check if column value in SQL Server table is null in ASP.NET using C# 获取ASP.net C#中Gridview中的列的值/文本以显示在标签中 - Get the value/text of a column in a Gridview in ASP.net C# to display in a label 如何从asp.net中的label.text调用函数背后的代码 - How to call code behind function from label.text in asp.net 如何更改asp.net内容页面中多次单击按钮的label.Text? - How to change label.Text in asp.net content page on multiple button clicks? 如何存储将行或列值返回到ASP.NET C#变量的SQL存储过程结果? - How do I store a SQL stored procedure result that returns a row or a column value into an ASP.NET C# variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM