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

我是研究僧i 提交于 2020-01-05 07:37:59

问题


My question is

Suppose I have a Column "fname" whose value is 'Nikhil' in table "profile".

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

I mean what should be the code if I want label text to be "fname" value that is "Nikhil"

Connection is already done properly because I am able to display table data in Gridview.

label1.Text = ?; // I want fname here

Regards,

Nikhil


回答1:


Go to MSDN to learn it 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.




回答2:


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



回答3:


First Create A connection Class in App_code folder and dont forget to set database path

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

<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



来源:https://stackoverflow.com/questions/6909924/how-to-retrieve-column-value-of-sql-server-table-and-store-it-in-label-text-of-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!