c# - 如何将字段绑定(bind)到文本框

标签 c# asp.net sql-server

我需要将字段(ComputerTag)绑定(bind)到文本字段。

这是我的代码:

public void load() 
{
    //Intializing sql statement
    string sqlStatement = "SELECT Computertag FROM Computer WHER 
    ComputerID=@ComputerID";

    SqlCommand comm = new SqlCommand();
    comm.CommandText = sqlStatement;
    int computerID = int.Parse(Request.QueryString["ComputerID"]);

    //get database connection from Ideal_dataAccess class
    SqlConnection connection = Ideal_DataAccess.getConnection();
    comm.Connection = connection;

    comm.Parameters.AddWithValue("@ComputerID", computerID);

    try
    {
       connection.Open();
       comm.ExecuteNonQuery();
       //Bind the computer tag value to the txtBxCompTag Text box
       txtBxCompTag.Text= string.Format("<%# Bind(\"{0}\") %>", "Computertag");

    }
    catch (Exception ex)
    {
        Utilities.LogError(ex);
        throw ex;
    }
    finally
    {
        connection.Close();
    }
}

但是 "txtBxCompTag.Text = string.Format("<%# Bind(\"{0}\") %>", "Computertag");"这行代码不会将值绑定(bind)到文本框。如何将值分配给文本框?

最佳答案

您可以使用ExecuteReader

private static void CreateCommand(string queryString,
    string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        connection.Open();

        SqlCommand command = new SqlCommand(queryString, connection);
        SqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            Console.WriteLine(String.Format("{0}", reader[0]));
        }
    }
}

以上代码来自msdn:http://msdn.microsoft.com/en-us/library/9kcbe65k(v=vs.90).aspx

关于c# - 如何将字段绑定(bind)到文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9271531/

相关文章:

asp.net - Umbraco 7.2 无法添加 Controller

c# - 如何传递自定义查询结果以在asp.net中查看

c# - 影响如何在 EF 6 代码优先中从数据库创建实体类

sql-server - T-SQL 检查表是否存在于模式中

c# - 是否有可能使用 MSMQ MessageQueue.Peek 超时丢失消息?

c# - 为什么 [SetUpFixture] 中的 [OneTimeSetUp] 在 (Test,TestCaseSource()) 之后调用

c# - C# 中的 DataGridViewRow 事件获取所选行

c# - MEF 插件依赖项未加载,因为未检查插件目录

c# - 通用类和 Type.GetType()

sql-server - 一次更新 sql server xml 数据字段上的多个属性