如何将数据从数据库显示到文本框中 [英] how to display data from database into textbox

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

问题描述

大家好,



i需要帮助才能将数据库显示到textbox.i不知道怎么做。有些人帮我。我已经尝试过编写其他来源的代码但它没有返回任何内容或错误。我不知道完成它的方式。然后显示后,我想通过文本框更新数据。



p / s:数据从数据库的第4列开始

hi all,

i need help on displaying database into textbox.i dont know how to do it.pls anyone help me.i already try coding from other sources but it return nothing or error.i dont know exactly the way of doing it.then after display, i want to update the data through textbox.

p/s: the data is starting from column 4th from database

推荐答案

首先请清除你的问题,需要更多的规范或增强你想要的蚂蚁你已经尝试了什么。 。



根据我对你的问题的理解我回复了它...



1)设计你的asp.net页面,其中包含显示数据所需的文本框数量,如果你需要显示1列数据然后创建或设计1个具有适当id或名称的文本框...



2)在代码behinde页面中,您需要编写用于连接数据库并从中获取数据的代码数据库...



3)从数据库获取数据后,您需要将该数据与文本框绑定..



4)如你所说,你需要更新数据,所以你需要放一个按钮来处理数据库数据的更新...





i认为你可以用自己的方式执行第一步..我在这里给出一个带有我自己的文本框ID的1列数据绑定示例,你需要的是你需要以你自己的方式修改我的简单代码并对您的解决方案进行适当的更改...



您的数据库操作可以使用这样的代码..

First of all please clear your question, need some more specification or enhancement about what you want ant what you are trying already..

as per my understanding about your question i replied it...

1) first of designed your asp.net page with number of textboxes you need to show the data like if you need to show 1 columns data then create or designed 1 textboxes with appropriate id or name...

2) in code behinde page you need to write the code for connecting to database and getting data from that database...

3) after getting data from database you need to bind that data with your text box..

4) as you mention that you need to update the data so you need to put one button for handaling updatation of database data...


i think you can perform 1st step with your own way.. and i here give a 1 column data binding example with my own textbox id, what you need is you need to modify my simple code in your own way and make appropriate changes for your solution...

for your database operation you can use code like this..
protected void CreateConnection()
{
    try
    {
        if (SqlCon == null)
        {
            string ConnectionString = "YOUR CONNECTION STRING";
            SqlCon = new SqlConnection(ConnectionString);
        }
        if (Da == null)
        {
            Da = new SqlDataAdapter();
        }
    }
    catch (Exception exe)
    {
        throw exe;
    }
}

protected void OpenConnection()
{
    if (SqlCon != null && SqlCon.State != ConnectionState.Open)
    {
        SqlCon.Open();
    }
}
protected void CloseConnection()
{
    if (SqlCon.State != ConnectionState.Closed)
        SqlCon.Close();
}





获取您可以使用的结果数据..



for getting resulted data you can use like this..

CreateConnection();
DataTable resultTable = new DataTable("YOURRESULTEDDATATABLENAME");
OpenConnection();
Da.SelectCommand = new SqlCommand();
Da.SelectCommand.Connection = SqlCon;
Da.SelectCommand.CommandType = CommandType.Text;
Da.SelectCommand.CommandText = "YOUR SELECT QUERY THAT WILL RETURN YOUR COLUMNS DATA";
Da.Fill(resultTable);

if (resultTable.Rows.Count > 0)
{
 // here i use only one record to disply so i give row number static that is 0 
    TextBox1.Text = resultTable.Rows[0]["YOUR COLUMN NAME"].ToString();
}
CloseConnection();





对于更新按钮查询,你需要使用类似的东西...



For update button query you need use something like this...

CreateConnection();
OpenConnection();
Da.UpdateCommand = new SqlCommand();
Da.UpdateCommand.Connection = SqlCon;
Da.UpdateCommand.CommandText = "YOUR UPDATE QUERY QUERY";
Da.UpdateCommand.ExecuteNonQuery();
CloseConnection();





i希望这可以帮助您或进一步细节,您可以看到此链接..



数据绑定 [ ^ ]


您好,



尝试绑定如下文本框,



方法1 :

获取数据集后,将第4列数据存储在变量中并设置文本框的值。



Hi,

Try binding the text box as below,

Method 1:
After you get the dataset,store the 4th column data in a variable and set the value of the text box.

txtBox_description.Text = String





方法2:

将数据集的列绑定到文本框。





Method 2:
Bind the column of the dataset to the textbox.

yourtxbxid.DataBindings.Add("Text",dsYourDataSet,"Datasetname.ColumnName");





请查看以下链接。



http://www.java2s.com/Code/CSharp/Database-ADO.net/BindDataSettoTextBox.htm [ ^ ]



希望这会有所帮助。



Please have a look at the below link for example.

http://www.java2s.com/Code/CSharp/Database-ADO.net/BindDataSettoTextBox.htm[^]

Hope this helps.


这篇关于如何将数据从数据库显示到文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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